diff options
author | EuAndreh <eu@euandre.org> | 2025-05-03 19:41:19 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2025-05-03 19:41:19 -0300 |
commit | a6e6896309007fa1013bd51cee125ab8a75f282d (patch) | |
tree | f6839c339e9d282138a793488cd8ac23ca356ac6 /tests/uuid.go | |
parent | tests/guuid.go: Use os.Exit over assert when testing panic (diff) | |
download | uuid-a6e6896309007fa1013bd51cee125ab8a75f282d.tar.gz uuid-a6e6896309007fa1013bd51cee125ab8a75f282d.tar.xz |
re s/guuid/uuid/g
Diffstat (limited to 'tests/uuid.go')
-rw-r--r-- | tests/uuid.go | 261 |
1 files changed, 261 insertions, 0 deletions
diff --git a/tests/uuid.go b/tests/uuid.go new file mode 100644 index 0000000..ad9cac4 --- /dev/null +++ b/tests/uuid.go @@ -0,0 +1,261 @@ +package uuid + +import ( + "bytes" + "encoding/hex" + "fmt" + "os" + "reflect" + "strings" +) + + + +func showColour() bool { + return os.Getenv("NO_COLOUR") == "" +} + +func testStart(name string) { + fmt.Fprintf(os.Stderr, "%s:\n", name) +} + +func testing(message string, body func()) { + if showColour() { + fmt.Fprintf( + os.Stderr, + "\033[0;33mtesting\033[0m: %s... ", + message, + ) + body() + fmt.Fprintf(os.Stderr, "\033[0;32mOK\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "testing: %s... ", message) + body() + fmt.Fprintf(os.Stderr, "OK.\n") + } +} + +func assertEq(given any, expected any) { + if !reflect.DeepEqual(given, expected) { + if showColour() { + fmt.Fprintf(os.Stderr, "\033[0;31mERR\033[0m.\n") + } else { + fmt.Fprintf(os.Stderr, "ERR.\n") + } + fmt.Fprintf(os.Stderr, "given != expected\n") + fmt.Fprintf(os.Stderr, "given: %#v\n", given) + fmt.Fprintf(os.Stderr, "expected: %#v\n", expected) + os.Exit(1) + } +} + + +func test_NewFrom() { + testStart("NewFrom()") + + testing("we get the same UUID from the same input", func() { + const s = "abcdefghijklmnop" + + r1 := strings.NewReader(s) + uuid1, err := NewFrom(r1) + assertEq(err, nil) + + r2 := strings.NewReader(s) + uuid2, err := NewFrom(r2) + assertEq(err, nil) + + assertEq(uuid1, uuid2) + }) + + testing("we the bytes in UUID are what the reader gives us", func() { + input := []byte{ + 0x00, + 0x01, + 0x02, + 0x03, + 0x04, + 0x05, + 0x06, + 0x07, + 0x08, + 0x09, + 0x0a, + 0x0b, + 0x0c, + 0x0d, + 0x0e, + 0x0f, + } + + expected := UUID{ + 0x00, + 0x01, + 0x02, + 0x03, + 0x04, + 0x05, + 0x06 + 0x40, + 0x07, + 0x08 + 0x80, + 0x09, + 0x0a, + 0x0b, + 0x0c, + 0x0d, + 0x0e, + 0x0f, + } + + r := bytes.NewReader(input) + given, err := NewFrom(r) + assertEq(err, nil) + assertEq(given, expected) + }) + + testing("v4 and variant markers", func() { + input := []byte{ + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x71, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + } + + expected := UUID{ + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x41, // not 0x11 + 0x11, + 0xb1, // not 0x11 + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + 0x11, + } + + r := bytes.NewReader(input) + given, err := NewFrom(r) + assertEq(err, nil) + assertEq(given, expected) + }) +} + +func test_New() { + testStart("New()") + + testing("we can generate UUID values: ", func() { + var uuid UUID = New() + assertEq(len(uuid), 16) + }) + + testing("panic when the randomReader fails", func() { + savedReader := randomReader + randomReader = strings.NewReader("abc") + defer func() { + r := recover() + assertEq(r == nil, false) + randomReader = savedReader + }() + + New() + os.Exit(5) + }) +} + +func test_String() { + testStart("UUID.String()") + + testing("simple example values", func() { + uuids := []UUID { + UUID { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + }, + UUID { + 0, 1, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 11, 12, 13, 14, 15, + }, + UUID { + 222, 222, 222, 222, 222, 222, 222, 222, + 222, 222, 222, 222, 222, 222, 222, 222, + }, + UUID { + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + }, + } + + strs := []string { + "00000000-0000-0000-0000-000000000000", + "00010203-0405-0607-0809-0a0b0c0d0e0f", + "dededede-dede-dede-dede-dededededede", + "ffffffff-ffff-ffff-ffff-ffffffffffff", + } + + for i := range uuids { + s1 := uuids[i].String() + s2 := fmt.Sprintf("%v", uuids[i]) + assertEq(s1, strs[i]) + assertEq(s2, strs[i]) + } + }) +} + +func test_FromString() { + testStart("FromString()") + + testing("UUID -> string -> UUID round trip", func() { + for i := 0; i < 100; i++ { + uuid0 := New() + uuid1, err := FromString(uuid0.String()) + assertEq(err, nil) + assertEq(uuid0, uuid1) + } + }) + + testing("errors we detect", func() { + var err error + + _, err = FromString("") + assertEq(err, ErrBadLength) + + _, err = FromString("---000000000000000000000000000000000") + assertEq(err, ErrBadDashCount) + + _, err = FromString("----00000000000000000000000000000000") + assertEq(err, ErrBadDashPosition) + + _, err = FromString("00000000-0000-0000-0000-00000000000g") + assertEq(err, hex.InvalidByteError('g')) + + _, err = FromString("00000000-0000-0000-0000-000000000000") + assertEq(err, nil) + }) +} + + + +func MainTest() { + test_NewFrom() + test_New() + test_String() + test_FromString() +} |