package envelope import ( "net" "net/url" "reflect" "testing" "time" "git.eugenetriguba.com/checkmate/assert" ) // GIVEN a string parser // WHEN we parse a string value // THEN it should return the string value as-is func TestStringParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf("")] result, err := parser.Parse("test string") assert.Nil(t, err) assert.Equal(t, "test string", result) } // GIVEN various integer parsers // WHEN we parse string representations of integers // THEN it should return the correct integer type func TestIntParsers(t *testing.T) { tests := []struct { name string typ reflect.Type input string expected any }{ { name: "int", typ: reflect.TypeOf(int(0)), input: "42", expected: int(42), }, { name: "int8", typ: reflect.TypeOf(int8(0)), input: "42", expected: int8(42), }, { name: "int16", typ: reflect.TypeOf(int16(0)), input: "42", expected: int16(42), }, { name: "int32", typ: reflect.TypeOf(int32(0)), input: "42", expected: int32(42), }, { name: "int64", typ: reflect.TypeOf(int64(0)), input: "42", expected: int64(42), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { parser := typeParserRegistry[tt.typ] result, err := parser.Parse(tt.input) assert.Nil(t, err) assert.Equal(t, tt.expected, result) }) } } // GIVEN various unsigned integer parsers // WHEN we parse string representations of integers // THEN it should return the correct unsigned integer type func TestUintParsers(t *testing.T) { tests := []struct { name string typ reflect.Type input string expected any }{ { name: "uint", typ: reflect.TypeOf(uint(0)), input: "42", expected: uint(42), }, { name: "uint8", typ: reflect.TypeOf(uint8(0)), input: "42", expected: uint8(42), }, { name: "uint16", typ: reflect.TypeOf(uint16(0)), input: "42", expected: uint16(42), }, { name: "uint32", typ: reflect.TypeOf(uint32(0)), input: "42", expected: uint32(42), }, { name: "uint64", typ: reflect.TypeOf(uint64(0)), input: "42", expected: uint64(42), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { parser := typeParserRegistry[tt.typ] result, err := parser.Parse(tt.input) assert.Nil(t, err) assert.Equal(t, tt.expected, result) }) } } // GIVEN a boolean parser // WHEN we parse string representations of boolean values // THEN it should: // - Return true for various true-like values (true, TRUE, 1, on, yes, etc.) // - Return false for various false-like values (false, FALSE, 0, off, no, etc.) // - Return an error for invalid boolean values func TestBoolParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf(bool(false))] for _, val := range []string{"true", "TRUE", "True", "1", "t", "T", "on", "ON", "yes", "YES", "y", "Y"} { result, err := parser.Parse(val) assert.Nil(t, err) assert.Equal(t, true, result) } for _, val := range []string{"false", "FALSE", "False", "0", "f", "F", "off", "OFF", "no", "NO", "n", "N"} { result, err := parser.Parse(val) assert.Nil(t, err) assert.Equal(t, false, result) } _, err := parser.Parse("not-a-bool") assert.NotNil(t, err) } // GIVEN float parsers (float32, float64) // WHEN we parse string representations of floating point numbers // THEN it should return the correct float type with proper precision func TestFloatParsers(t *testing.T) { tests := []struct { name string typ reflect.Type input string expected any }{ { name: "float32", typ: reflect.TypeOf(float32(0)), input: "3.14159", expected: float32(3.14159), }, { name: "float64", typ: reflect.TypeOf(float64(0)), input: "3.14159265359", expected: float64(3.14159265359), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { parser := typeParserRegistry[tt.typ] result, err := parser.Parse(tt.input) assert.Nil(t, err) assert.Equal(t, tt.expected, result) }) } } // GIVEN a time.Time parser // WHEN we parse: // - A valid RFC3339 formatted time string // - An invalid time string // // THEN it should return the correct time.Time value or an error respectively func TestTimeParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf(time.Time{})] result, err := parser.Parse("2023-08-31T12:34:56Z") assert.Nil(t, err) expected, _ := time.Parse(time.RFC3339, "2023-08-31T12:34:56Z") assert.Equal(t, expected, result) _, err = parser.Parse("not-a-time") assert.NotNil(t, err) } // GIVEN a time.Duration parser // WHEN we parse various duration strings (5s, 1m30s, 1h, 24h) // THEN it should: // - Convert each string to the correct time.Duration value // - Return an error for invalid duration strings func TestDurationParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf(time.Duration(0))] tests := []struct { input string expected time.Duration }{ {"5s", 5 * time.Second}, {"1m30s", 90 * time.Second}, {"1h", time.Hour}, {"24h", 24 * time.Hour}, } for _, tt := range tests { result, err := parser.Parse(tt.input) assert.Nil(t, err) assert.Equal(t, tt.expected, result) } _, err := parser.Parse("not-a-duration") assert.NotNil(t, err) } // GIVEN a net.IP parser // WHEN we parse various IP address formats (IPv4 and IPv6) // THEN it should: // - Correctly convert each string to a net.IP value // - Return an error for invalid IP addresses func TestIPParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf(net.IP{})] tests := []string{ "192.168.1.1", "127.0.0.1", "::1", "2001:db8::1", } for _, testip := range tests { result, err := parser.Parse(testip) assert.Nil(t, err) ip, ok := result.(net.IP) assert.True(t, ok) assert.Equal(t, testip, ip.String()) } _, err := parser.Parse("not-an-ip") assert.NotNil(t, err) } // GIVEN a net.IPNet parser // WHEN we parse CIDR notation strings // THEN it should: // - Correctly convert each string to a net.IPNet value // - Return an error for invalid CIDR notations func TestIPNetParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf(net.IPNet{})] tests := []string{ "192.168.1.0/24", "10.0.0.0/8", "2001:db8::/32", } for _, ipnet := range tests { result, err := parser.Parse(ipnet) assert.Nil(t, err) ipNet, ok := result.(net.IPNet) assert.True(t, ok) assert.Equal(t, ipnet, ipNet.String()) } _, err := parser.Parse("not-a-cidr") assert.NotNil(t, err) } // GIVEN a url.URL parser // WHEN we parse URL strings // THEN it should: // - Correctly convert each string to a url.URL value // - Return an error for invalid URL strings func TestURLParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf(url.URL{})] tests := []struct { input string expected string }{ {"https://example.com", "https://example.com"}, {"http://localhost:8080/path?query=value", "http://localhost:8080/path?query=value"}, {"postgres://user:pass@host:5432/dbname", "postgres://user:pass@host:5432/dbname"}, } for _, tt := range tests { result, err := parser.Parse(tt.input) assert.Nil(t, err) url, ok := result.(url.URL) assert.True(t, ok) assert.Equal(t, tt.expected, url.String()) } _, err := parser.Parse(":") assert.NotNil(t, err) } // GIVEN a string slice parser // WHEN we parse comma-separated string values // THEN it should correctly split the input into a string slice func TestStringSliceParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf([]string{})] tests := []struct { input string expected []string }{ {"one,two,three", []string{"one", "two", "three"}}, {"", []string{}}, {"single", []string{"single"}}, {"with space, and another", []string{"with space", " and another"}}, } for _, tt := range tests { result, err := parser.Parse(tt.input) assert.Nil(t, err) slice, ok := result.([]string) assert.True(t, ok) assert.Equal(t, len(tt.expected), len(slice)) for i, v := range tt.expected { assert.Equal(t, v, slice[i]) } } } // GIVEN an int slice parser // WHEN we parse comma-separated integer values // THEN it should: // - Correctly convert each element to an integer // - Return an error if any element cannot be parsed as an integer func TestIntSliceParser(t *testing.T) { parser := typeParserRegistry[reflect.TypeOf([]int{})] tests := []struct { input string expected []int }{ {"1,2,3", []int{1, 2, 3}}, {"", []int{}}, {"42", []int{42}}, {"1, 2, 3", []int{1, 2, 3}}, } for _, tt := range tests { result, err := parser.Parse(tt.input) assert.Nil(t, err) slice, ok := result.([]int) assert.True(t, ok) assert.Equal(t, len(tt.expected), len(slice)) for i, v := range tt.expected { assert.Equal(t, v, slice[i]) } } _, err := parser.Parse("1,not-an-int,3") assert.NotNil(t, err) } // GIVEN the RegisterTypeParser function // WHEN we register a custom type parser // THEN it should: // - Successfully add the parser to the registry // - Correctly use the parser when parsing values func TestRegisterTypeParserSupportsRegisteringCustomTypes(t *testing.T) { type CustomType struct { Value string } defer delete(typeParserRegistry, reflect.TypeOf(CustomType{})) RegisterTypeParser(reflect.TypeOf(CustomType{}), TypeParserFunc(func(value string) (any, error) { return CustomType{Value: "prefix-" + value}, nil })) parser, exists := typeParserRegistry[reflect.TypeOf(CustomType{})] assert.True(t, exists) result, err := parser.Parse("test") assert.Nil(t, err) customValue, ok := result.(CustomType) assert.True(t, ok) assert.Equal(t, "prefix-test", customValue.Value) } // GIVEN various type parsers // WHEN we provide invalid string values // THEN each parser should return an appropriate error func TestParseInvalidValues(t *testing.T) { tests := []struct { name string typ reflect.Type input string }{ {"Invalid int", reflect.TypeOf(int(0)), "not-an-int"}, {"Invalid int8", reflect.TypeOf(int8(0)), "not-an-int"}, {"Invalid int16", reflect.TypeOf(int16(0)), "not-an-int"}, {"Invalid int32", reflect.TypeOf(int32(0)), "not-an-int"}, {"Invalid int64", reflect.TypeOf(int64(0)), "not-an-int"}, {"Invalid uint", reflect.TypeOf(uint(0)), "not-a-uint"}, {"Invalid uint8", reflect.TypeOf(uint8(0)), "not-an-int"}, {"Invalid uint16", reflect.TypeOf(uint16(0)), "not-an-int"}, {"Invalid uint32", reflect.TypeOf(uint32(0)), "not-an-int"}, {"Invalid uint64", reflect.TypeOf(uint64(0)), "not-an-int"}, {"Invalid uint negative", reflect.TypeOf(uint(0)), "-5"}, {"Invalid float32", reflect.TypeOf(float32(0)), "not-a-float"}, {"Invalid float64", reflect.TypeOf(float64(0)), "not-a-float"}, {"Invalid bool", reflect.TypeOf(false), "not-a-bool"}, {"Invalid time", reflect.TypeOf(time.Time{}), "not-a-time"}, {"Invalid duration", reflect.TypeOf(time.Duration(0)), "not-a-duration"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { parser := typeParserRegistry[tt.typ] _, err := parser.Parse(tt.input) assert.NotNil(t, err) }) } }