File: format_test.go

package info (click to toggle)
golang-github-twinj-uuid 0.10.0%2Bgit20160909.96.7bbe408-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 236 kB
  • sloc: makefile: 3
file content (148 lines) | stat: -rw-r--r-- 4,093 bytes parent folder | download | duplicates (3)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package uuid

import (
	"github.com/stretchr/testify/assert"
	"regexp"
	"strings"
	"testing"
)

const (
	clean                   = `[0-9a-f]{8}[0-9a-f]{4}[1-5][0-9a-f]{3}[0-9a-f]{4}[0-9a-f]{12}`
	cleanHexPattern         = `^` + clean + `$`
	curlyHexPattern         = `^\{` + clean + `\}$`
	bracketHexPattern       = `^\(` + clean + `\)$`
	hyphen                  = `[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}`
	cleanHyphenHexPattern   = `^` + hyphen + `$`
	curlyHyphenHexPattern   = `^\{` + hyphen + `\}$`
	bracketHyphenHexPattern = `^\(` + hyphen + `\)$`
	urnHexPattern           = `^urn:uuid:` + hyphen + `$`
)

var (
	formats = []Format{
		FormatCanonicalCurly,
		FormatHex,
		FormatHexCurly,
		FormatHexBracket,
		FormatCanonical,
		FormatCanonicalBracket,
		FormatUrn,
	}
	patterns = []string{
		curlyHyphenHexPattern,
		cleanHexPattern,
		curlyHexPattern,
		bracketHexPattern,
		cleanHyphenHexPattern,
		bracketHyphenHexPattern,
		urnHexPattern,
	}
)

func TestSwitchFormat(t *testing.T) {
	ids := []UUID{NewV4(), NewV4()}

	// Reset default
	SwitchFormat(FormatCanonical)

	for _, u := range ids {
		for i := range formats {
			SwitchFormat(formats[i])
			assert.True(t, regexp.MustCompile(patterns[i]).MatchString(u.String()), "Format %s must compile pattern %s", formats[i], patterns[i])
		}
	}

	assert.True(t, didSwitchFormatPanic(""), "Switch format should panic when format invalid")
	assert.True(t, didSwitchFormatPanic("%c%c%c%x%x%x"), "Switch format should panic when format invalid")
	assert.True(t, didSwitchFormatPanic("%x%X%x"), "Switch format should panic when format invalid")
	assert.True(t, didSwitchFormatPanic("%x%x%x%x%x%%%%"), "Switch format should panic when format invalid")

	// Reset default
	SwitchFormat(FormatCanonical)
}

func didSwitchFormatPanic(pFormat string) bool {
	return func() (didPanic bool) {
		defer func() {
			if recover() != nil {
				didPanic = true
			}
		}()

		SwitchFormat(Format(pFormat))
		return
	}()
}

func TestSwitchFormatToUpper(t *testing.T) {
	ids := []UUID{NewV4(), NewV4()}

	// Reset default
	SwitchFormat(FormatCanonical)

	for _, u := range ids {
		for i := range formats {
			SwitchFormatToUpper(formats[i])
			assert.True(t, regexp.MustCompile(strings.ToUpper(patterns[i])).MatchString(u.String()), "Format %s must compile pattern %s", formats[i], patterns[i])
		}
	}

	assert.True(t, didSwitchFormatToUpperPanic(""), "Switch format should panic when format invalid")
	assert.True(t, didSwitchFormatToUpperPanic("%c%c%c%x%x%x"), "Switch format should panic when format invalid")
	assert.True(t, didSwitchFormatToUpperPanic("%x%X%x"), "Switch format should panic when format invalid")
	assert.True(t, didSwitchFormatToUpperPanic("%x%x%x%x%x%%%%"), "Switch format should panic when format invalid")

	// Reset default
	SwitchFormat(FormatCanonical)
}

func didSwitchFormatToUpperPanic(pFormat string) bool {
	return func() (didPanic bool) {
		defer func() {
			if recover() != nil {
				didPanic = true
			}
		}()

		SwitchFormatToUpper(Format(pFormat))
		return
	}()
}

func TestFormatter(t *testing.T) {
	ids := []UUID{NewV4(), NewV4()}

	for _, u := range ids {
		for i := range formats {
			assert.True(t, regexp.MustCompile(patterns[i]).MatchString(Formatter(u, formats[i])), "Format must compile")
		}
	}

	for k, v := range namespaces {
		s := Formatter(k, FormatCanonical)
		assert.Equal(t, v, s, "Should match")

		s = Formatter(k, Format(strings.ToUpper(string(FormatCanonical))))
		assert.Equal(t, strings.ToUpper(v), s, "Should match")
	}

	assert.True(t, didFormatterPanic(""), "Should panic when format invalid")
	assert.True(t, didFormatterPanic("%c%c%c%x%x%x"), "Should panic when format invalid")
	assert.True(t, didFormatterPanic("%x%X%x"), "Should panic when format invalid")
	assert.True(t, didFormatterPanic("%x%x%x%x%x%%%%"), "Should panic when format invalid")

}

func didFormatterPanic(pFormat string) bool {
	return func() (didPanic bool) {
		defer func() {
			if recover() != nil {
				didPanic = true
			}
		}()

		Formatter(NameSpaceDNS, Format(pFormat))
		return
	}()
}