Skip to content

Commit

Permalink
GUID tests to use t.Run (#256)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
helsaawy authored Aug 29, 2022
1 parent ebbecbd commit 376b201
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 35 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ go 1.17

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

require golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
require golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
28 changes: 25 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
10 changes: 8 additions & 2 deletions pkg/guid/guid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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{})

Expand Down
63 changes: 35 additions & 28 deletions pkg/guid/guid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}

Expand All @@ -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())
}
})
}
}

Expand Down Expand Up @@ -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)
}
})
}
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/guid/variant_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build tools

package winio

import _ "golang.org/x/tools/cmd/stringer"

0 comments on commit 376b201

Please sign in to comment.