Skip to content

Commit 4f33da3

Browse files
author
Jonathan Sharman
committed
Add StrictValidate tests
1 parent 87ce8f4 commit 4f33da3

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

uuid_test.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,32 @@ func FuzzFromBytes(f *testing.F) {
641641
})
642642
}
643643

644+
// TestStrictValidate checks various scenarios for the StrictValidate function
645+
func TestStrictValidate(t *testing.T) {
646+
testCases := []struct {
647+
name string
648+
input string
649+
expect error
650+
}{
651+
{"Valid UUID", "123e4567-e89b-12d3-a456-426655440000", nil},
652+
{"Valid UUID with URN", "urn:uuid:123e4567-e89b-12d3-a456-426655440000", nil},
653+
{"Nonstandard UUID with Braces", "{123e4567-e89b-12d3-a456-426655440000}", errors.New("invalid UUID length: 38")},
654+
{"Nonstandard UUID No Hyphens", "123e4567e89b12d3a456426655440000", errors.New("invalid UUID length: 32")},
655+
{"Invalid UUID", "invalid-uuid", errors.New("invalid UUID length: 12")},
656+
{"Invalid Length", "123", fmt.Errorf("invalid UUID length: %d", len("123"))},
657+
{"Invalid URN Prefix", "urn:test:123e4567-e89b-12d3-a456-426655440000", fmt.Errorf("invalid urn prefix: %q", "urn:test:")},
658+
}
659+
660+
for _, tc := range testCases {
661+
t.Run(tc.name, func(t *testing.T) {
662+
err := StrictValidate(tc.input)
663+
if (err != nil) != (tc.expect != nil) || (err != nil && err.Error() != tc.expect.Error()) {
664+
t.Errorf("StrictValidate(%q) = %v, want %v", tc.input, err, tc.expect)
665+
}
666+
})
667+
}
668+
}
669+
644670
// TestValidate checks various scenarios for the Validate function
645671
func TestValidate(t *testing.T) {
646672
testCases := []struct {
@@ -669,8 +695,10 @@ func TestValidate(t *testing.T) {
669695
}
670696
}
671697

672-
var asString = "f47ac10b-58cc-0372-8567-0e02b2c3d479"
673-
var asBytes = []byte(asString)
698+
var (
699+
asString = "f47ac10b-58cc-0372-8567-0e02b2c3d479"
700+
asBytes = []byte(asString)
701+
)
674702

675703
func BenchmarkParse(b *testing.B) {
676704
for i := 0; i < b.N; i++ {
@@ -935,7 +963,7 @@ func TestVersion7Monotonicity(t *testing.T) {
935963
type fakeRand struct{}
936964

937965
func (g fakeRand) Read(bs []byte) (int, error) {
938-
for i, _ := range bs {
966+
for i := range bs {
939967
bs[i] = 0x88
940968
}
941969
return len(bs), nil

0 commit comments

Comments
 (0)