GUID tests to use t.Run (#256)

Switched to subtests using `t.Run()` for GUID testing, rather than using
log statements to demarcate different tests.

Added `.String` to GUID `Variant` and `Version` using
`golang.org/x/tools/cmd/stringer`.

Added `tools.go` to version `stringer` in go.mod and allow
`go generate ./...` to be run without needing a `go get` call.
Based off of [go wiki](https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module)
recommendation.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
This commit is contained in:
Hamza El-Saawy
2022-08-29 16:27:34 -04:00
committed by GitHub
parent ebbecbd9b5
commit 376b2013fe
6 changed files with 103 additions and 35 deletions
+8 -2
View File
@@ -14,17 +14,19 @@ import (
"strconv"
)
//go:generate go run golang.org/x/tools/cmd/stringer -type=Variant -trimprefix=Variant -linecomment
// Variant specifies which GUID variant (or "type") of the GUID. It determines
// how the entirety of the rest of the GUID is interpreted.
type Variant uint8
// The variants specified by RFC 4122.
// The variants specified by RFC 4122 section 4.1.1.
const (
// VariantUnknown specifies a GUID variant which does not conform to one of
// the variant encodings specified in RFC 4122.
VariantUnknown Variant = iota
VariantNCS
VariantRFC4122
VariantRFC4122 // RFC 4122
VariantMicrosoft
VariantFuture
)
@@ -34,6 +36,10 @@ const (
// hash of an input string.
type Version uint8
func (v Version) String() string {
return strconv.FormatUint(uint64(v), 10)
}
var _ = (encoding.TextMarshaler)(GUID{})
var _ = (encoding.TextUnmarshaler)(&GUID{})
+35 -28
View File
@@ -46,27 +46,29 @@ func Test_Variant(t *testing.T) {
{mustFromString(t, "f5cbc1a9-4cba-45a0-ffdd-b6761fc7dcc0"), VariantFuture},
}
for _, tc := range testCases {
actualVariant := tc.g.Variant()
if actualVariant != tc.v {
t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g)
}
t.Run(tc.v.String()+"/"+tc.g.String(), func(t *testing.T) {
actualVariant := tc.g.Variant()
if actualVariant != tc.v {
t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g)
}
})
}
}
func Test_SetVariant(t *testing.T) {
testCases := []Variant{
VariantNCS,
VariantRFC4122,
VariantMicrosoft,
VariantFuture,
}
g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
for _, tc := range testCases {
t.Logf("Test case: %d", tc)
g.setVariant(tc)
if g.Variant() != tc {
t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", tc, g.Variant())
for i := 0; i < len(_Variant_index)-1; i++ {
v := Variant(i)
if v == VariantUnknown {
// Unknown is not a valid variant
continue
}
t.Run(v.String(), func(t *testing.T) {
g.setVariant(v)
if g.Variant() != v {
t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", v, g.Variant())
}
})
}
}
@@ -83,22 +85,25 @@ func Test_Version(t *testing.T) {
{mustFromString(t, "f5cbc1a9-4cba-55a0-0fdd-b6761fc7dcc0"), 5},
}
for _, tc := range testCases {
actualVersion := tc.g.Version()
if actualVersion != tc.v {
t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g)
}
t.Run(tc.v.String()+"-"+tc.g.String(), func(t *testing.T) {
actualVersion := tc.g.Version()
if actualVersion != tc.v {
t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g)
}
})
}
}
func Test_SetVersion(t *testing.T) {
g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
for tc := 0; tc < 16; tc++ {
t.Logf("Test case: %d", tc)
v := Version(tc)
g.setVersion(v)
if g.Version() != v {
t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version())
}
t.Run(v.String(), func(t *testing.T) {
g.setVersion(v)
if g.Version() != v {
t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version())
}
})
}
}
@@ -160,10 +165,12 @@ func Test_V5KnownValues(t *testing.T) {
},
}
for _, tc := range testCases {
g := mustNewV5(t, tc.ns, []byte(tc.name))
if g != tc.g {
t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g)
}
t.Run(tc.name, func(t *testing.T) {
g := mustNewV5(t, tc.ns, []byte(tc.name))
if g != tc.g {
t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g)
}
})
}
}
+27
View File
@@ -0,0 +1,27 @@
// Code generated by "stringer -type=Variant -trimprefix=Variant -linecomment"; DO NOT EDIT.
package guid
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[VariantUnknown-0]
_ = x[VariantNCS-1]
_ = x[VariantRFC4122-2]
_ = x[VariantMicrosoft-3]
_ = x[VariantFuture-4]
}
const _Variant_name = "UnknownNCSRFC 4122MicrosoftFuture"
var _Variant_index = [...]uint8{0, 7, 10, 18, 27, 33}
func (i Variant) String() string {
if i >= Variant(len(_Variant_index)-1) {
return "Variant(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _Variant_name[_Variant_index[i]:_Variant_index[i+1]]
}