Skip to content

Commit 2e9ad0b

Browse files
authored
Merge pull request #227 from aqyuki/improve-assert-test-coverage
Improve the test coverage about `DefaultVerifier`
2 parents 886cdfa + 68baa92 commit 2e9ad0b

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

assert_test.go

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package spectest
22

33
import (
4+
"fmt"
45
"net/http"
56
"testing"
67
)
78

9+
type mockTestingT struct{}
10+
11+
func (m *mockTestingT) Errorf(format string, args ...interface{}) {}
12+
func (m *mockTestingT) Fatal(args ...interface{}) {}
13+
func (m *mockTestingT) Fatalf(format string, args ...interface{}) {}
14+
func (m *mockTestingT) Name() string { return "mock" }
15+
816
func TestApiTestAssertStatusCodes(t *testing.T) {
917
tests := []struct {
1018
responseStatus []int
@@ -30,3 +38,215 @@ func TestApiTestAssertStatusCodes(t *testing.T) {
3038
}
3139
}
3240
}
41+
42+
func Test_DefaultVerifier_True(t *testing.T) {
43+
t.Parallel()
44+
verifier := &DefaultVerifier{}
45+
mock := &mockTestingT{}
46+
tests := []struct {
47+
name string
48+
args bool
49+
want bool
50+
}{
51+
{
52+
name: "should return true",
53+
args: true,
54+
want: true,
55+
},
56+
{
57+
name: "should return false",
58+
args: false,
59+
want: false,
60+
},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
actual := verifier.True(mock, tt.args)
66+
if actual != tt.want {
67+
t.Fatalf("Expected %t but received %t", actual, tt.want)
68+
}
69+
})
70+
}
71+
}
72+
73+
func Test_DefaultVerifier_JSONEq(t *testing.T) {
74+
t.Parallel()
75+
76+
verifier := &DefaultVerifier{}
77+
mock := &mockTestingT{}
78+
79+
type args struct {
80+
expected string
81+
actual string
82+
}
83+
84+
tests := []struct {
85+
name string
86+
args args
87+
want bool
88+
}{
89+
{
90+
name: "should return true",
91+
args: args{
92+
expected: `{"name":"John","age":30,"car":null}`,
93+
actual: `{"name":"John","age":30,"car":null}`,
94+
},
95+
want: true,
96+
},
97+
{
98+
name: "should failure with different values",
99+
args: args{
100+
expected: `{"name":"John","age":30,"car":null}`,
101+
actual: `{"name":"John","age":31,"car":null}`,
102+
},
103+
want: false,
104+
},
105+
{
106+
name: "should failure to parse expected",
107+
args: args{
108+
expected: `{"name":"John","age":30,"car":null`,
109+
actual: `{"name":"John","age":30,"car":null}`,
110+
},
111+
want: false,
112+
},
113+
{
114+
name: "should failure to parse actual",
115+
args: args{
116+
expected: `{"name":"John","age":30,"car":null}`,
117+
actual: `{"name":"John","age":30,"car":null`,
118+
},
119+
want: false,
120+
},
121+
}
122+
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
actual := verifier.JSONEq(mock, tt.args.expected, tt.args.actual)
126+
if actual != tt.want {
127+
t.Fatalf("Expected %t but received %t", actual, tt.want)
128+
}
129+
})
130+
}
131+
}
132+
133+
func Test_DefaultVerifier_Equal(t *testing.T) {
134+
t.Parallel()
135+
136+
verifier := &DefaultVerifier{}
137+
mock := &mockTestingT{}
138+
139+
var notOperationFunc = func() {}
140+
141+
type args struct {
142+
expected interface{}
143+
actual interface{}
144+
}
145+
146+
tests := []struct {
147+
name string
148+
args args
149+
want bool
150+
}{
151+
{
152+
name: "should return true",
153+
args: args{
154+
expected: 1,
155+
actual: 1,
156+
},
157+
want: true,
158+
},
159+
{
160+
name: "should return false because not operation function was given",
161+
args: args{
162+
expected: notOperationFunc,
163+
actual: notOperationFunc,
164+
},
165+
want: false,
166+
},
167+
{
168+
name: "should return false because different values",
169+
args: args{
170+
expected: 1,
171+
actual: 2,
172+
},
173+
want: false,
174+
},
175+
}
176+
177+
for _, tt := range tests {
178+
t.Run(tt.name, func(t *testing.T) {
179+
actual := verifier.Equal(mock, tt.args.expected, tt.args.actual)
180+
if actual != tt.want {
181+
t.Fatalf("Expected %t but received %t", actual, tt.want)
182+
}
183+
})
184+
}
185+
}
186+
187+
func Test_DefaultVerifier_Fail(t *testing.T) {
188+
t.Parallel()
189+
190+
verifier := &DefaultVerifier{}
191+
mock := &mockTestingT{}
192+
193+
tests := []struct {
194+
name string
195+
args []interface{}
196+
t TestingT
197+
}{
198+
{
199+
// FIXME: change the name of this test more better
200+
name: "pat1",
201+
args: []interface{}{},
202+
t: mock,
203+
},
204+
{
205+
name: "pat2",
206+
args: []interface{}{"foo"},
207+
t: mock,
208+
},
209+
}
210+
211+
for _, tt := range tests {
212+
t.Run(tt.name, func(t *testing.T) {
213+
res := verifier.Fail(tt.t, tt.name, tt.args...)
214+
if res {
215+
t.Fatal("Expected false but received true")
216+
}
217+
})
218+
}
219+
}
220+
221+
func Test_DefaultVerifier_NoError(t *testing.T) {
222+
t.Parallel()
223+
224+
verifier := &DefaultVerifier{}
225+
mock := &mockTestingT{}
226+
227+
tests := []struct {
228+
name string
229+
args error
230+
want bool
231+
}{
232+
{
233+
name: "should return true",
234+
args: nil,
235+
want: true,
236+
},
237+
{
238+
name: "should return false",
239+
args: fmt.Errorf("error"),
240+
want: false,
241+
},
242+
}
243+
244+
for _, tt := range tests {
245+
t.Run(tt.name, func(t *testing.T) {
246+
actual := verifier.NoError(mock, tt.args)
247+
if actual != tt.want {
248+
t.Fatalf("Expected %t but received %t", actual, tt.want)
249+
}
250+
})
251+
}
252+
}

0 commit comments

Comments
 (0)