Skip to content

Commit 4b5729d

Browse files
committed
Use go-cmp to show diff in tests
1 parent 6146ca5 commit 4b5729d

File tree

5 files changed

+31
-26
lines changed

5 files changed

+31
-26
lines changed

audience_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package jwt_test
22

33
import (
44
"encoding/json"
5-
"reflect"
65
"testing"
76

87
"github.com/gbrlsnchs/jwt/v3"
8+
"github.com/google/go-cmp/cmp"
99
)
1010

1111
func TestAudienceMarshal(t *testing.T) {
@@ -20,7 +20,7 @@ func TestAudienceMarshal(t *testing.T) {
2020
if b, err = json.Marshal(v); err != nil {
2121
t.Fatal(err)
2222
}
23-
checkAudMarshal(t, "{}", b)
23+
checkAudMarshal(t, b, "{}")
2424

2525
})
2626

@@ -44,12 +44,12 @@ func TestAudienceMarshal(t *testing.T) {
4444
if b, err = tc.aud.MarshalJSON(); err != nil {
4545
t.Fatal(err)
4646
}
47-
checkAudMarshal(t, tc.expected, b)
47+
checkAudMarshal(t, b, tc.expected)
4848
}
4949
if b, err = json.Marshal(tc.aud); err != nil {
5050
t.Fatal(err)
5151
}
52-
checkAudMarshal(t, tc.expected, b)
52+
checkAudMarshal(t, b, tc.expected)
5353
})
5454
}
5555
}
@@ -69,23 +69,23 @@ func TestAudienceUnmarshal(t *testing.T) {
6969
if err := aud.UnmarshalJSON(tc.jstr); err != nil {
7070
t.Fatal(err)
7171
}
72-
checkAudUnmarshal(t, tc.expected, aud)
72+
checkAudUnmarshal(t, aud, tc.expected)
7373
if err := json.Unmarshal(tc.jstr, &aud); err != nil {
7474
t.Fatal(err)
7575
}
76-
checkAudUnmarshal(t, tc.expected, aud)
76+
checkAudUnmarshal(t, aud, tc.expected)
7777
})
7878
}
7979
}
8080

