summaryrefslogtreecommitdiff
path: root/tests/functional/string-round-trip/uuid.go
blob: 82414ea51eb2fdc1cbdacd64ec3ccb869caaca31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package uuid

import (
	"fmt"
	"os"
	"reflect"
)



func showColour() bool {
	return os.Getenv("NO_COLOUR") == ""
}

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 MainTest() {
	testing("v4 string is the same after round-trip", func() {
		str1 := NewV4().String()
		id, err := FromString(str1)
		assertEq(err, nil)
		str2 := id.String()
		assertEq(str1, str2)
	})

	testing("v4 UUID is the same after round-trip", func() {
		id1 := NewV4()
		id2, err := FromString(id1.String())
		assertEq(err, nil)
		assertEq(id1, id2)
	})

	testing("v7 string is the same after round-trip", func() {
		str1 := NewV7().String()
		id, err := FromString(str1)
		assertEq(err, nil)
		str2 := id.String()
		assertEq(str1, str2)
	})

	testing("v7 UUID is the same after round-trip", func() {
		id1 := NewV7()
		id2, err := FromString(id1.String())
		assertEq(err, nil)
		assertEq(id1, id2)
	})
}