From bf99c8fe20f279427960f7f5695bc0d40514a0c8 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Thu, 17 Nov 2022 23:13:43 +0800 Subject: [PATCH 01/19] [Chenyu Zhao]: initial homework-2-test-first-development in golang --- .gitignore | 4 ++++ go.mod | 11 +++++++++++ go.sum | 17 +++++++++++++++++ main.go | 7 +++++++ 4 files changed, 39 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..410fd7f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +vendor + +main diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..f6a14de --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/homework-2-test-first-development + +go 1.18 + +require github.com/stretchr/testify v1.8.1 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2ec90f7 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..c048119 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("hello world") +} -- Gitee From 72c352ac81e516b4c2750e5f3705589828b73b58 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Thu, 17 Nov 2022 23:17:27 +0800 Subject: [PATCH 02/19] [Chenyu Zhao]: test of FormatReceipt func with no products --- receipt/receipt.go | 8 ++++++++ receipt/receipt_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 receipt/receipt.go create mode 100644 receipt/receipt_test.go diff --git a/receipt/receipt.go b/receipt/receipt.go new file mode 100644 index 0000000..5ca4619 --- /dev/null +++ b/receipt/receipt.go @@ -0,0 +1,8 @@ +package receipt + +type Receipt struct { +} + +func (r *Receipt) FormatReceipt(receipt *Receipt) string { + return "Receipt\n------\n------\nTotal: 0.00" +} diff --git a/receipt/receipt_test.go b/receipt/receipt_test.go new file mode 100644 index 0000000..b197dfa --- /dev/null +++ b/receipt/receipt_test.go @@ -0,0 +1,16 @@ +package receipt + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFormatReceiptWithNoProducts(t *testing.T) { + // Given + receipt := &Receipt{} + // When + receiptContent := receipt.FormatReceipt(receipt) + // Then + assert.Equal(t, "Receipt\n------\n------\nTotal: 0.00", receiptContent) +} -- Gitee From d232a62728846b8c641cbe4d55e5ba6dc75932c6 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 09:50:37 +0800 Subject: [PATCH 03/19] [Chenyu Zhao]: test of ValidateBarcodes func with invalid barcodes --- barcode/barcode.go | 24 ++++++++++++++++++++++++ barcode/barcode_test.go | 24 ++++++++++++++++++++++++ errorcode/error_code.go | 21 +++++++++++++++++++++ product/product.go | 15 +++++++++++++++ product/product_test.go | 1 + validationresult/validation_result.go | 17 +++++++++++++++++ 6 files changed, 102 insertions(+) create mode 100644 barcode/barcode.go create mode 100644 barcode/barcode_test.go create mode 100644 errorcode/error_code.go create mode 100644 product/product.go create mode 100644 product/product_test.go create mode 100644 validationresult/validation_result.go diff --git a/barcode/barcode.go b/barcode/barcode.go new file mode 100644 index 0000000..306ce24 --- /dev/null +++ b/barcode/barcode.go @@ -0,0 +1,24 @@ +package barcode + +import ( + "github.com/homework-2-test-first-development/errorcode" + "github.com/homework-2-test-first-development/product" + "github.com/homework-2-test-first-development/validationresult" +) + +type BarcodeInfo struct { + Barcode string + Amount int32 +} + +func NewBarcode(barcode string, amount int32) *BarcodeInfo { + return &BarcodeInfo{ + Barcode: barcode, + Amount: amount, + } +} + +func ValidateBarcodes(selectedBarcodes []*BarcodeInfo, products []*product.Product) *validationresult.ValidationResult { + err := errorcode.NewErr("", errorcode.InvalidBarcode) + return validationresult.NewValidationResult("", err) +} diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go new file mode 100644 index 0000000..8421256 --- /dev/null +++ b/barcode/barcode_test.go @@ -0,0 +1,24 @@ +package barcode + +import ( + "testing" + + "github.com/homework-2-test-first-development/errorcode" + "github.com/homework-2-test-first-development/product" + "github.com/stretchr/testify/assert" +) + +func TestValidateBarcodesIfBarcodeisInvalid(t *testing.T) { + // Given + selectedBarcodes := make([]*BarcodeInfo, 0) + selectedBarcodes = append(selectedBarcodes, NewBarcode("123456", 1)) + + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + + // When + result := ValidateBarcodes(selectedBarcodes, products) + + // Then + assert.Equal(t, errorcode.InvalidBarcode, result.ErrorMes.Code) +} diff --git a/errorcode/error_code.go b/errorcode/error_code.go new file mode 100644 index 0000000..b1c2bae --- /dev/null +++ b/errorcode/error_code.go @@ -0,0 +1,21 @@ +package errorcode + +import "fmt" + +const ( + Valid = 0 + InvalidBarcode = 1 + ProductNotFound = 2 +) + +type Err struct { + error + Code int +} + +func NewErr(msg string, code int) Err { + return Err{ + error: fmt.Errorf(msg), + Code: code, + } +} diff --git a/product/product.go b/product/product.go new file mode 100644 index 0000000..041b603 --- /dev/null +++ b/product/product.go @@ -0,0 +1,15 @@ +package product + +type Product struct { + Name string + Price float32 + Barcode string +} + +func NewProduct(name string, price float32, barcode string) *Product { + return &Product{ + Name: name, + Price: price, + Barcode: barcode, + } +} diff --git a/product/product_test.go b/product/product_test.go new file mode 100644 index 0000000..e6ae091 --- /dev/null +++ b/product/product_test.go @@ -0,0 +1 @@ +package product diff --git a/validationresult/validation_result.go b/validationresult/validation_result.go new file mode 100644 index 0000000..3495d2c --- /dev/null +++ b/validationresult/validation_result.go @@ -0,0 +1,17 @@ +package validationresult + +import ( + "github.com/homework-2-test-first-development/errorcode" +) + +type ValidationResult struct { + Barcode string + ErrorMes errorcode.Err +} + +func NewValidationResult(barcode string, ErrorMes errorcode.Err) *ValidationResult { + return &ValidationResult{ + Barcode: barcode, + ErrorMes: errorcode.NewErr("", errorcode.InvalidBarcode), + } +} -- Gitee From 5300af100104d949413672ea140686b434d00146 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 10:26:47 +0800 Subject: [PATCH 04/19] [Chenyu Zhao]: test of ValidateBarcodes func with valid barcodes --- barcode/barcode.go | 26 ++++++++++++++++++++++++++ barcode/barcode_test.go | 17 +++++++++++++++++ validationresult/validation_result.go | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index 306ce24..006ae2c 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -4,6 +4,8 @@ import ( "github.com/homework-2-test-first-development/errorcode" "github.com/homework-2-test-first-development/product" "github.com/homework-2-test-first-development/validationresult" + "strings" + "unicode" ) type BarcodeInfo struct { @@ -20,5 +22,29 @@ func NewBarcode(barcode string, amount int32) *BarcodeInfo { func ValidateBarcodes(selectedBarcodes []*BarcodeInfo, products []*product.Product) *validationresult.ValidationResult { err := errorcode.NewErr("", errorcode.InvalidBarcode) + if selectedBarcodes == nil { + return validationresult.NewValidationResult("", err) + } + + for _, v := range selectedBarcodes { + if !strings.HasPrefix(v.Barcode, "ITEM") { + return validationresult.NewValidationResult("", err) + } + + if !IsAllNumber(v.Barcode[4:]) { + return validationresult.NewValidationResult("", err) + } + } + + err.Code = errorcode.Valid return validationresult.NewValidationResult("", err) } + +func IsAllNumber(str string) bool { + for _, v := range str { + if unicode.IsNumber(v) == false { + return false + } + } + return true +} diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 8421256..3cdd98a 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -22,3 +22,20 @@ func TestValidateBarcodesIfBarcodeisInvalid(t *testing.T) { // Then assert.Equal(t, errorcode.InvalidBarcode, result.ErrorMes.Code) } + +func TestValidateBarcodesIfBarcodeIsValid(t *testing.T) { + // Given + selectedBarcodes := make([]*BarcodeInfo, 0) + selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000001", 2)) + selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000002", 2)) + + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + products = append(products, product.NewProduct("water", 1.00, "ITEM000002")) + + // When + result := ValidateBarcodes(selectedBarcodes, products) + + // Then + assert.Equal(t, errorcode.Valid, result.ErrorMes.Code) +} diff --git a/validationresult/validation_result.go b/validationresult/validation_result.go index 3495d2c..1726b52 100644 --- a/validationresult/validation_result.go +++ b/validationresult/validation_result.go @@ -12,6 +12,6 @@ type ValidationResult struct { func NewValidationResult(barcode string, ErrorMes errorcode.Err) *ValidationResult { return &ValidationResult{ Barcode: barcode, - ErrorMes: errorcode.NewErr("", errorcode.InvalidBarcode), + ErrorMes: ErrorMes, } } -- Gitee From a09a53590f8fd7b54bdeb9dea36cfb55a768aaa7 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 10:42:08 +0800 Subject: [PATCH 05/19] [Chenyu Zhao]: test of ValidateBarcodes func with barcode amount is invalid --- barcode/barcode.go | 13 +++++++++---- barcode/barcode_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index 006ae2c..1515df5 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -1,19 +1,20 @@ package barcode import ( + "strings" + "unicode" + "github.com/homework-2-test-first-development/errorcode" "github.com/homework-2-test-first-development/product" "github.com/homework-2-test-first-development/validationresult" - "strings" - "unicode" ) type BarcodeInfo struct { Barcode string - Amount int32 + Amount uint32 } -func NewBarcode(barcode string, amount int32) *BarcodeInfo { +func NewBarcode(barcode string, amount uint32) *BarcodeInfo { return &BarcodeInfo{ Barcode: barcode, Amount: amount, @@ -34,6 +35,10 @@ func ValidateBarcodes(selectedBarcodes []*BarcodeInfo, products []*product.Produ if !IsAllNumber(v.Barcode[4:]) { return validationresult.NewValidationResult("", err) } + + if v.Amount <= 0 { + return validationresult.NewValidationResult("", err) + } } err.Code = errorcode.Valid diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 3cdd98a..f66c471 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -39,3 +39,19 @@ func TestValidateBarcodesIfBarcodeIsValid(t *testing.T) { // Then assert.Equal(t, errorcode.Valid, result.ErrorMes.Code) } + +func TestValidateBarcodesIfBarcodeAmountIsInvalid(t *testing.T) { + // Given + selectedBarcodes := make([]*BarcodeInfo, 0) + selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000001", 0)) + + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + products = append(products, product.NewProduct("water", 1.00, "ITEM000002")) + + // When + result := ValidateBarcodes(selectedBarcodes, products) + + // Then + assert.Equal(t, errorcode.InvalidBarcode, result.ErrorMes.Code) +} -- Gitee From 9bdbde9a3e6f7cb31e0b557a38a40c29c160c404 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 10:50:53 +0800 Subject: [PATCH 06/19] [Chenyu Zhao]: test of ValidateBarcodes func when barcode is not in the product list --- barcode/barcode.go | 16 +++++++++++++++- barcode/barcode_test.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index 1515df5..2c7ff3d 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -32,13 +32,18 @@ func ValidateBarcodes(selectedBarcodes []*BarcodeInfo, products []*product.Produ return validationresult.NewValidationResult("", err) } - if !IsAllNumber(v.Barcode[4:]) { + if !IsAllNumber(v.Barcode[4:]) { // format: ITEM000008 position 4 return validationresult.NewValidationResult("", err) } if v.Amount <= 0 { return validationresult.NewValidationResult("", err) } + + if !IsBarcodeInProductList(v.Barcode, products) { + err.Code = errorcode.ProductNotFound + return validationresult.NewValidationResult("", err) + } } err.Code = errorcode.Valid @@ -53,3 +58,12 @@ func IsAllNumber(str string) bool { } return true } + +func IsBarcodeInProductList(barcode string, products []*product.Product) bool { + for _, v := range products { + if barcode == v.Barcode { + return true + } + } + return false +} diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index f66c471..459aa33 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -55,3 +55,19 @@ func TestValidateBarcodesIfBarcodeAmountIsInvalid(t *testing.T) { // Then assert.Equal(t, errorcode.InvalidBarcode, result.ErrorMes.Code) } +func TestValidateBarcodesIfProductNotFound(t *testing.T) { + // Given + selectedBarcodes := make([]*BarcodeInfo, 0) + selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000008", 2)) + selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000009", 3)) + + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + products = append(products, product.NewProduct("water", 1.00, "ITEM000002")) + + // When + result := ValidateBarcodes(selectedBarcodes, products) + + // Then + assert.Equal(t, errorcode.ProductNotFound, result.ErrorMes.Code) +} -- Gitee From 26333e7be08e93af94cc634af5ec9205721f4db8 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 11:14:19 +0800 Subject: [PATCH 07/19] [Chenyu Zhao]: test of CreateErrorReceipt func when the error type is InvalidBarcode --- receipt/receipt.go | 13 +++++++++++++ receipt/receipt_test.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/receipt/receipt.go b/receipt/receipt.go index 5ca4619..b27b675 100644 --- a/receipt/receipt.go +++ b/receipt/receipt.go @@ -1,8 +1,21 @@ package receipt +import ( + "github.com/homework-2-test-first-development/errorcode" + "github.com/homework-2-test-first-development/validationresult" +) + type Receipt struct { } +func (r *Receipt) CreateErrorReceipt(validationResult validationresult.ValidationResult) string { + if validationResult.ErrorMes.Code == errorcode.InvalidBarcode { + return "ERROR\n-------\nThe barcode cannot be recognized" + } + + return "" +} + func (r *Receipt) FormatReceipt(receipt *Receipt) string { return "Receipt\n------\n------\nTotal: 0.00" } diff --git a/receipt/receipt_test.go b/receipt/receipt_test.go index b197dfa..0c8a61a 100644 --- a/receipt/receipt_test.go +++ b/receipt/receipt_test.go @@ -3,9 +3,23 @@ package receipt import ( "testing" + "github.com/homework-2-test-first-development/errorcode" + "github.com/homework-2-test-first-development/validationresult" "github.com/stretchr/testify/assert" ) +func TestCreateErrorReceiptWhenErrorTypeIsInvalidBarcode(t *testing.T) { + // Given + var validationResult validationresult.ValidationResult + validationResult.Barcode = "123456" + validationResult.ErrorMes = errorcode.NewErr("", errorcode.InvalidBarcode) + // When + receipt := Receipt{} + receiptContent := receipt.CreateErrorReceipt(validationResult) + // Then + assert.Equal(t, "ERROR\n-------\nThe barcode cannot be recognized", receiptContent) +} + func TestFormatReceiptWithNoProducts(t *testing.T) { // Given receipt := &Receipt{} -- Gitee From 35215fd75e2461dea01b3f41c4bf5ea88eb0d4d6 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 11:19:06 +0800 Subject: [PATCH 08/19] [Chenyu Zhao]: test of CreateErrorReceipt func when the error type is ProductNotFound --- receipt/receipt.go | 5 +++++ receipt/receipt_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/receipt/receipt.go b/receipt/receipt.go index b27b675..487a042 100644 --- a/receipt/receipt.go +++ b/receipt/receipt.go @@ -1,6 +1,7 @@ package receipt import ( + "fmt" "github.com/homework-2-test-first-development/errorcode" "github.com/homework-2-test-first-development/validationresult" ) @@ -13,6 +14,10 @@ func (r *Receipt) CreateErrorReceipt(validationResult validationresult.Validatio return "ERROR\n-------\nThe barcode cannot be recognized" } + if validationResult.ErrorMes.Code == errorcode.ProductNotFound { + return fmt.Sprintf("ERROR\\n-------\\nThe product cannot be found: %s", validationResult.Barcode) + } + return "" } diff --git a/receipt/receipt_test.go b/receipt/receipt_test.go index 0c8a61a..61932a6 100644 --- a/receipt/receipt_test.go +++ b/receipt/receipt_test.go @@ -20,6 +20,18 @@ func TestCreateErrorReceiptWhenErrorTypeIsInvalidBarcode(t *testing.T) { assert.Equal(t, "ERROR\n-------\nThe barcode cannot be recognized", receiptContent) } +func TestCreateErrorReceiptWhenErrorTypeIsProductNotFound(t *testing.T) { + // Given + var validationResult validationresult.ValidationResult + validationResult.Barcode = "ITEM000008" + validationResult.ErrorMes = errorcode.NewErr("", errorcode.ProductNotFound) + // When + receipt := Receipt{} + receiptContent := receipt.CreateErrorReceipt(validationResult) + // Then + assert.Equal(t, "ERROR\\n-------\\nThe product cannot be found: ITEM000008", receiptContent) +} + func TestFormatReceiptWithNoProducts(t *testing.T) { // Given receipt := &Receipt{} -- Gitee From e7822066fe018d3cad96cdb51115751c2836510d Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 11:41:25 +0800 Subject: [PATCH 09/19] [Chenyu Zhao]: fix: fix selectedBarcode to []string --- barcode/barcode.go | 19 ++++++++++++++----- barcode/barcode_test.go | 21 +++++++++++---------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index 2c7ff3d..f945150 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -1,6 +1,7 @@ package barcode import ( + "strconv" "strings" "unicode" @@ -21,26 +22,34 @@ func NewBarcode(barcode string, amount uint32) *BarcodeInfo { } } -func ValidateBarcodes(selectedBarcodes []*BarcodeInfo, products []*product.Product) *validationresult.ValidationResult { +func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *validationresult.ValidationResult { err := errorcode.NewErr("", errorcode.InvalidBarcode) if selectedBarcodes == nil { return validationresult.NewValidationResult("", err) } for _, v := range selectedBarcodes { - if !strings.HasPrefix(v.Barcode, "ITEM") { + barcodeString := v[0:4] + if !strings.HasPrefix(barcodeString, "ITEM") { return validationresult.NewValidationResult("", err) } - if !IsAllNumber(v.Barcode[4:]) { // format: ITEM000008 position 4 + barcodeNum := v[4:10] + if !IsAllNumber(barcodeNum) { // format: ITEM000008 position 4 return validationresult.NewValidationResult("", err) } - if v.Amount <= 0 { + amount, amountErr := strconv.Atoi(v[11:]) + if amountErr != nil { return validationresult.NewValidationResult("", err) } - if !IsBarcodeInProductList(v.Barcode, products) { + if amount <= 0 { + return validationresult.NewValidationResult("", err) + } + + barcode := v[0:10] + if !IsBarcodeInProductList(barcode, products) { err.Code = errorcode.ProductNotFound return validationresult.NewValidationResult("", err) } diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 459aa33..7b6d08e 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -10,8 +10,8 @@ import ( func TestValidateBarcodesIfBarcodeisInvalid(t *testing.T) { // Given - selectedBarcodes := make([]*BarcodeInfo, 0) - selectedBarcodes = append(selectedBarcodes, NewBarcode("123456", 1)) + selectedBarcodes := make([]string, 0) + selectedBarcodes = append(selectedBarcodes, "123456-100") products := make([]*product.Product, 0) products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) @@ -25,9 +25,9 @@ func TestValidateBarcodesIfBarcodeisInvalid(t *testing.T) { func TestValidateBarcodesIfBarcodeIsValid(t *testing.T) { // Given - selectedBarcodes := make([]*BarcodeInfo, 0) - selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000001", 2)) - selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000002", 2)) + selectedBarcodes := make([]string, 0) + selectedBarcodes = append(selectedBarcodes, "ITEM000001-2") + selectedBarcodes = append(selectedBarcodes, "ITEM000002-2") products := make([]*product.Product, 0) products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) @@ -42,8 +42,8 @@ func TestValidateBarcodesIfBarcodeIsValid(t *testing.T) { func TestValidateBarcodesIfBarcodeAmountIsInvalid(t *testing.T) { // Given - selectedBarcodes := make([]*BarcodeInfo, 0) - selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000001", 0)) + selectedBarcodes := make([]string, 0) + selectedBarcodes = append(selectedBarcodes, "ITEM000001-0") products := make([]*product.Product, 0) products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) @@ -55,11 +55,12 @@ func TestValidateBarcodesIfBarcodeAmountIsInvalid(t *testing.T) { // Then assert.Equal(t, errorcode.InvalidBarcode, result.ErrorMes.Code) } + func TestValidateBarcodesIfProductNotFound(t *testing.T) { // Given - selectedBarcodes := make([]*BarcodeInfo, 0) - selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000008", 2)) - selectedBarcodes = append(selectedBarcodes, NewBarcode("ITEM000009", 3)) + selectedBarcodes := make([]string, 0) + selectedBarcodes = append(selectedBarcodes, "ITEM000008-2") + selectedBarcodes = append(selectedBarcodes, "ITEM000009-3") products := make([]*product.Product, 0) products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) -- Gitee From 3b78f86abedd5003ddcb61ddfac42c13ffeef374 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 12:47:22 +0800 Subject: [PATCH 10/19] [Chenyu Zhao]: fix: fix string split in ValidateBarcodes func --- barcode/barcode.go | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index f945150..82893a6 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -29,17 +29,22 @@ func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *v } for _, v := range selectedBarcodes { - barcodeString := v[0:4] + if !IsOneSplitCharacter(v) { + return validationresult.NewValidationResult("", err) + } + + barcode := strings.Split(v, "-") + barcodeString := barcode[0] if !strings.HasPrefix(barcodeString, "ITEM") { return validationresult.NewValidationResult("", err) } - barcodeNum := v[4:10] + barcodeNum := barcodeString[4:] if !IsAllNumber(barcodeNum) { // format: ITEM000008 position 4 return validationresult.NewValidationResult("", err) } - amount, amountErr := strconv.Atoi(v[11:]) + amount, amountErr := strconv.Atoi(barcode[1]) if amountErr != nil { return validationresult.NewValidationResult("", err) } @@ -48,8 +53,7 @@ func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *v return validationresult.NewValidationResult("", err) } - barcode := v[0:10] - if !IsBarcodeInProductList(barcode, products) { + if !IsBarcodeInProductList(barcodeString, products) { err.Code = errorcode.ProductNotFound return validationresult.NewValidationResult("", err) } @@ -59,6 +63,21 @@ func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *v return validationresult.NewValidationResult("", err) } +func IsOneSplitCharacter(str string) bool { + chrNum := 0 + for _, v := range str { + if v == '-' { + chrNum++ + } + } + + if chrNum == 1 { + return true + } + + return false +} + func IsAllNumber(str string) bool { for _, v := range str { if unicode.IsNumber(v) == false { -- Gitee From ab6402e0ae1bdd2928bfe94133e89c3043f29b53 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 12:53:01 +0800 Subject: [PATCH 11/19] [Chenyu Zhao]: feat: add test of ParseBarcode func with valid selectedBarcodes --- barcode/barcode.go | 13 +++++++++++++ barcode/barcode_test.go | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/barcode/barcode.go b/barcode/barcode.go index 82893a6..ab19049 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -95,3 +95,16 @@ func IsBarcodeInProductList(barcode string, products []*product.Product) bool { } return false } + +func ParseBarcode(selectedBarcodes []string) []*BarcodeInfo { + barcodesAndAmount := make([]*BarcodeInfo, 0) + + for _, v := range selectedBarcodes { + barcode := strings.Split(v, "-") + barcodeString := barcode[0] + amount, _ := strconv.Atoi(barcode[1]) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode(barcodeString, uint32(amount))) + } + + return barcodesAndAmount +} diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 7b6d08e..dd4ccb3 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -72,3 +72,20 @@ func TestValidateBarcodesIfProductNotFound(t *testing.T) { // Then assert.Equal(t, errorcode.ProductNotFound, result.ErrorMes.Code) } + +func TestParseBarcode(t *testing.T) { + // Given + selectedBarcodes := make([]string, 0) + selectedBarcodes = append(selectedBarcodes, "ITEM000001-2") + selectedBarcodes = append(selectedBarcodes, "ITEM000002-3") + + barcodesAndAmount := make([]*BarcodeInfo, 0) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000001", 2)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000002", 3)) + + // When + result := ParseBarcode(selectedBarcodes) + + // Then + assert.Equal(t, barcodesAndAmount, result) +} -- Gitee From ca2e723384ff2eccb8e5ae36b40c4182e5a4d092 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 12:58:56 +0800 Subject: [PATCH 12/19] [Chenyu Zhao]: fix: fix ParseBarcode function name to TestParseBarcodeWithValidSelectedBarcodes --- barcode/barcode_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index dd4ccb3..14ef713 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -73,7 +73,7 @@ func TestValidateBarcodesIfProductNotFound(t *testing.T) { assert.Equal(t, errorcode.ProductNotFound, result.ErrorMes.Code) } -func TestParseBarcode(t *testing.T) { +func TestParseBarcodeWithValidSelectedBarcodes(t *testing.T) { // Given selectedBarcodes := make([]string, 0) selectedBarcodes = append(selectedBarcodes, "ITEM000001-2") -- Gitee From 209e97d3e9802e565d38b7aa13af24ce8fcd017c Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 13:18:44 +0800 Subject: [PATCH 13/19] [Chenyu Zhao]: feat: add test of GroupBarcode with empty barcodesAndAmount --- barcode/barcode.go | 12 +++++++++++- barcode/barcode_test.go | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index ab19049..f7057d8 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -93,12 +93,13 @@ func IsBarcodeInProductList(barcode string, products []*product.Product) bool { return true } } + return false } func ParseBarcode(selectedBarcodes []string) []*BarcodeInfo { barcodesAndAmount := make([]*BarcodeInfo, 0) - + for _, v := range selectedBarcodes { barcode := strings.Split(v, "-") barcodeString := barcode[0] @@ -108,3 +109,12 @@ func ParseBarcode(selectedBarcodes []string) []*BarcodeInfo { return barcodesAndAmount } + +func GroupBarcode(barcodeInfo []*BarcodeInfo) []*BarcodeInfo { + groupedBarcodes := make([]*BarcodeInfo, 0) + if barcodeInfo == nil || len(barcodeInfo) == 0 { + return groupedBarcodes + } + + return groupedBarcodes +} diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 14ef713..71297bd 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -89,3 +89,15 @@ func TestParseBarcodeWithValidSelectedBarcodes(t *testing.T) { // Then assert.Equal(t, barcodesAndAmount, result) } + +func TestGroupBarcodeWithEmptyBarcodeAndAmount(t *testing.T) { + // Given + barcodesAndAmount := make([]*BarcodeInfo, 0) + groupedBarcodes := make([]*BarcodeInfo, 0) + + // When + result := GroupBarcode(barcodesAndAmount) + + // Then + assert.Equal(t, groupedBarcodes, result) +} -- Gitee From a04d8aea2e81a178ab3c95b9295c39ab0c498977 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 13:44:10 +0800 Subject: [PATCH 14/19] [Chenyu Zhao]: feat: add test of ValidateBarcodes func with just one product --- barcode/barcode.go | 33 ++++++++++----------------------- barcode/barcode_test.go | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index f7057d8..e8144ea 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -29,11 +29,11 @@ func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *v } for _, v := range selectedBarcodes { - if !IsOneSplitCharacter(v) { + barcode := strings.Split(v, "-") + if barcode == nil || len(barcode) > 2 { return validationresult.NewValidationResult("", err) } - barcode := strings.Split(v, "-") barcodeString := barcode[0] if !strings.HasPrefix(barcodeString, "ITEM") { return validationresult.NewValidationResult("", err) @@ -44,13 +44,15 @@ func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *v return validationresult.NewValidationResult("", err) } - amount, amountErr := strconv.Atoi(barcode[1]) - if amountErr != nil { - return validationresult.NewValidationResult("", err) - } + if len(barcode) == 2 { + amount, amountErr := strconv.Atoi(barcode[1]) + if amountErr != nil { + return validationresult.NewValidationResult("", err) + } - if amount <= 0 { - return validationresult.NewValidationResult("", err) + if amount <= 0 { + return validationresult.NewValidationResult("", err) + } } if !IsBarcodeInProductList(barcodeString, products) { @@ -63,21 +65,6 @@ func ValidateBarcodes(selectedBarcodes []string, products []*product.Product) *v return validationresult.NewValidationResult("", err) } -func IsOneSplitCharacter(str string) bool { - chrNum := 0 - for _, v := range str { - if v == '-' { - chrNum++ - } - } - - if chrNum == 1 { - return true - } - - return false -} - func IsAllNumber(str string) bool { for _, v := range str { if unicode.IsNumber(v) == false { diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 71297bd..9f619f2 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -8,7 +8,22 @@ import ( "github.com/stretchr/testify/assert" ) -func TestValidateBarcodesIfBarcodeisInvalid(t *testing.T) { +func TestValidateBarcodesWithJustOneProduct(t *testing.T) { + // Given + selectedBarcodes := make([]string, 0) + selectedBarcodes = append(selectedBarcodes, "ITEM000001") + + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + + // When + result := ValidateBarcodes(selectedBarcodes, products) + + // Then + assert.Equal(t, errorcode.Valid, result.ErrorMes.Code) +} + +func TestValidateBarcodesIfBarcodeIsInvalid(t *testing.T) { // Given selectedBarcodes := make([]string, 0) selectedBarcodes = append(selectedBarcodes, "123456-100") -- Gitee From 305b6d07529fee97485a3d3f8f6be47e68086338 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 14:26:42 +0800 Subject: [PATCH 15/19] [Chenyu Zhao]: fix: fix test of ParseBarcode func with valid selectedBarcodes --- barcode/barcode.go | 18 +++++++----------- barcode/barcode_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/barcode/barcode.go b/barcode/barcode.go index e8144ea..70a87ee 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -90,18 +90,14 @@ func ParseBarcode(selectedBarcodes []string) []*BarcodeInfo { for _, v := range selectedBarcodes { barcode := strings.Split(v, "-") barcodeString := barcode[0] - amount, _ := strconv.Atoi(barcode[1]) - barcodesAndAmount = append(barcodesAndAmount, NewBarcode(barcodeString, uint32(amount))) - } - - return barcodesAndAmount -} -func GroupBarcode(barcodeInfo []*BarcodeInfo) []*BarcodeInfo { - groupedBarcodes := make([]*BarcodeInfo, 0) - if barcodeInfo == nil || len(barcodeInfo) == 0 { - return groupedBarcodes + if len(barcode) == 1 { + barcodesAndAmount = append(barcodesAndAmount, NewBarcode(barcodeString, 1)) + } else { + amount, _ := strconv.Atoi(barcode[1]) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode(barcodeString, uint32(amount))) + } } - return groupedBarcodes + return barcodesAndAmount } diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 9f619f2..1345cd1 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -93,10 +93,16 @@ func TestParseBarcodeWithValidSelectedBarcodes(t *testing.T) { selectedBarcodes := make([]string, 0) selectedBarcodes = append(selectedBarcodes, "ITEM000001-2") selectedBarcodes = append(selectedBarcodes, "ITEM000002-3") + selectedBarcodes = append(selectedBarcodes, "ITEM000003") + selectedBarcodes = append(selectedBarcodes, "ITEM000003") + selectedBarcodes = append(selectedBarcodes, "ITEM000004") barcodesAndAmount := make([]*BarcodeInfo, 0) barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000001", 2)) barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000002", 3)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000003", 1)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000003", 1)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000004", 1)) // When result := ParseBarcode(selectedBarcodes) -- Gitee From dcff1d2b0dba9947139d1677110670bbd19f137f Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 14:42:43 +0800 Subject: [PATCH 16/19] [Chenyu Zhao]: feat: add test of GroupBarcode with valid barcodeAndAmount --- barcode/barcode.go | 28 ++++++++++++++++++++++++++++ barcode/barcode_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/barcode/barcode.go b/barcode/barcode.go index 70a87ee..e12c1cc 100644 --- a/barcode/barcode.go +++ b/barcode/barcode.go @@ -101,3 +101,31 @@ func ParseBarcode(selectedBarcodes []string) []*BarcodeInfo { return barcodesAndAmount } + +func GroupBarcode(barcodeInfo []*BarcodeInfo) []*BarcodeInfo { + groupedBarcodes := make([]*BarcodeInfo, 0) + if barcodeInfo == nil || len(barcodeInfo) == 0 { + return groupedBarcodes + } + + for _, v := range barcodeInfo { + pos := IsContainBarcode(v, groupedBarcodes) + if pos != -1 { + groupedBarcodes[pos].Amount += v.Amount + continue + } + groupedBarcodes = append(groupedBarcodes, NewBarcode(v.Barcode, v.Amount)) + } + + return groupedBarcodes +} + +func IsContainBarcode(barcode *BarcodeInfo, barcodeInfo []*BarcodeInfo) int { + for k, v := range barcodeInfo { + if barcode.Barcode == v.Barcode { + return k + } + } + + return -1 +} diff --git a/barcode/barcode_test.go b/barcode/barcode_test.go index 1345cd1..2ccfcc9 100644 --- a/barcode/barcode_test.go +++ b/barcode/barcode_test.go @@ -122,3 +122,28 @@ func TestGroupBarcodeWithEmptyBarcodeAndAmount(t *testing.T) { // Then assert.Equal(t, groupedBarcodes, result) } + +func TestGroupBarcodeWithValidBarcodeAndAmount(t *testing.T) { + // Given + barcodesAndAmount := make([]*BarcodeInfo, 0) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000001", 2)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000002", 3)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000003", 1)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000003", 1)) + barcodesAndAmount = append(barcodesAndAmount, NewBarcode("ITEM000004", 1)) + + groupedBarcodes := make([]*BarcodeInfo, 0) + groupedBarcodes = append(groupedBarcodes, NewBarcode("ITEM000001", 2)) + groupedBarcodes = append(groupedBarcodes, NewBarcode("ITEM000002", 3)) + groupedBarcodes = append(groupedBarcodes, NewBarcode("ITEM000003", 2)) + groupedBarcodes = append(groupedBarcodes, NewBarcode("ITEM000004", 1)) + + // When + result := GroupBarcode(barcodesAndAmount) + + // Then + assert.Equal(t, groupedBarcodes[0], result[0]) + assert.Equal(t, groupedBarcodes[1], result[1]) + assert.Equal(t, groupedBarcodes[2], result[2]) + assert.Equal(t, groupedBarcodes[3], result[3]) +} -- Gitee From 85ca4d78a0796975793fb825ddfbfcf1a76d2c45 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 16:33:58 +0800 Subject: [PATCH 17/19] [Chenyu Zhao]: feat: add test of CreateReceipt with empty GroupedBarcodes --- receipt/receipt.go | 26 ++++++++++++++++++++++++++ receipt/receipt_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/receipt/receipt.go b/receipt/receipt.go index 487a042..2f4c23f 100644 --- a/receipt/receipt.go +++ b/receipt/receipt.go @@ -2,11 +2,37 @@ package receipt import ( "fmt" + + "github.com/homework-2-test-first-development/barcode" "github.com/homework-2-test-first-development/errorcode" + "github.com/homework-2-test-first-development/product" "github.com/homework-2-test-first-development/validationresult" ) +type ReceiptItem struct { + Name string + TotalPrice uint32 + Amount uint32 +} + type Receipt struct { + Products []*ReceiptItem + TotalPrice uint32 +} + +func CreateReceiptItem(name string, totalPrice uint32, amount uint32) *ReceiptItem { + return &ReceiptItem{ + Name: name, + TotalPrice: totalPrice, + Amount: amount, + } +} + +func CreateReceipt(groupedBarcodes []*barcode.BarcodeInfo, products []*product.Product) *Receipt { + return &Receipt{ + Products: make([]*ReceiptItem, 0), + TotalPrice: 0, + } } func (r *Receipt) CreateErrorReceipt(validationResult validationresult.ValidationResult) string { diff --git a/receipt/receipt_test.go b/receipt/receipt_test.go index 61932a6..4866e22 100644 --- a/receipt/receipt_test.go +++ b/receipt/receipt_test.go @@ -3,11 +3,36 @@ package receipt import ( "testing" + "github.com/homework-2-test-first-development/barcode" "github.com/homework-2-test-first-development/errorcode" + "github.com/homework-2-test-first-development/product" "github.com/homework-2-test-first-development/validationresult" + "github.com/stretchr/testify/assert" ) +func TestCreateReceiptWithEmptyGroupedBarcodes(t *testing.T) { + // Given + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + products = append(products, product.NewProduct("water", 1.00, "ITEM000002")) + products = append(products, product.NewProduct("happywater", 5.00, "ITEM000003")) + products = append(products, product.NewProduct("beer", 3.00, "ITEM000004")) + + groupedBarcodes := make([]*barcode.BarcodeInfo, 0) + + receipt := Receipt{ + Products: make([]*ReceiptItem, 0), + TotalPrice: 0, + } + + // When + result := CreateReceipt(groupedBarcodes, products) + + // Then + assert.Equal(t, receipt, *result) +} + func TestCreateErrorReceiptWhenErrorTypeIsInvalidBarcode(t *testing.T) { // Given var validationResult validationresult.ValidationResult -- Gitee From 57cbf09334ce789bf81f132434ed842598007a50 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 17:17:02 +0800 Subject: [PATCH 18/19] [Chenyu Zhao]: feat: add test of CreateReceipt with valid GroupedBarcodes --- receipt/receipt.go | 36 ++++++++++++++++++++++++++++++------ receipt/receipt_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/receipt/receipt.go b/receipt/receipt.go index 2f4c23f..7af6823 100644 --- a/receipt/receipt.go +++ b/receipt/receipt.go @@ -11,16 +11,16 @@ import ( type ReceiptItem struct { Name string - TotalPrice uint32 + TotalPrice float32 Amount uint32 } type Receipt struct { Products []*ReceiptItem - TotalPrice uint32 + TotalPrice float32 } -func CreateReceiptItem(name string, totalPrice uint32, amount uint32) *ReceiptItem { +func CreateReceiptItem(name string, totalPrice float32, amount uint32) *ReceiptItem { return &ReceiptItem{ Name: name, TotalPrice: totalPrice, @@ -29,10 +29,34 @@ func CreateReceiptItem(name string, totalPrice uint32, amount uint32) *ReceiptIt } func CreateReceipt(groupedBarcodes []*barcode.BarcodeInfo, products []*product.Product) *Receipt { - return &Receipt{ - Products: make([]*ReceiptItem, 0), - TotalPrice: 0, + receipt := &Receipt{} + receipt.Products = make([]*ReceiptItem, 0) + + // forrange groupedBarcodes + for _, v := range groupedBarcodes { + var name string + var price float32 + name, price = GetNameAndPriceFromProduct(v, products) + + receiptItem := &ReceiptItem{} + receiptItem.Name = name + receiptItem.TotalPrice = float32(v.Amount) * price + receiptItem.Amount = v.Amount + receipt.Products = append(receipt.Products, receiptItem) + + receipt.TotalPrice += receiptItem.TotalPrice + } + + return receipt +} + +func GetNameAndPriceFromProduct(barcodeInfo *barcode.BarcodeInfo, products []*product.Product) (string, float32) { + for _, v := range products { + if v.Barcode == barcodeInfo.Barcode { + return v.Name, v.Price + } } + return "", 0 } func (r *Receipt) CreateErrorReceipt(validationResult validationresult.ValidationResult) string { diff --git a/receipt/receipt_test.go b/receipt/receipt_test.go index 4866e22..e7060ea 100644 --- a/receipt/receipt_test.go +++ b/receipt/receipt_test.go @@ -33,6 +33,41 @@ func TestCreateReceiptWithEmptyGroupedBarcodes(t *testing.T) { assert.Equal(t, receipt, *result) } +func TestCreateReceiptWithValidGroupedBarcodes(t *testing.T) { + // Given + products := make([]*product.Product, 0) + products = append(products, product.NewProduct("cola", 3.00, "ITEM000001")) + products = append(products, product.NewProduct("water", 1.00, "ITEM000002")) + products = append(products, product.NewProduct("happywater", 5.00, "ITEM000003")) + products = append(products, product.NewProduct("beer", 3.00, "ITEM000004")) + + groupedBarcodes := make([]*barcode.BarcodeInfo, 0) + groupedBarcodes = append(groupedBarcodes, barcode.NewBarcode("ITEM000001", 4)) + groupedBarcodes = append(groupedBarcodes, barcode.NewBarcode("ITEM000002", 5)) + groupedBarcodes = append(groupedBarcodes, barcode.NewBarcode("ITEM000003", 1)) + groupedBarcodes = append(groupedBarcodes, barcode.NewBarcode("ITEM000004", 3)) + + receipt := Receipt{ + Products: make([]*ReceiptItem, 0), + TotalPrice: 0, + } + receipt.Products = append(receipt.Products, CreateReceiptItem("cola", 12.00, 4)) + receipt.Products = append(receipt.Products, CreateReceiptItem("water", 5.00, 5)) + receipt.Products = append(receipt.Products, CreateReceiptItem("happywater", 5.00, 1)) + receipt.Products = append(receipt.Products, CreateReceiptItem("beer", 9.00, 3)) + receipt.TotalPrice = 31 + + // When + result := CreateReceipt(groupedBarcodes, products) + + // Then + assert.Equal(t, receipt.Products[0], result.Products[0]) + assert.Equal(t, receipt.Products[1], result.Products[1]) + assert.Equal(t, receipt.Products[2], result.Products[2]) + assert.Equal(t, receipt.Products[3], result.Products[3]) + assert.Equal(t, receipt.TotalPrice, result.TotalPrice) +} + func TestCreateErrorReceiptWhenErrorTypeIsInvalidBarcode(t *testing.T) { // Given var validationResult validationresult.ValidationResult -- Gitee From 91265b2d3d677187206699c96d04cd18803715d5 Mon Sep 17 00:00:00 2001 From: Chenyu Zhao Date: Fri, 18 Nov 2022 19:25:15 +0800 Subject: [PATCH 19/19] [Chenyu Zhao]: feat: add test of CreateReceipt with FormatReceipt func with valid receipt --- receipt/receipt.go | 16 +++++++++++++++- receipt/receipt_test.go | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/receipt/receipt.go b/receipt/receipt.go index 7af6823..a2d3a63 100644 --- a/receipt/receipt.go +++ b/receipt/receipt.go @@ -72,5 +72,19 @@ func (r *Receipt) CreateErrorReceipt(validationResult validationresult.Validatio } func (r *Receipt) FormatReceipt(receipt *Receipt) string { - return "Receipt\n------\n------\nTotal: 0.00" + if receipt.Products == nil || len(receipt.Products) == 0 { + return "Receipt\n------\n------\nTotal: 0.00" + } + + itemStr := "Name: %s, Amount: %d, Total: %.2f\n" + result := "" + + for _, v := range receipt.Products { + item := fmt.Sprintf(itemStr, v.Name, v.Amount, v.TotalPrice) + result += item + } + + tailStr := fmt.Sprintf("-------\nTotal: %.2f", receipt.TotalPrice) + + return "Receipt\n-------\n" + result + tailStr } diff --git a/receipt/receipt_test.go b/receipt/receipt_test.go index e7060ea..7f84b1f 100644 --- a/receipt/receipt_test.go +++ b/receipt/receipt_test.go @@ -47,7 +47,7 @@ func TestCreateReceiptWithValidGroupedBarcodes(t *testing.T) { groupedBarcodes = append(groupedBarcodes, barcode.NewBarcode("ITEM000003", 1)) groupedBarcodes = append(groupedBarcodes, barcode.NewBarcode("ITEM000004", 3)) - receipt := Receipt{ + receipt := &Receipt{ Products: make([]*ReceiptItem, 0), TotalPrice: 0, } @@ -100,3 +100,19 @@ func TestFormatReceiptWithNoProducts(t *testing.T) { // Then assert.Equal(t, "Receipt\n------\n------\nTotal: 0.00", receiptContent) } + +func TestFormatReceiptWithValidReceipt(t *testing.T) { + // Given + receipt := &Receipt{ + Products: make([]*ReceiptItem, 0), + TotalPrice: 0, + } + receipt.Products = append(receipt.Products, CreateReceiptItem("cola", 12.00, 4)) + receipt.Products = append(receipt.Products, CreateReceiptItem("water", 5.00, 5)) + receipt.TotalPrice = 17 + // When + receiptContent := receipt.FormatReceipt(receipt) + // Then + assert.Equal(t, "Receipt\n-------\nName: cola, Amount: 4, Total: 12.00\nName: water, Amount: 5, Total: 5.00\n-------\nTotal: 17.00", + receiptContent) +} -- Gitee