From 9b52578b9755ec3c419e90c1855af3b8b1eee142 Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 20:25:13 +0800 Subject: [PATCH 01/14] feat: add cs develop project --- .gitignore | 5 ++++ .../.idea/.idea.PrintReceipt/.idea/.gitignore | 13 +++++++++ .../.idea.PrintReceipt/.idea/indexLayout.xml | 8 ++++++ .../.idea/.idea.PrintReceipt/.idea/vcs.xml | 6 ++++ PrintReceipt/PrintReceipt.sln | 22 +++++++++++++++ .../PrintReceipt.sln.DotSettings.user | 4 +++ PrintReceipt/PrintReceipt/PrintReceipt.csproj | 10 +++++++ PrintReceipt/PrintReceipt/Program.cs | 3 ++ .../PrintReceiptTest/PrintReceiptTest.cs | 9 ++++++ .../PrintReceiptTest/PrintReceiptTest.csproj | 28 +++++++++++++++++++ PrintReceipt/PrintReceiptTest/Usings.cs | 1 + 11 files changed, 109 insertions(+) create mode 100644 .gitignore create mode 100644 PrintReceipt/.idea/.idea.PrintReceipt/.idea/.gitignore create mode 100644 PrintReceipt/.idea/.idea.PrintReceipt/.idea/indexLayout.xml create mode 100644 PrintReceipt/.idea/.idea.PrintReceipt/.idea/vcs.xml create mode 100644 PrintReceipt/PrintReceipt.sln create mode 100644 PrintReceipt/PrintReceipt.sln.DotSettings.user create mode 100644 PrintReceipt/PrintReceipt/PrintReceipt.csproj create mode 100644 PrintReceipt/PrintReceipt/Program.cs create mode 100644 PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs create mode 100644 PrintReceipt/PrintReceiptTest/PrintReceiptTest.csproj create mode 100644 PrintReceipt/PrintReceiptTest/Usings.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9369618 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/PrintReceipt/PrintReceipt/bin/* +/PrintReceipt/PrintReceipt/obj/* +/PrintReceipt/PrintReceiptTest/bin/* +/PrintReceipt/PrintReceiptTest/obj/* + diff --git a/PrintReceipt/.idea/.idea.PrintReceipt/.idea/.gitignore b/PrintReceipt/.idea/.idea.PrintReceipt/.idea/.gitignore new file mode 100644 index 0000000..844d505 --- /dev/null +++ b/PrintReceipt/.idea/.idea.PrintReceipt/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/contentModel.xml +/modules.xml +/projectSettingsUpdater.xml +/.idea.PrintReceipt.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/PrintReceipt/.idea/.idea.PrintReceipt/.idea/indexLayout.xml b/PrintReceipt/.idea/.idea.PrintReceipt/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/PrintReceipt/.idea/.idea.PrintReceipt/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/PrintReceipt/.idea/.idea.PrintReceipt/.idea/vcs.xml b/PrintReceipt/.idea/.idea.PrintReceipt/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/PrintReceipt/.idea/.idea.PrintReceipt/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt.sln b/PrintReceipt/PrintReceipt.sln new file mode 100644 index 0000000..f3ffa93 --- /dev/null +++ b/PrintReceipt/PrintReceipt.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintReceipt", "PrintReceipt\PrintReceipt.csproj", "{68AF9ACC-B110-428F-809E-B3E39B683296}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrintReceiptTest", "PrintReceiptTest\PrintReceiptTest.csproj", "{E623BEED-3353-4D15-A109-2B065A08E43C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {68AF9ACC-B110-428F-809E-B3E39B683296}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {68AF9ACC-B110-428F-809E-B3E39B683296}.Debug|Any CPU.Build.0 = Debug|Any CPU + {68AF9ACC-B110-428F-809E-B3E39B683296}.Release|Any CPU.ActiveCfg = Release|Any CPU + {68AF9ACC-B110-428F-809E-B3E39B683296}.Release|Any CPU.Build.0 = Release|Any CPU + {E623BEED-3353-4D15-A109-2B065A08E43C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E623BEED-3353-4D15-A109-2B065A08E43C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E623BEED-3353-4D15-A109-2B065A08E43C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E623BEED-3353-4D15-A109-2B065A08E43C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/PrintReceipt/PrintReceipt.sln.DotSettings.user b/PrintReceipt/PrintReceipt.sln.DotSettings.user new file mode 100644 index 0000000..f8f41ff --- /dev/null +++ b/PrintReceipt/PrintReceipt.sln.DotSettings.user @@ -0,0 +1,4 @@ + + <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &lt;PrintReceiptTest&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Project Location="/Users/duming.zhu/code/homework-2-test-first-development/PrintReceipt/PrintReceiptTest" Presentation="&lt;PrintReceiptTest&gt;" /> +</SessionState> \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/PrintReceipt.csproj b/PrintReceipt/PrintReceipt/PrintReceipt.csproj new file mode 100644 index 0000000..b9de063 --- /dev/null +++ b/PrintReceipt/PrintReceipt/PrintReceipt.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/PrintReceipt/PrintReceipt/Program.cs b/PrintReceipt/PrintReceipt/Program.cs new file mode 100644 index 0000000..e5dff12 --- /dev/null +++ b/PrintReceipt/PrintReceipt/Program.cs @@ -0,0 +1,3 @@ +// See https://aka.ms/new-console-template for more information + +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs new file mode 100644 index 0000000..e9d9dc6 --- /dev/null +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -0,0 +1,9 @@ +namespace PrintReceiptTest; + +public class UnitTest1 +{ + [Fact] + public void Test1() + { + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.csproj b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.csproj new file mode 100644 index 0000000..27b4eaf --- /dev/null +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.csproj @@ -0,0 +1,28 @@ + + + + net6.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/PrintReceipt/PrintReceiptTest/Usings.cs b/PrintReceipt/PrintReceiptTest/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/PrintReceipt/PrintReceiptTest/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file -- Gitee From e3a351ade3a844669f74d88055f49423d43aa8dc Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 20:34:21 +0800 Subject: [PATCH 02/14] test: add INVALID_BARCODE test for ValidateBarcodes --- .../PrintReceipt.sln.DotSettings.user | 4 +++- .../PrintReceipt/BarcodesValidator.cs | 23 +++++++++++++++++++ .../Enums/ValidationResultType.cs | 8 +++++++ PrintReceipt/PrintReceipt/PrintReceipt.csproj | 4 ++++ PrintReceipt/PrintReceipt/Product.cs | 8 +++++++ PrintReceipt/PrintReceipt/SelectedBarcodes.cs | 12 ++++++++++ PrintReceipt/PrintReceipt/ValidationResult.cs | 10 ++++++++ .../PrintReceiptTest/PrintReceiptTest.cs | 20 ++++++++++++++-- 8 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 PrintReceipt/PrintReceipt/BarcodesValidator.cs create mode 100644 PrintReceipt/PrintReceipt/Enums/ValidationResultType.cs create mode 100644 PrintReceipt/PrintReceipt/Product.cs create mode 100644 PrintReceipt/PrintReceipt/SelectedBarcodes.cs create mode 100644 PrintReceipt/PrintReceipt/ValidationResult.cs diff --git a/PrintReceipt/PrintReceipt.sln.DotSettings.user b/PrintReceipt/PrintReceipt.sln.DotSettings.user index f8f41ff..e316112 100644 --- a/PrintReceipt/PrintReceipt.sln.DotSettings.user +++ b/PrintReceipt/PrintReceipt.sln.DotSettings.user @@ -1,4 +1,6 @@  <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &lt;PrintReceiptTest&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <Project Location="/Users/duming.zhu/code/homework-2-test-first-development/PrintReceipt/PrintReceiptTest" Presentation="&lt;PrintReceiptTest&gt;" /> -</SessionState> \ No newline at end of file +</SessionState> + + \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/BarcodesValidator.cs b/PrintReceipt/PrintReceipt/BarcodesValidator.cs new file mode 100644 index 0000000..7f79068 --- /dev/null +++ b/PrintReceipt/PrintReceipt/BarcodesValidator.cs @@ -0,0 +1,23 @@ +using PrintReceipt.Enums; + +namespace PrintReceipt; + +public class BarcodesValidator +{ + public static ValidationResult ValidateBarcodes(SelectedBarcodes selectBarcodes, IList? products) + { + var validationResult = new ValidationResult(); + foreach (var barcodes in selectBarcodes.SelectedBarcodeList) + { + var barcodeDetails = barcodes.Split("-"); + if (barcodeDetails.Length != 2 || + barcodeDetails[1].Length == 0) + { + validationResult.Error = ValidationResultType.INVALID_BARCODE; + validationResult.Barcode = barcodeDetails[0]; + break; + } + } + return validationResult; + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/Enums/ValidationResultType.cs b/PrintReceipt/PrintReceipt/Enums/ValidationResultType.cs new file mode 100644 index 0000000..fda8896 --- /dev/null +++ b/PrintReceipt/PrintReceipt/Enums/ValidationResultType.cs @@ -0,0 +1,8 @@ +namespace PrintReceipt.Enums; + +public enum ValidationResultType +{ + VALID=1, + INVALID_BARCODE=2, + INVALID_PRODUCT_NOT_FIND=3, +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/PrintReceipt.csproj b/PrintReceipt/PrintReceipt/PrintReceipt.csproj index b9de063..2e1277b 100644 --- a/PrintReceipt/PrintReceipt/PrintReceipt.csproj +++ b/PrintReceipt/PrintReceipt/PrintReceipt.csproj @@ -7,4 +7,8 @@ enable + + + + diff --git a/PrintReceipt/PrintReceipt/Product.cs b/PrintReceipt/PrintReceipt/Product.cs new file mode 100644 index 0000000..546709f --- /dev/null +++ b/PrintReceipt/PrintReceipt/Product.cs @@ -0,0 +1,8 @@ +namespace PrintReceipt; + +public class Product +{ + public string Name { set; get; } + public double Price { set; get; } + public string Barcode { set; get; } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/SelectedBarcodes.cs b/PrintReceipt/PrintReceipt/SelectedBarcodes.cs new file mode 100644 index 0000000..da84ea9 --- /dev/null +++ b/PrintReceipt/PrintReceipt/SelectedBarcodes.cs @@ -0,0 +1,12 @@ +namespace PrintReceipt; +using Newtonsoft.Json; + +public class SelectedBarcodes +{ + public IList SelectedBarcodeList { get; set; } + + public SelectedBarcodes(string selectBarcodesJson) + { + SelectedBarcodeList = JsonConvert.DeserializeObject>(selectBarcodesJson); + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/ValidationResult.cs b/PrintReceipt/PrintReceipt/ValidationResult.cs new file mode 100644 index 0000000..76f1a7a --- /dev/null +++ b/PrintReceipt/PrintReceipt/ValidationResult.cs @@ -0,0 +1,10 @@ +using PrintReceipt.Enums; + +namespace PrintReceipt; + +public class ValidationResult +{ + public string Barcode { set; get; } + + public ValidationResultType Error { set; get; } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index e9d9dc6..ea5a36b 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -1,9 +1,25 @@ +using Newtonsoft.Json; +using PrintReceipt; +using PrintReceipt.Enums; + namespace PrintReceiptTest; -public class UnitTest1 +public class PrintReceiptTest { [Fact] - public void Test1() + public void Should_Get_INVALID_BARCODE_ValidationResult_If_SelectedBarcodes_Dont_Contains_Amount() { + // Given + string selectBarcodesJson = "[\"ITEM000003\"]"; + string productsJson = @"[{""name"":""phone"",""price"":2000,""barcode"":""ITEM000003""}]"; + var selectBarcodes = new SelectedBarcodes(selectBarcodesJson); + var products = JsonConvert.DeserializeObject>(productsJson); + + // when + var validationResult = BarcodesValidator.ValidateBarcodes(selectBarcodes, products); + + // then + Assert.Equal(validationResult.Error, ValidationResultType.INVALID_BARCODE); + Assert.Equal(validationResult.Barcode, "ITEM000003"); } } \ No newline at end of file -- Gitee From c9cf06afd3573f30d6dedee2ea8a55a1723f93bc Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 20:42:34 +0800 Subject: [PATCH 03/14] test: add PRODUCT_NOT_FIND test for ValidateBarcodes --- .../PrintReceipt/BarcodesValidator.cs | 21 +++++++++++++++++++ .../PrintReceiptTest/PrintReceiptTest.cs | 17 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/PrintReceipt/PrintReceipt/BarcodesValidator.cs b/PrintReceipt/PrintReceipt/BarcodesValidator.cs index 7f79068..b010cc0 100644 --- a/PrintReceipt/PrintReceipt/BarcodesValidator.cs +++ b/PrintReceipt/PrintReceipt/BarcodesValidator.cs @@ -17,7 +17,28 @@ public class BarcodesValidator validationResult.Barcode = barcodeDetails[0]; break; } + if (!FindBarcodeInProducts(products, barcodeDetails[0])) + { + validationResult.Error = ValidationResultType.INVALID_PRODUCT_NOT_FIND; + validationResult.Barcode = barcodeDetails[0]; + break; + } } return validationResult; } + + private static bool FindBarcodeInProducts(IList? products, string barcode) + { + ValidationResult validationResult; + bool isFind = false; + foreach (var product in products) + { + if (product.Barcode.Equals(barcode)) + { + isFind = true; + break; + } + } + return isFind; + } } \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index ea5a36b..121ac5f 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -22,4 +22,21 @@ public class PrintReceiptTest Assert.Equal(validationResult.Error, ValidationResultType.INVALID_BARCODE); Assert.Equal(validationResult.Barcode, "ITEM000003"); } + + [Fact] + public void Should_Get_PRODUCT_NOT_FIND_ValidationResult_If_SelectedBarcodes_Dont_Contains_Given_Product() + { + // Given + string selectBarcodesJson = "[\"ITEM000002-2\"]"; + string productsJson = @"[{""name"":""phone"",""price"":2000,""barcode"":""ITEM000003""}]"; + var selectBarcodes = new SelectedBarcodes(selectBarcodesJson); + var products = JsonConvert.DeserializeObject>(productsJson); + + // when + var validationResult = BarcodesValidator.ValidateBarcodes(selectBarcodes, products); + + // then + Assert.Equal(validationResult.Error, ValidationResultType.INVALID_PRODUCT_NOT_FIND); + Assert.Equal(validationResult.Barcode, "ITEM000002"); + } } \ No newline at end of file -- Gitee From 31781dcf512c31bd2b0b4c30f7442ab2d71f5e7d Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 20:46:51 +0800 Subject: [PATCH 04/14] test: add VALID test for ValidateBarcodes --- PrintReceipt/PrintReceipt/BarcodesValidator.cs | 6 +++++- .../PrintReceiptTest/PrintReceiptTest.cs | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/PrintReceipt/PrintReceipt/BarcodesValidator.cs b/PrintReceipt/PrintReceipt/BarcodesValidator.cs index b010cc0..7f129d1 100644 --- a/PrintReceipt/PrintReceipt/BarcodesValidator.cs +++ b/PrintReceipt/PrintReceipt/BarcodesValidator.cs @@ -6,7 +6,11 @@ public class BarcodesValidator { public static ValidationResult ValidateBarcodes(SelectedBarcodes selectBarcodes, IList? products) { - var validationResult = new ValidationResult(); + var validationResult = new ValidationResult() + { + Barcode = "", + Error = ValidationResultType.VALID + }; foreach (var barcodes in selectBarcodes.SelectedBarcodeList) { var barcodeDetails = barcodes.Split("-"); diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 121ac5f..7173bf1 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -39,4 +39,20 @@ public class PrintReceiptTest Assert.Equal(validationResult.Error, ValidationResultType.INVALID_PRODUCT_NOT_FIND); Assert.Equal(validationResult.Barcode, "ITEM000002"); } + + [Fact] + public void Should_Get_VALID_ValidationResult_If_SelectedBarcodes_Is_Normal() + { + // Given + string selectBarcodesJson = "[\"ITEM000003-2\"]"; + string productsJson = @"[{""name"":""phone"",""price"":2000,""barcode"":""ITEM000003""}]"; + var selectBarcodes = new SelectedBarcodes(selectBarcodesJson); + var products = JsonConvert.DeserializeObject>(productsJson); + + // when + var validationResult = BarcodesValidator.ValidateBarcodes(selectBarcodes, products); + + // then + Assert.Equal(validationResult.Error, ValidationResultType.VALID); + } } \ No newline at end of file -- Gitee From d0f71c8de9afea0c66f67ff16ad281779e1c31c6 Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 20:51:14 +0800 Subject: [PATCH 05/14] test: add Unrecognized Receipt test for CreateErrorReceipt --- .../PrintReceipt/ErrorReceiptCreator.cs | 18 ++++++++++++++++++ .../PrintReceiptTest/PrintReceiptTest.cs | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs diff --git a/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs b/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs new file mode 100644 index 0000000..1923452 --- /dev/null +++ b/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs @@ -0,0 +1,18 @@ +using PrintReceipt.Enums; + +namespace PrintReceipt; + +public class ErrorReceiptCreator +{ + + public static string CreateErrorReceipt(ValidationResult validationResult) + { + switch (validationResult.Error) + { + case ValidationResultType.INVALID_BARCODE: + return "ERROR\n-------\nThe barcode cannot be recognized"; + } + + return null; + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 7173bf1..5f542b3 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -55,4 +55,21 @@ public class PrintReceiptTest // then Assert.Equal(validationResult.Error, ValidationResultType.VALID); } + + [Fact] + public void Should_Get_Unrecognized_Receipt_If_ValidationResult_Type_Is_INVALID_BARCODE() + { + // Given + var validationResult = new ValidationResult() + { + Error = ValidationResultType.INVALID_BARCODE, + Barcode = "" + }; + + // when + var errorReceipt = ErrorReceiptCreator.CreateErrorReceipt(validationResult); + + // then + Assert.Equal(errorReceipt, "ERROR\n-------\nThe barcode cannot be recognized"); + } } \ No newline at end of file -- Gitee From 760e079d73b574daa9311fa91c207b452132ef8a Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 21:09:40 +0800 Subject: [PATCH 06/14] test: add product not find Receipt test for CreateErrorReceipt --- .../PrintReceipt/ErrorReceiptCreator.cs | 2 ++ .../PrintReceiptTest/PrintReceiptTest.cs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs b/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs index 1923452..b514318 100644 --- a/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs +++ b/PrintReceipt/PrintReceipt/ErrorReceiptCreator.cs @@ -11,6 +11,8 @@ public class ErrorReceiptCreator { case ValidationResultType.INVALID_BARCODE: return "ERROR\n-------\nThe barcode cannot be recognized"; + case ValidationResultType.INVALID_PRODUCT_NOT_FIND: + return "ERROR\n-------\nThe product cannot be found: " + validationResult.Barcode; } return null; diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 5f542b3..005b545 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -72,4 +72,21 @@ public class PrintReceiptTest // then Assert.Equal(errorReceipt, "ERROR\n-------\nThe barcode cannot be recognized"); } + + [Fact] + public void Should_Get_PRODUCT_NOT_FIND_Receipt_If_ValidationResult_Type_Is_PRODUCT_NOT_FIND() + { + // Given + var validationResult = new ValidationResult() + { + Error = ValidationResultType.INVALID_PRODUCT_NOT_FIND, + Barcode = "ITEM000002" + }; + + // when + var errorReceipt = ErrorReceiptCreator.CreateErrorReceipt(validationResult); + + // then + Assert.Equal(errorReceipt, "ERROR\n-------\nThe product cannot be found: ITEM000002"); + } } \ No newline at end of file -- Gitee From 4b302ad054185950521660a76ea5c2bd935e0c4f Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 21:22:19 +0800 Subject: [PATCH 07/14] test: add Get BarcodesAndAmount test for ParseBarcodes --- .../PrintReceipt.sln.DotSettings.user | 7 ++++++- PrintReceipt/PrintReceipt/BarcodesParser.cs | 19 +++++++++++++++++ PrintReceipt/PrintReceipt/ProductBarcode.cs | 21 +++++++++++++++++++ .../PrintReceiptTest/PrintReceiptTest.cs | 16 ++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 PrintReceipt/PrintReceipt/BarcodesParser.cs create mode 100644 PrintReceipt/PrintReceipt/ProductBarcode.cs diff --git a/PrintReceipt/PrintReceipt.sln.DotSettings.user b/PrintReceipt/PrintReceipt.sln.DotSettings.user index e316112..45bbe6f 100644 --- a/PrintReceipt/PrintReceipt.sln.DotSettings.user +++ b/PrintReceipt/PrintReceipt.sln.DotSettings.user @@ -1,5 +1,10 @@  - <SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &lt;PrintReceiptTest&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="Should_Get_BarcodesAndAmount_If_SelectedBarcodes_Is_VALID" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::E623BEED-3353-4D15-A109-2B065A08E43C::net6.0::PrintReceiptTest.PrintReceiptTest.Should_Get_BarcodesAndAmount_If_SelectedBarcodes_Is_VALID</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="All tests from &lt;PrintReceiptTest&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <Project Location="/Users/duming.zhu/code/homework-2-test-first-development/PrintReceipt/PrintReceiptTest" Presentation="&lt;PrintReceiptTest&gt;" /> </SessionState> diff --git a/PrintReceipt/PrintReceipt/BarcodesParser.cs b/PrintReceipt/PrintReceipt/BarcodesParser.cs new file mode 100644 index 0000000..b57ec8a --- /dev/null +++ b/PrintReceipt/PrintReceipt/BarcodesParser.cs @@ -0,0 +1,19 @@ +namespace PrintReceipt; + +public class BarcodesParser +{ + public static List ParseBarcodes(SelectedBarcodes selectedBarcodes) + { + var productBarcodes = new List(); + foreach (var barcodes in selectedBarcodes.SelectedBarcodeList) + { + var barcodeDetails = barcodes.Split("-"); + var productBarcode = new ProductBarcode(); + productBarcode.Amount = int.Parse(barcodeDetails[1]); + productBarcode.Barcode = barcodeDetails[0]; + productBarcodes.Add(productBarcode); + } + + return productBarcodes; + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/ProductBarcode.cs b/PrintReceipt/PrintReceipt/ProductBarcode.cs new file mode 100644 index 0000000..528a4a2 --- /dev/null +++ b/PrintReceipt/PrintReceipt/ProductBarcode.cs @@ -0,0 +1,21 @@ +namespace PrintReceipt; + +public class ProductBarcode +{ + public string Barcode { set; get; } + public int Amount { set; get; } + + public override bool Equals(Object obj) + { + // Check for null and compare run-time types. + if ((obj == null) || !this.GetType().Equals(obj.GetType())) + { + return false; + } + + ProductBarcode productBarcode = (ProductBarcode)obj; + return productBarcode.Barcode.Equals(Barcode) && + productBarcode.Amount == Amount; + } + +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 005b545..9687c5a 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -89,4 +89,20 @@ public class PrintReceiptTest // then Assert.Equal(errorReceipt, "ERROR\n-------\nThe product cannot be found: ITEM000002"); } + + [Fact] + public void Should_Get_BarcodesAndAmount_If_SelectedBarcodes_Is_VALID() + { + // Given + string selectBarcodesJson = "[\"ITEM000003-3\"]"; + var selectedBarcodes = new SelectedBarcodes(selectBarcodesJson); + + // when + var barcodesAndAmount = BarcodesParser.ParseBarcodes(selectedBarcodes); + + // then + Assert.Equal(barcodesAndAmount.Count(), 1); + Assert.Equal( barcodesAndAmount[0].Amount, 3); + Assert.Equal(barcodesAndAmount[0].Barcode, "ITEM000003"); + } } \ No newline at end of file -- Gitee From c31c64d05c6ff2de4f1edb4b3d8e5e69ec42ccab Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 21:24:48 +0800 Subject: [PATCH 08/14] test: add Get BarcodesAndAmount test for BarcodesParser with three barcode --- PrintReceipt/PrintReceipt.sln.DotSettings.user | 4 +--- .../PrintReceiptTest/PrintReceiptTest.cs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/PrintReceipt/PrintReceipt.sln.DotSettings.user b/PrintReceipt/PrintReceipt.sln.DotSettings.user index 45bbe6f..86bf451 100644 --- a/PrintReceipt/PrintReceipt.sln.DotSettings.user +++ b/PrintReceipt/PrintReceipt.sln.DotSettings.user @@ -1,8 +1,6 @@  <SessionState ContinuousTestingMode="0" IsActive="True" Name="Should_Get_BarcodesAndAmount_If_SelectedBarcodes_Is_VALID" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> - <TestAncestor> - <TestId>xUnit::E623BEED-3353-4D15-A109-2B065A08E43C::net6.0::PrintReceiptTest.PrintReceiptTest.Should_Get_BarcodesAndAmount_If_SelectedBarcodes_Is_VALID</TestId> - </TestAncestor> + <Project Location="/Users/duming.zhu/code/homework-2-test-first-development/PrintReceipt/PrintReceiptTest" Presentation="&lt;PrintReceiptTest&gt;" /> </SessionState> <SessionState ContinuousTestingMode="0" Name="All tests from &lt;PrintReceiptTest&gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <Project Location="/Users/duming.zhu/code/homework-2-test-first-development/PrintReceipt/PrintReceiptTest" Presentation="&lt;PrintReceiptTest&gt;" /> diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 9687c5a..314a315 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -105,4 +105,20 @@ public class PrintReceiptTest Assert.Equal( barcodesAndAmount[0].Amount, 3); Assert.Equal(barcodesAndAmount[0].Barcode, "ITEM000003"); } + + [Fact] + public void Should_Get_BarcodesAndAmount_If_SelectedBarcodes_Is_VALID_With_Three_Barcodes() + { + // Given + string selectBarcodesJson = @"[""ITEM000001-1"", ""ITEM000002-2"", ""ITEM000003-3""]"; + var selectBarcodes = new SelectedBarcodes(selectBarcodesJson); + + // when + var barcodesAndAmount = BarcodesParser.ParseBarcodes(selectBarcodes); + + // then + Assert.Equal(barcodesAndAmount.Count(), 3); + Assert.Equal( barcodesAndAmount[0].Amount, 1); + Assert.Equal(barcodesAndAmount[0].Barcode, "ITEM000001"); + } } \ No newline at end of file -- Gitee From d3953713fd4cba3e973a7adad2b79efafaac0d8e Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 21:27:08 +0800 Subject: [PATCH 09/14] test: add Get GroupedBarcodes test for BarcodesParser with three different barcode --- PrintReceipt/PrintReceipt/BarcodesParser.cs | 15 +++++++++ .../PrintReceiptTest/PrintReceiptTest.cs | 32 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/PrintReceipt/PrintReceipt/BarcodesParser.cs b/PrintReceipt/PrintReceipt/BarcodesParser.cs index b57ec8a..26e5ed8 100644 --- a/PrintReceipt/PrintReceipt/BarcodesParser.cs +++ b/PrintReceipt/PrintReceipt/BarcodesParser.cs @@ -16,4 +16,19 @@ public class BarcodesParser return productBarcodes; } + + public static List GroupBarcodes(List barcodesAndAmount) + { + List groupedBarcodes = new List(); + foreach (var productBarcode in barcodesAndAmount) + { + if (groupedBarcodes.Contains(productBarcode)) + { + continue; + } + groupedBarcodes.Add(productBarcode); + } + + return groupedBarcodes; + } } \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 314a315..20e4956 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -121,4 +121,36 @@ public class PrintReceiptTest Assert.Equal( barcodesAndAmount[0].Amount, 1); Assert.Equal(barcodesAndAmount[0].Barcode, "ITEM000001"); } + + [Fact] + public void Should_Get_Three_Item_In_GroupedBarcodes_If_BarcodesAndAmount_With_Three_Different_Barcodes() + { + // Given + var barcodesAndAmount = new List() + { + new ProductBarcode() + { + Amount = 1, + Barcode = "ITEM000001", + }, + new ProductBarcode() + { + Amount = 2, + Barcode = "ITEM000002", + }, + new ProductBarcode() + { + Amount = 3, + Barcode = "ITEM000003", + } + }; + + // when + var groupedBarcodes = BarcodesParser.GroupBarcodes(barcodesAndAmount); + + // then + Assert.Equal(groupedBarcodes.Count(), 3); + Assert.Equal( groupedBarcodes[0].Amount, 1); + Assert.Equal(groupedBarcodes[0].Barcode, "ITEM000001"); + } } \ No newline at end of file -- Gitee From 34b1a32260472a93cc8371f44f3eab5565818b22 Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 21:46:18 +0800 Subject: [PATCH 10/14] test: add Get GroupedBarcodes test for BarcodesParser with three of two barcode same --- .../PrintReceiptTest/PrintReceiptTest.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 20e4956..0133869 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -153,4 +153,35 @@ public class PrintReceiptTest Assert.Equal( groupedBarcodes[0].Amount, 1); Assert.Equal(groupedBarcodes[0].Barcode, "ITEM000001"); } + + [Fact] + public void Should_Get_Two_Item_In_GroupedBarcodes_If_BarcodesAndAmount_With_Three_Barcodes_Two_Same() + { + // Given + var barcodesAndAmount = new List() + { + new ProductBarcode() + { + Amount = 1, + Barcode = "ITEM000001", + }, + new ProductBarcode() + { + Amount = 2, + Barcode = "ITEM000002", + }, + new ProductBarcode() + { + Amount = 1, + Barcode = "ITEM000001", + } + }; + // when + var groupedBarcodes = BarcodesParser.GroupBarcodes(barcodesAndAmount); + + // then + Assert.Equal(groupedBarcodes.Count(), 2); + Assert.Equal( groupedBarcodes[1].Amount, 2); + Assert.Equal(groupedBarcodes[1].Barcode, "ITEM000002"); + } } \ No newline at end of file -- Gitee From 8ff6e8128de01ba884fe846d93e11dd8286d2191 Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 21:51:42 +0800 Subject: [PATCH 11/14] test: add get Receipt test for GroupedBarcodes is normal --- PrintReceipt/PrintReceipt/Receipt.cs | 7 ++++ PrintReceipt/PrintReceipt/ReceiptCreator.cs | 31 +++++++++++++++ PrintReceipt/PrintReceipt/ReceiptProduct.cs | 9 +++++ .../PrintReceiptTest/PrintReceiptTest.cs | 38 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 PrintReceipt/PrintReceipt/Receipt.cs create mode 100644 PrintReceipt/PrintReceipt/ReceiptCreator.cs create mode 100644 PrintReceipt/PrintReceipt/ReceiptProduct.cs diff --git a/PrintReceipt/PrintReceipt/Receipt.cs b/PrintReceipt/PrintReceipt/Receipt.cs new file mode 100644 index 0000000..1a2af4f --- /dev/null +++ b/PrintReceipt/PrintReceipt/Receipt.cs @@ -0,0 +1,7 @@ +namespace PrintReceipt; + +public class Receipt +{ + public List ReceiptProducts { set; get; } = new List(); + public double TotalPrice { set; get; } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/ReceiptCreator.cs b/PrintReceipt/PrintReceipt/ReceiptCreator.cs new file mode 100644 index 0000000..ffa1af2 --- /dev/null +++ b/PrintReceipt/PrintReceipt/ReceiptCreator.cs @@ -0,0 +1,31 @@ +using System.Runtime.Intrinsics.Arm; + +namespace PrintReceipt; + +public class ReceiptCreator +{ + public static Receipt CreateReceipt(List groupedBarcodes, List products) + { + var receipt = new Receipt(); + double totalPrice = 0; + foreach (var groupedBarcode in groupedBarcodes) + { + var receiptProduct = new ReceiptProduct(); + var product = FindBarcodesProduct(groupedBarcode.Barcode, products); + receiptProduct.Amount = groupedBarcode.Amount; + receiptProduct.Name = product.Name; + receiptProduct.TotalPrice = groupedBarcode.Amount * product.Price; + receiptProduct.Price = product.Price; + totalPrice = totalPrice + receiptProduct.TotalPrice; + receipt.ReceiptProducts.Add(receiptProduct); + } + + receipt.TotalPrice = totalPrice; + return receipt; + } + + public static Product FindBarcodesProduct(string barcode, List products) + { + return products.FirstOrDefault(product => barcode.Equals(product.Barcode)); + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceipt/ReceiptProduct.cs b/PrintReceipt/PrintReceipt/ReceiptProduct.cs new file mode 100644 index 0000000..91c08c4 --- /dev/null +++ b/PrintReceipt/PrintReceipt/ReceiptProduct.cs @@ -0,0 +1,9 @@ +namespace PrintReceipt; + +public class ReceiptProduct +{ + public string Name { set; get; } + public double Price { set; get; } + public double TotalPrice { set; get; } + public int Amount { set; get; } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 0133869..b55beb5 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -184,4 +184,42 @@ public class PrintReceiptTest Assert.Equal( groupedBarcodes[1].Amount, 2); Assert.Equal(groupedBarcodes[1].Barcode, "ITEM000002"); } + [Fact] + public void Should_Get_Receipt_If_GroupedBarcodes_Is_Normal() + { + // Given + string productsJson = @"[ {""name"": ""phone"",""price"": 2000,""barcode"": ""ITEM000003"" }, + {""name"": ""pen"",""price"": 20,""barcode"": ""ITEM000002"" }, + {""name"": ""table"",""price"": 200,""barcode"": ""ITEM000001"" }]"; + var products = JsonConvert.DeserializeObject>(productsJson); + + var groupedBarcodes = new List() + { + new ProductBarcode() + { + Amount = 1, + Barcode = "ITEM000001", + }, + new ProductBarcode() + { + Amount = 2, + Barcode = "ITEM000002", + }, + new ProductBarcode() + { + Amount = 3, + Barcode = "ITEM000003", + } + }; + + // when + var receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + + //then + Assert.Equal(receipt.ReceiptProducts.Count(), 3); + Assert.Equal(receipt.ReceiptProducts[0].TotalPrice, 200); + Assert.Equal(receipt.ReceiptProducts[1].TotalPrice, 40); + Assert.Equal(receipt.ReceiptProducts[2].TotalPrice, 6000); + Assert.Equal(receipt.TotalPrice, 6240); + } } \ No newline at end of file -- Gitee From 6ff39ad5c0ef1c423e5ae0fb4fb2f5a97b9c6aad Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 22:01:52 +0800 Subject: [PATCH 12/14] test:add empty receipt test for FormatReceipt --- PrintReceipt/PrintReceipt/ReceiptFormatter.cs | 15 +++++++++++++++ PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs | 13 +++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 PrintReceipt/PrintReceipt/ReceiptFormatter.cs diff --git a/PrintReceipt/PrintReceipt/ReceiptFormatter.cs b/PrintReceipt/PrintReceipt/ReceiptFormatter.cs new file mode 100644 index 0000000..ccdfb6b --- /dev/null +++ b/PrintReceipt/PrintReceipt/ReceiptFormatter.cs @@ -0,0 +1,15 @@ +namespace PrintReceipt; + +public class ReceiptFormatter +{ + public static string FormatReceipt(Receipt emptyReceipt) + { + string receiptContent = ""; + if (emptyReceipt == null || emptyReceipt.ReceiptProducts.Count() == 0) + { + receiptContent = "Receipt\n------\n------\nTotal: 0.00"; + } + + return receiptContent; + } +} \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index b55beb5..407ea35 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -222,4 +222,17 @@ public class PrintReceiptTest Assert.Equal(receipt.ReceiptProducts[2].TotalPrice, 6000); Assert.Equal(receipt.TotalPrice, 6240); } + + [Fact] + void should_get_empty_receipt_content_if_receipt_contains_no_products() + { + // Given + var emptyReceipt = new Receipt(); + + // When + string receiptContent = ReceiptFormatter.FormatReceipt(emptyReceipt); + + // Then + Assert.Equal("Receipt\n------\n------\nTotal: 0.00", receiptContent); + } } \ No newline at end of file -- Gitee From 40102888b0bf45bf98096aac9be2f9384059de5f Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 22:29:47 +0800 Subject: [PATCH 13/14] test:add get normal receipt content test --- PrintReceipt/PrintReceipt/ReceiptFormatter.cs | 22 ++++++--- .../PrintReceiptTest/PrintReceiptTest.cs | 45 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/PrintReceipt/PrintReceipt/ReceiptFormatter.cs b/PrintReceipt/PrintReceipt/ReceiptFormatter.cs index ccdfb6b..da45da0 100644 --- a/PrintReceipt/PrintReceipt/ReceiptFormatter.cs +++ b/PrintReceipt/PrintReceipt/ReceiptFormatter.cs @@ -2,14 +2,26 @@ namespace PrintReceipt; public class ReceiptFormatter { - public static string FormatReceipt(Receipt emptyReceipt) + public static string FormatReceipt(Receipt receipt) { - string receiptContent = ""; - if (emptyReceipt == null || emptyReceipt.ReceiptProducts.Count() == 0) + var receiptContent = ""; + if (receipt == null || receipt.ReceiptProducts.Count() == 0) { receiptContent = "Receipt\n------\n------\nTotal: 0.00"; + return receiptContent; + } + else + { + receiptContent = "Receipt\n-------\n"; + foreach (var receiptProduct in receipt.ReceiptProducts) + { + receiptContent += "Name: " + receiptProduct.Name + ", "; + receiptContent += "Amount: " + receiptProduct.Amount + ", "; + receiptContent += "Price: " + receiptProduct.Price.ToString("f2") + ", "; + receiptContent += "Total: " + receiptProduct.TotalPrice.ToString("f2")+"\n"; + } + receiptContent += "-------\nTotal: " + receipt.TotalPrice.ToString("f2"); + return receiptContent; } - - return receiptContent; } } \ No newline at end of file diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 407ea35..164c4e9 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -235,4 +235,49 @@ public class PrintReceiptTest // Then Assert.Equal("Receipt\n------\n------\nTotal: 0.00", receiptContent); } + [Fact] + void should_get_normal_receipt_content_if_receipt_contains_products() + { + // Given + var receipt = new Receipt() + { + ReceiptProducts = new List() + { + new ReceiptProduct() + { + Amount = 1, + Price = 200.00, + Name = "table", + TotalPrice = 200.00 + }, + new ReceiptProduct() + { + Amount = 2, + Price = 20.00, + Name = "pen", + TotalPrice = 40.00 + }, + new ReceiptProduct() + { + Amount = 3, + Price = 2000.00, + Name = "phone", + TotalPrice = 6000.00 + } + }, + TotalPrice = 6240.00 + }; + // When + string receiptContent = ReceiptFormatter.FormatReceipt(receipt); + + // Then + Assert.Equal(@"Receipt +------- +Name: table, Amount: 1, Price: 200.00, Total: 200.00 +Name: pen, Amount: 2, Price: 20.00, Total: 40.00 +Name: phone, Amount: 3, Price: 2000.00, Total: 6000.00 +------- +Total: 6240.00", + receiptContent); + } } \ No newline at end of file -- Gitee From c3dbe09774d26cc47dd713501c34aabf50c171c4 Mon Sep 17 00:00:00 2001 From: duming Date: Tue, 15 Nov 2022 22:43:10 +0800 Subject: [PATCH 14/14] test:add get receiptcontent test if selectedBarcodes is normal --- .../PrintReceiptTest/PrintReceiptTest.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs index 164c4e9..c124f12 100644 --- a/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs +++ b/PrintReceipt/PrintReceiptTest/PrintReceiptTest.cs @@ -280,4 +280,36 @@ Name: phone, Amount: 3, Price: 2000.00, Total: 6000.00 Total: 6240.00", receiptContent); } + + [Fact] + public void Should_Get_Normal_ReceiptContent_If_SelectedBarcodes_Is_Normal() { + + // Given + string selectBarcodesJson = @"[""ITEM000001-1"", ""ITEM000002-2"", ""ITEM000003-3""]"; + string productsJson = @"[ {""name"": ""phone"",""price"": 2000,""barcode"": ""ITEM000003"" }, + {""name"": ""pen"",""price"": 20,""barcode"": ""ITEM000002"" }, + {""name"": ""table"",""price"": 200,""barcode"": ""ITEM000001"" }]"; + + var selectedBarcodes = new SelectedBarcodes(selectBarcodesJson); + var products = JsonConvert.DeserializeObject>(productsJson); + + + // when + var validationResult = BarcodesValidator.ValidateBarcodes(selectedBarcodes, products); + var barcodesAndAmount = BarcodesParser.ParseBarcodes(selectedBarcodes); + var groupedBarcodes = BarcodesParser.GroupBarcodes(barcodesAndAmount); + var receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + string receiptContent = ReceiptFormatter.FormatReceipt(receipt); + + // then + Assert.Equal(@"Receipt +------- +Name: table, Amount: 1, Price: 200.00, Total: 200.00 +Name: pen, Amount: 2, Price: 20.00, Total: 40.00 +Name: phone, Amount: 3, Price: 2000.00, Total: 6000.00 +------- +Total: 6240.00", + receiptContent); + + } } \ No newline at end of file -- Gitee