Skip to content

Commit

Permalink
Merge pull request lestrrat-go#188 from lestrrat-go/topic/coverage
Browse files Browse the repository at this point in the history
Play the coverage game
  • Loading branch information
lestrrat authored May 5, 2020
2 parents 09c3c54 + 985731f commit 45a269f
Show file tree
Hide file tree
Showing 13 changed files with 582 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test:
go test -v -race ./...

cover:
go test -v -race -coverpkg=./... -coverprofile=coverage.out.tmp ./...
go test -v -tags debug0 -race -coverpkg=./... -coverprofile=coverage.out.tmp ./...
@# This is NOT cheating. tools to generate code don't need to be
@# included in the final result
@cat coverage.out.tmp | grep -v "internal/cmd" | grep -v "internal/codegen" > coverage.out
Expand Down
26 changes: 26 additions & 0 deletions jwa/compression_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestCompressionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for DEF`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "DEF", jwa.Deflate.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant NoCompress`, func(t *testing.T) {
t.Parallel()
var dst jwa.CompressionAlgorithm
Expand Down Expand Up @@ -70,4 +76,24 @@ func TestCompressionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for `, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "", jwa.NoCompress.String(), `stringified value matches`) {
return
}
})
t.Run(`bail out on random integer value`, func(t *testing.T) {
t.Parallel()
var dst jwa.CompressionAlgorithm
if !assert.Error(t, dst.Accept(1), `accept should fail`) {
return
}
})
t.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {
t.Parallel()
var dst jwa.CompressionAlgorithm
if !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {
return
}
})
}
50 changes: 50 additions & 0 deletions jwa/content_encryption_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestContentEncryptionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for A128CBC-HS256`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "A128CBC-HS256", jwa.A128CBC_HS256.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant A128GCM`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
Expand Down Expand Up @@ -70,6 +76,12 @@ func TestContentEncryptionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for A128GCM`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "A128GCM", jwa.A128GCM.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant A192CBC_HS384`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
Expand Down Expand Up @@ -100,6 +112,12 @@ func TestContentEncryptionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for A192CBC-HS384`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "A192CBC-HS384", jwa.A192CBC_HS384.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant A192GCM`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
Expand Down Expand Up @@ -130,6 +148,12 @@ func TestContentEncryptionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for A192GCM`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "A192GCM", jwa.A192GCM.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant A256CBC_HS512`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
Expand Down Expand Up @@ -160,6 +184,12 @@ func TestContentEncryptionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for A256CBC-HS512`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "A256CBC-HS512", jwa.A256CBC_HS512.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant A256GCM`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
Expand Down Expand Up @@ -190,4 +220,24 @@ func TestContentEncryptionAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for A256GCM`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "A256GCM", jwa.A256GCM.String(), `stringified value matches`) {
return
}
})
t.Run(`bail out on random integer value`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
if !assert.Error(t, dst.Accept(1), `accept should fail`) {
return
}
})
t.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {
t.Parallel()
var dst jwa.ContentEncryptionAlgorithm
if !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {
return
}
})
}
32 changes: 32 additions & 0 deletions jwa/elliptic_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestEllipticCurveAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for P-256`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "P-256", jwa.P256.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant P384`, func(t *testing.T) {
t.Parallel()
var dst jwa.EllipticCurveAlgorithm
Expand Down Expand Up @@ -70,6 +76,12 @@ func TestEllipticCurveAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for P-384`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "P-384", jwa.P384.String(), `stringified value matches`) {
return
}
})
t.Run(`accept jwa constant P521`, func(t *testing.T) {
t.Parallel()
var dst jwa.EllipticCurveAlgorithm
Expand Down Expand Up @@ -100,11 +112,31 @@ func TestEllipticCurveAlgorithm(t *testing.T) {
return
}
})
t.Run(`stringification for P-521`, func(t *testing.T) {
t.Parallel()
if !assert.Equal(t, "P-521", jwa.P521.String(), `stringified value matches`) {
return
}
})
t.Run(`do not accept invalid constant InvalidEllipticCurve`, func(t *testing.T) {
t.Parallel()
var dst jwa.EllipticCurveAlgorithm
if !assert.Error(t, dst.Accept(jwa.InvalidEllipticCurve), `accept should fail`) {
return
}
})
t.Run(`bail out on random integer value`, func(t *testing.T) {
t.Parallel()
var dst jwa.EllipticCurveAlgorithm
if !assert.Error(t, dst.Accept(1), `accept should fail`) {
return
}
})
t.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {
t.Parallel()
var dst jwa.EllipticCurveAlgorithm
if !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {
return
}
})
}
23 changes: 23 additions & 0 deletions jwa/internal/cmd/gentypes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,13 @@ func (t typ) GenerateTest() error {
fmt.Fprintf(&buf, "\nreturn")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n})")

fmt.Fprintf(&buf, "\nt.Run(`stringification for %s`, func(t *testing.T) {", e.value)
fmt.Fprintf(&buf, "\nt.Parallel()")
fmt.Fprintf(&buf, "\nif !assert.Equal(t, %#v, jwa.%s.String(), `stringified value matches`) {", e.value, e.name)
fmt.Fprintf(&buf, "\nreturn")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n})")
}

for _, e := range invalids {
Expand All @@ -493,6 +500,22 @@ func (t typ) GenerateTest() error {
fmt.Fprintf(&buf, "\n})")
}

fmt.Fprintf(&buf, "\nt.Run(`bail out on random integer value`, func(t *testing.T) {")
fmt.Fprintf(&buf, "\nt.Parallel()")
fmt.Fprintf(&buf, "\nvar dst jwa.%s", t.name)
fmt.Fprintf(&buf, "\nif !assert.Error(t, dst.Accept(1), `accept should fail`) {")
fmt.Fprintf(&buf, "\nreturn")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n})")

fmt.Fprintf(&buf, "\nt.Run(`do not accept invalid (totally made up) string value`, func(t *testing.T) {")
fmt.Fprintf(&buf, "\nt.Parallel()")
fmt.Fprintf(&buf, "\nvar dst jwa.%s", t.name)
fmt.Fprintf(&buf, "\nif !assert.Error(t, dst.Accept(`totallyInvfalidValue`), `accept should fail`) {")
fmt.Fprintf(&buf, "\nreturn")
fmt.Fprintf(&buf, "\n}")
fmt.Fprintf(&buf, "\n})")

fmt.Fprintf(&buf, "\n}")

formatted, err := imports.Process("", buf.Bytes(), nil)
Expand Down
Loading

0 comments on commit 45a269f

Please sign in to comment.