Skip to content

Commit 7a2c08e

Browse files
committed
Fix test of cjson.encodeCanonical
Signed-off-by: Aditya Sirish <[email protected]>
1 parent 976737f commit 7a2c08e

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

cjson/canonicaljson_test.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@ import (
55
"fmt"
66
"strings"
77
"testing"
8+
9+
"github.com/stretchr/testify/assert"
810
)
911

10-
type KeyVal struct {
12+
type keyVal struct {
1113
Private string `json:"private"`
1214
Public string `json:"public"`
1315
Certificate string `json:"certificate,omitempty"`
1416
}
1517

16-
type Key struct {
18+
type key struct {
1719
KeyID string `json:"keyid"`
1820
KeyIDHashAlgorithms []string `json:"keyid_hash_algorithms"`
1921
KeyType string `json:"keytype"`
20-
KeyVal KeyVal `json:"keyval"`
22+
KeyVal keyVal `json:"keyval"`
2123
Scheme string `json:"scheme"`
2224
}
2325

2426
func TestEncodeCanonical(t *testing.T) {
2527
objects := []interface{}{
26-
Key{},
27-
Key{
28-
KeyVal: KeyVal{
28+
key{},
29+
key{
30+
KeyVal: keyVal{
2931
Private: "priv",
3032
Public: "pub",
3133
},
@@ -42,8 +44,8 @@ func TestEncodeCanonical(t *testing.T) {
4244
"int2": float64(42),
4345
"string": `\"`,
4446
},
45-
Key{
46-
KeyVal: KeyVal{
47+
key{
48+
KeyVal: keyVal{
4749
Certificate: "cert",
4850
Private: "priv",
4951
Public: "pub",
@@ -91,21 +93,22 @@ func TestEncodeCanonicalErr(t *testing.T) {
9193
}
9294
}
9395

94-
func TestencodeCanonical(t *testing.T) {
95-
expectedError := "Can't canonicalize"
96+
func TestEncodeCanonicalHelper(t *testing.T) {
97+
defer func() {
98+
if r := recover(); r == nil {
99+
t.Error("encodeCanonical did not panic as expected")
100+
}
101+
}()
96102

97103
objects := []interface{}{
98-
TestencodeCanonical,
99-
[]interface{}{TestencodeCanonical},
104+
TestEncodeCanonicalHelper,
105+
[]interface{}{TestEncodeCanonicalHelper},
100106
}
101107

102108
for i := 0; i < len(objects); i++ {
103109
var result strings.Builder
104110
err := encodeCanonical(objects[i], &result)
105-
if err == nil || !strings.Contains(err.Error(), expectedError) {
106-
t.Errorf("EncodeCanonical returned '%s', expected '%s' error",
107-
err, expectedError)
108-
}
111+
assert.Nil(t, err)
109112
}
110113
}
111114

0 commit comments

Comments
 (0)