From 259dc875a4e6aab31573db26fbfdc27153e5bdf4 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 5 Sep 2023 12:35:52 +0100 Subject: [PATCH] Add tests for validation package --- validation/rules.go | 2 +- validation/validate_test.go | 77 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 validation/validate_test.go diff --git a/validation/rules.go b/validation/rules.go index 33ecc03..a31b18e 100644 --- a/validation/rules.go +++ b/validation/rules.go @@ -86,7 +86,7 @@ var FormatRules = map[string]rule{ "qr": { minLength: 1, maxLength: 4296, - charset: *ISO88591, + charset: *ANY, divisible: 1, }, } diff --git a/validation/validate_test.go b/validation/validate_test.go new file mode 100644 index 0000000..4f9fbe8 --- /dev/null +++ b/validation/validate_test.go @@ -0,0 +1,77 @@ +package validation + +import ( + "fmt" + "testing" + + "git.fjla.uk/fred.boniface/barcodes/generation" +) + +const unicodeString string = "Hello, 你好, नमस्ते, こんにちは, 😃🌍🚀🎉~!@#$%^&*()_+-=[]{}|;:'\",.<>?/\\±§©µ¿¡®¬°¹²³½¾¼×÷≤≥∞∑∫√∂∆∏∃∀Ω≈≠¬≡≣∀∂∃Ω℧℁ℝℂℍℤℚ" +const asciiString string = "!7^%&3#(1@!9)6*8_:;4<~2`5-+0|{/.,?]'\"~" + +func TestValidation(t *testing.T) { + testCases := []struct { + input generation.Parameters + expected bool + }{ + { + input: generation.Parameters{ + Format: "aztec", + ECCLevel: 4, + Content: unicodeString, + }, + expected: true, + }, + { + input: generation.Parameters{ + Format: "codabar", + ECCLevel: 4, + Content: "ABCD", + }, + expected: false, + }, + { + input: generation.Parameters{ + Format: "codabar", + ECCLevel: 4, + Content: "2342-943$", + }, + expected: true, + }, + { + input: generation.Parameters{ + Format: "datamatrix", + ECCLevel: 4, + Content: unicodeString, + }, + expected: true, + }, + { + input: generation.Parameters{ + Format: "qr", + ECCLevel: 4, + Content: unicodeString, + }, + expected: true, + }, + { + input: generation.Parameters{ + Format: "code39", + ECCLevel: 4, + Content: asciiString, + }, + expected: true, + }, + } + + for _, testCase := range testCases { + result := Validate(testCase.input) + + if result != testCase.expected { + inputStr := fmt.Sprintf("%+v", testCase.input) // Convert struct to string representation + + t.Errorf("Input: \n%s\n\nExpected: %t\n\n Got: %t", inputStr, testCase.expected, result) + } + } +}