81-
func checkAudMarshal(t *testing.T, want string, got []byte) {
82-
if want != string(got) {
83-
t.Errorf("want %q, got %q", want, got)
81+
func checkAudMarshal(t *testing.T, got []byte, want string) {
82+
if string(got) != want {
83+
t.Errorf("jwt.Audience.Marshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
8484
}
8585
}
8686

87-
func checkAudUnmarshal(t *testing.T, want, got jwt.Audience) {
88-
if !reflect.DeepEqual(want, got) {
89-
t.Errorf("want %v, got %v", want, got)
87+
func checkAudUnmarshal(t *testing.T, got, want jwt.Audience) {
88+
if !cmp.Equal(got, want) {
89+
t.Errorf("jwt.Audience.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
9090
}
9191
}

internal/decode_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/gbrlsnchs/jwt/v3/internal"
8+
"github.com/google/go-cmp/cmp"
89
)
910

1011
var (
@@ -41,11 +42,11 @@ func TestDecode(t *testing.T) {
4142
dt decodeTest
4243
err = internal.Decode([]byte(b64), &dt)
4344
)
44-
if want, got := tc.errors, err != nil; want != got {
45+
if want, got := tc.errors, internal.ErrorAs(err, new(base64.CorruptInputError)); got != want {
4546
t.Fatalf("want %t, got %t: %v", want, got, err)
4647
}
47-
if want, got := tc.expected, dt.X; want != got {
48-
t.Errorf("want %q, got %q", want, got)
48+
if want, got := tc.expected, dt.X; got != want {
49+
t.Errorf("internal.Decode mismatch (-want +got):\n%s", cmp.Diff(want, got))
4950
}
5051
})
5152
}

time_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/gbrlsnchs/jwt/v3"
99
"github.com/gbrlsnchs/jwt/v3/internal"
10+
"github.com/google/go-cmp/cmp"
1011
)
1112

1213
func TestTimeMarshalJSON(t *testing.T) {
@@ -31,7 +32,7 @@ func TestTimeMarshalJSON(t *testing.T) {
3132
t.Fatal(err)
3233
}
3334
if want, got := tc.want, n; got != want {
34-
t.Errorf("want %d, got %d", want, got)
35+
t.Errorf("jwt.Time.Marshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
3536
}
3637
})
3738
}
@@ -45,9 +46,9 @@ func TestTimeUnmarshalJSON(t *testing.T) {
4546
isNil bool
4647
}{
4748
{now.Unix(), jwt.Time{now}, false},
48-
{internal.Epoch.Unix() - 1337, jwt.Time{internal.Epoch}, false},
49+
{internal.Epoch.Unix() - 0xDEAD, jwt.Time{internal.Epoch}, false},
4950
{internal.Epoch.Unix(), jwt.Time{internal.Epoch}, false},
50-
{internal.Epoch.Unix() + 1337, jwt.Time{internal.Epoch.Add(1337 * time.Second)}, false},
51+
{internal.Epoch.Unix() + 0xDEAD, jwt.Time{internal.Epoch.Add(0xDEAD * time.Second)}, false},
5152
{0, jwt.Time{}, true},
5253
}
5354
for _, tc := range testCases {
@@ -64,8 +65,8 @@ func TestTimeUnmarshalJSON(t *testing.T) {
6465
if err = tt.UnmarshalJSON(b); err != nil {
6566
t.Fatal(err)
6667
}
67-
if want, got := tc.want, tt; got.Unix() != want.Unix() {
68-
t.Errorf("want %d, got %d", want.Unix(), got.Unix())
68+
if want, got := tc.want.Unix(), tt.Unix(); got != want {
69+
t.Errorf("jwt.Time.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
6970
}
7071
})
7172
}

validators_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"time"
66

77
"github.com/gbrlsnchs/jwt/v3"
8+
"github.com/gbrlsnchs/jwt/v3/internal"
9+
"github.com/google/go-cmp/cmp"
810
)
911

1012
func TestValidators(t *testing.T) {
@@ -49,8 +51,8 @@ func TestValidators(t *testing.T) {
4951
}
5052
for _, tc := range testCases {
5153
t.Run(tc.claim, func(t *testing.T) {
52-
if want, got := tc.err, tc.vl(tc.pl); want != got {
53-
t.Errorf("want %v, got %v", want, got)
54+
if want, got := tc.err, tc.vl(tc.pl); !internal.ErrorIs(got, want) {
55+
t.Errorf(cmp.Diff(want, got))
5456
}
5557
})
5658
}

verify_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/gbrlsnchs/jwt/v3"
9+
"github.com/gbrlsnchs/jwt/v3/internal"
910
"github.com/google/go-cmp/cmp"
1011
)
1112

@@ -85,8 +86,8 @@ func TestVerify(t *testing.T) {
8586
v interface{}
8687
)
8788
_, err := jwt.Verify([]byte(token), jwt.None(), &v)
88-
if want, got := jwt.ErrNotJSONObject, err; got != want {
89-
t.Errorf("want %v, got %v", want, got)
89+
if want, got := jwt.ErrNotJSONObject, err; !internal.ErrorIs(got, want) {
90+
t.Errorf("jwt.Verify JSON payload mismatch (-want +got):\n%s", cmp.Diff(want, got))
9091
}
9192
})
9293
}
@@ -154,8 +155,8 @@ func TestValidatePayload(t *testing.T) {
154155
t.Fatal(err)
155156
}
156157
_, err = jwt.Verify(token, hs256, tc.pl, jwt.ValidatePayload(tc.pl, tc.vds...))
157-
if want, got := tc.err, err; got != want {
158-
t.Errorf("want %v, got %v", want, got)
158+
if want, got := tc.err, err; !internal.ErrorIs(got, want) {
159+
t.Errorf("jwt.Verify with validators mismatch (-want +got):\n%s", cmp.Diff(want, got))
159160
}
160161
})
161162
}

0 commit comments

Comments
 (0)