Skip to content

Commit 376b201

Browse files
authored
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 <[email protected]> Signed-off-by: Hamza El-Saawy <[email protected]>
1 parent ebbecbd commit 376b201

6 files changed

Lines changed: 103 additions & 35 deletions

File tree

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ go 1.17
44

55
require (
66
github.com/sirupsen/logrus v1.7.0
7-
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150
7+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f
8+
golang.org/x/tools v0.1.12
89
)
910

10-
require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
11+
require golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect

go.sum

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,30 @@ github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM
66
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
77
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
88
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
9-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
9+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
10+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
11+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
12+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
13+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
14+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
15+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
16+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
17+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1018
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
1120
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12-
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
13-
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
21+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
22+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
25+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
27+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
28+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
29+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
30+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
31+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
32+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
33+
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
34+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
35+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/guid/guid.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ import (
1414
"strconv"
1515
)
1616

17+
//go:generate go run golang.org/x/tools/cmd/stringer -type=Variant -trimprefix=Variant -linecomment
18+
1719
// Variant specifies which GUID variant (or "type") of the GUID. It determines
1820
// how the entirety of the rest of the GUID is interpreted.
1921
type Variant uint8
2022

21-
// The variants specified by RFC 4122.
23+
// The variants specified by RFC 4122 section 4.1.1.
2224
const (
2325
// VariantUnknown specifies a GUID variant which does not conform to one of
2426
// the variant encodings specified in RFC 4122.
2527
VariantUnknown Variant = iota
2628
VariantNCS
27-
VariantRFC4122
29+
VariantRFC4122 // RFC 4122
2830
VariantMicrosoft
2931
VariantFuture
3032
)
@@ -34,6 +36,10 @@ const (
3436
// hash of an input string.
3537
type Version uint8
3638

39+
func (v Version) String() string {
40+
return strconv.FormatUint(uint64(v), 10)
41+
}
42+
3743
var _ = (encoding.TextMarshaler)(GUID{})
3844
var _ = (encoding.TextUnmarshaler)(&GUID{})
3945

pkg/guid/guid_test.go

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,29 @@ func Test_Variant(t *testing.T) {
4646
{mustFromString(t, "f5cbc1a9-4cba-45a0-ffdd-b6761fc7dcc0"), VariantFuture},
4747
}
4848
for _, tc := range testCases {
49-
actualVariant := tc.g.Variant()
50-
if actualVariant != tc.v {
51-
t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g)
52-
}
49+
t.Run(tc.v.String()+"/"+tc.g.String(), func(t *testing.T) {
50+
actualVariant := tc.g.Variant()
51+
if actualVariant != tc.v {
52+
t.Fatalf("Variant is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVariant, tc.g)
53+
}
54+
})
5355
}
5456
}
5557

5658
func Test_SetVariant(t *testing.T) {
57-
testCases := []Variant{
58-
VariantNCS,
59-
VariantRFC4122,
60-
VariantMicrosoft,
61-
VariantFuture,
62-
}
6359
g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
64-
for _, tc := range testCases {
65-
t.Logf("Test case: %d", tc)
66-
g.setVariant(tc)
67-
if g.Variant() != tc {
68-
t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", tc, g.Variant())
60+
for i := 0; i < len(_Variant_index)-1; i++ {
61+
v := Variant(i)
62+
if v == VariantUnknown {
63+
// Unknown is not a valid variant
64+
continue
6965
}
66+
t.Run(v.String(), func(t *testing.T) {
67+
g.setVariant(v)
68+
if g.Variant() != v {
69+
t.Fatalf("Variant is incorrect.\nExpected: %d\nActual: %d", v, g.Variant())
70+
}
71+
})
7072
}
7173
}
7274

@@ -83,22 +85,25 @@ func Test_Version(t *testing.T) {
8385
{mustFromString(t, "f5cbc1a9-4cba-55a0-0fdd-b6761fc7dcc0"), 5},
8486
}
8587
for _, tc := range testCases {
86-
actualVersion := tc.g.Version()
87-
if actualVersion != tc.v {
88-
t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g)
89-
}
88+
t.Run(tc.v.String()+"-"+tc.g.String(), func(t *testing.T) {
89+
actualVersion := tc.g.Version()
90+
if actualVersion != tc.v {
91+
t.Fatalf("Version is not correct.\nExpected: %d\nActual: %d\nGUID: %s", tc.v, actualVersion, tc.g)
92+
}
93+
})
9094
}
9195
}
9296

9397
func Test_SetVersion(t *testing.T) {
9498
g := mustFromString(t, "f5cbc1a9-4cba-45a0-bfdd-b6761fc7dcc0")
9599
for tc := 0; tc < 16; tc++ {
96-
t.Logf("Test case: %d", tc)
97100
v := Version(tc)
98-
g.setVersion(v)
99-
if g.Version() != v {
100-
t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version())
101-
}
101+
t.Run(v.String(), func(t *testing.T) {
102+
g.setVersion(v)
103+
if g.Version() != v {
104+
t.Fatalf("Version is incorrect.\nExpected: %d\nActual: %d", v, g.Version())
105+
}
106+
})
102107
}
103108
}
104109

@@ -160,10 +165,12 @@ func Test_V5KnownValues(t *testing.T) {
160165
},
161166
}
162167
for _, tc := range testCases {
163-
g := mustNewV5(t, tc.ns, []byte(tc.name))
164-
if g != tc.g {
165-
t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g)
166-
}
168+
t.Run(tc.name, func(t *testing.T) {
169+
g := mustNewV5(t, tc.ns, []byte(tc.name))
170+
if g != tc.g {
171+
t.Fatalf("GUIDs are not equal.\nExpected: %s\nActual: %s", tc.g, g)
172+
}
173+
})
167174
}
168175
}
169176

pkg/guid/variant_string.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build tools
2+
3+
package winio
4+
5+
import _ "golang.org/x/tools/cmd/stringer"

0 commit comments

Comments
 (0)