Add tests for validation package

This commit is contained in:
Fred Boniface 2023-09-05 12:35:52 +01:00
parent cfb0d4ebc6
commit 259dc875a4
2 changed files with 78 additions and 1 deletions

View File

@ -86,7 +86,7 @@ var FormatRules = map[string]rule{
"qr": {
minLength: 1,
maxLength: 4296,
charset: *ISO88591,
charset: *ANY,
divisible: 1,
},
}

View File

@ -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)
}
}
}