| 1 |
package uuid |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/stretchr/testify/assert" |
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
const ( |
| 11 |
clean = `[0-9a-f]{8}[0-9a-f]{4}[1-5][0-9a-f]{3}[0-9a-f]{4}[0-9a-f]{12}` |
| 12 |
cleanHexPattern = `^` + clean + `$` |
| 13 |
curlyHexPattern = `^\{` + clean + `\}$` |
| 14 |
bracketHexPattern = `^\(` + clean + `\)$` |
| 15 |
hyphen = `[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}` |
| 16 |
cleanHyphenHexPattern = `^` + hyphen + `$` |
| 17 |
curlyHyphenHexPattern = `^\{` + hyphen + `\}$` |
| 18 |
bracketHyphenHexPattern = `^\(` + hyphen + `\)$` |
| 19 |
urnHexPattern = `^urn:uuid:` + hyphen + `$` |
| 20 |
) |
| 21 |
|
| 22 |
var ( |
| 23 |
formats = []Format{ |
| 24 |
FormatCanonicalCurly, |
| 25 |
FormatHex, |
| 26 |
FormatHexCurly, |
| 27 |
FormatHexBracket, |
| 28 |
FormatCanonical, |
| 29 |
FormatCanonicalBracket, |
| 30 |
FormatUrn, |
| 31 |
} |
| 32 |
patterns = []string{ |
| 33 |
curlyHyphenHexPattern, |
| 34 |
cleanHexPattern, |
| 35 |
curlyHexPattern, |
| 36 |
bracketHexPattern, |
| 37 |
cleanHyphenHexPattern, |
| 38 |
bracketHyphenHexPattern, |
| 39 |
urnHexPattern, |
| 40 |
} |
| 41 |
) |
| 42 |
|
| 43 |
func TestSwitchFormat(t *testing.T) { |
| 44 |
ids := []UUID{NewV4(), NewV4()} |
| 45 |
|
| 46 |
// Reset default |
| 47 |
SwitchFormat(FormatCanonical) |
| 48 |
|
| 49 |
for _, u := range ids { |
| 50 |
for i := range formats { |
| 51 |
SwitchFormat(formats[i]) |
| 52 |
assert.True(t, regexp.MustCompile(patterns[i]).MatchString(u.String()), "Format %s must compile pattern %s", formats[i], patterns[i]) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
assert.True(t, didSwitchFormatPanic(""), "Switch format should panic when format invalid") |
| 57 |
assert.True(t, didSwitchFormatPanic("%c%c%c%x%x%x"), "Switch format should panic when format invalid") |
| 58 |
assert.True(t, didSwitchFormatPanic("%x%X%x"), "Switch format should panic when format invalid") |
| 59 |
assert.True(t, didSwitchFormatPanic("%x%x%x%x%x%%%%"), "Switch format should panic when format invalid") |
| 60 |
|
| 61 |
// Reset default |
| 62 |
SwitchFormat(FormatCanonical) |
| 63 |
} |
| 64 |
|
| 65 |
func didSwitchFormatPanic(pFormat string) bool { |
| 66 |
return func() (didPanic bool) { |
| 67 |
defer func() { |
| 68 |
if recover() != nil { |
| 69 |
didPanic = true |
| 70 |
} |
| 71 |
}() |
| 72 |
|
| 73 |
SwitchFormat(Format(pFormat)) |
| 74 |
return |
| 75 |
}() |
| 76 |
} |
| 77 |
|
| 78 |
func TestSwitchFormatToUpper(t *testing.T) { |
| 79 |
ids := []UUID{NewV4(), NewV4()} |
| 80 |
|
| 81 |
// Reset default |
| 82 |
SwitchFormat(FormatCanonical) |
| 83 |
|
| 84 |
for _, u := range ids { |
| 85 |
for i := range formats { |
| 86 |
SwitchFormatToUpper(formats[i]) |
| 87 |
assert.True(t, regexp.MustCompile(strings.ToUpper(patterns[i])).MatchString(u.String()), "Format %s must compile pattern %s", formats[i], patterns[i]) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
assert.True(t, didSwitchFormatToUpperPanic(""), "Switch format should panic when format invalid") |
| 92 |
assert.True(t, didSwitchFormatToUpperPanic("%c%c%c%x%x%x"), "Switch format should panic when format invalid") |
| 93 |
assert.True(t, didSwitchFormatToUpperPanic("%x%X%x"), "Switch format should panic when format invalid") |
| 94 |
assert.True(t, didSwitchFormatToUpperPanic("%x%x%x%x%x%%%%"), "Switch format should panic when format invalid") |
| 95 |
|
| 96 |
// Reset default |
| 97 |
SwitchFormat(FormatCanonical) |
| 98 |
} |
| 99 |
|
| 100 |
func didSwitchFormatToUpperPanic(pFormat string) bool { |
| 101 |
return func() (didPanic bool) { |
| 102 |
defer func() { |
| 103 |
if recover() != nil { |
| 104 |
didPanic = true |
| 105 |
} |
| 106 |
}() |
| 107 |
|
| 108 |
SwitchFormatToUpper(Format(pFormat)) |
| 109 |
return |
| 110 |
}() |
| 111 |
} |
| 112 |
|
| 113 |
func TestFormatter(t *testing.T) { |
| 114 |
ids := []UUID{NewV4(), NewV4()} |
| 115 |
|
| 116 |
for _, u := range ids { |
| 117 |
for i := range formats { |
| 118 |
assert.True(t, regexp.MustCompile(patterns[i]).MatchString(Formatter(u, formats[i])), "Format must compile") |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
for k, v := range namespaces { |
| 123 |
s := Formatter(k, FormatCanonical) |
| 124 |
assert.Equal(t, v, s, "Should match") |
| 125 |
|
| 126 |
s = Formatter(k, Format(strings.ToUpper(string(FormatCanonical)))) |
| 127 |
assert.Equal(t, strings.ToUpper(v), s, "Should match") |
| 128 |
} |
| 129 |
|
| 130 |
assert.True(t, didFormatterPanic(""), "Should panic when format invalid") |
| 131 |
assert.True(t, didFormatterPanic("%c%c%c%x%x%x"), "Should panic when format invalid") |
| 132 |
assert.True(t, didFormatterPanic("%x%X%x"), "Should panic when format invalid") |
| 133 |
assert.True(t, didFormatterPanic("%x%x%x%x%x%%%%"), "Should panic when format invalid") |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
func didFormatterPanic(pFormat string) bool { |
| 138 |
return func() (didPanic bool) { |
| 139 |
defer func() { |
| 140 |
if recover() != nil { |
| 141 |
didPanic = true |
| 142 |
} |
| 143 |
}() |
| 144 |
|
| 145 |
Formatter(NameSpaceDNS, Format(pFormat)) |
| 146 |
return |
| 147 |
}() |
| 148 |
} |