Skip to content

Commit fd07901

Browse files
authored
test: added tests to augment coverage (enable/stubs, yaml) (#60)
* test: added tests to cover theme customization in enable/stubs * test(yaml): added coverage for failing YAML cases --------- Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 126a469 commit fd07901

File tree

4 files changed

+409
-34
lines changed

4 files changed

+409
-34
lines changed

enable/stubs/colors/enable_colors_test.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,56 @@
44
package colors
55

66
import (
7+
"fmt"
78
"testing"
89

910
target "github.com/go-openapi/testify/v2/assert"
1011
colorstub "github.com/go-openapi/testify/v2/internal/assertions/enable/colors"
1112
)
1213

1314
func TestEnableColors(t *testing.T) {
14-
t.Parallel()
15+
// Test execution is serialized on the sensitive package initialization section
16+
//
17+
// Proper test coverage of Enable() is assured by the testintegration package.
1518

19+
for _, opt := range []Option{
20+
WithSanitizedTheme("light"),
21+
WithDark(),
22+
WithLight(),
23+
WithTheme(ThemeDark),
24+
} {
25+
mock := new(mockT)
26+
target.NotPanics(mock, func() {
27+
testEnableWithLock(opt) // executed serially
28+
29+
_ = target.JSONEq(mock, `{"hello": "world", "foo": "bar"}`, `{"hello": "worldwide", "foo": "bar"}`)
30+
})
31+
// we may call several times with different options, but only the first setup is going to be used:
32+
// colorization is set lazily with a sync.Once global by the consuming package.
33+
t.Log(mock.errorString())
34+
}
35+
}
36+
37+
func testEnableWithLock(opt Option) {
1638
colorstub.Enable(
1739
func() []colorstub.Option {
1840
return []colorstub.Option{
1941
colorstub.WithEnable(true),
20-
colorstub.WithSanitizedTheme("light"),
42+
opt,
2143
}
2244
})
45+
}
46+
47+
type mockT struct {
48+
errorFmt string
49+
args []any
50+
}
51+
52+
func (m *mockT) Errorf(format string, args ...any) {
53+
m.errorFmt = format
54+
m.args = args
55+
}
2356

24-
mock := new(testing.T)
25-
target.NotPanics(mock, func() {
26-
_ = target.JSONEq(mock, `{"hello": "world", "foo": "bar"}`, `{"hello": "worldwide", "foo": "bar"}`)
27-
})
57+
func (m *mockT) errorString() string {
58+
return fmt.Sprintf(m.errorFmt, m.args...)
2859
}

enable/yaml/forward_assertions_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,69 @@ func TestYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
123123
t.Error("YAMLEq should return false")
124124
}
125125
}
126+
127+
func TestYAMLEqBytesWrapper(t *testing.T) {
128+
t.Parallel()
129+
130+
t.Run("should pass", func(t *testing.T) {
131+
t.Parallel()
132+
133+
assert := target.New(new(testing.T))
134+
if !assert.YAMLEqBytes([]byte(expectedYAML), []byte(actualYAML)) {
135+
t.Error("YAMLEqBytes should return true")
136+
}
137+
})
138+
139+
t.Run("should fail", func(t *testing.T) {
140+
t.Parallel()
141+
142+
assert := target.New(new(testing.T))
143+
if assert.YAMLEqBytes([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`)) {
144+
t.Error("YAMLEqBytes should return false")
145+
}
146+
})
147+
}
148+
149+
func TestYAMLEqfWrapper(t *testing.T) {
150+
t.Parallel()
151+
152+
t.Run("should pass", func(t *testing.T) {
153+
t.Parallel()
154+
155+
assert := target.New(new(testing.T))
156+
if !assert.YAMLEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "equivalent") {
157+
t.Error("YAMLEqf should return true")
158+
}
159+
})
160+
161+
t.Run("should fail", func(t *testing.T) {
162+
t.Parallel()
163+
164+
assert := target.New(new(testing.T))
165+
if assert.YAMLEqf(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "not equivalent") {
166+
t.Error("YAMLEqf should return false")
167+
}
168+
})
169+
}
170+
171+
func TestYAMLEqBytesfWrapper(t *testing.T) {
172+
t.Parallel()
173+
174+
t.Run("should pass", func(t *testing.T) {
175+
t.Parallel()
176+
177+
assert := target.New(new(testing.T))
178+
if !assert.YAMLEqBytesf([]byte(expectedYAML), []byte(actualYAML), yamlCheckMsg, "equivalent bytes") {
179+
t.Error("YAMLEqBytesf should return true")
180+
}
181+
})
182+
183+
t.Run("should fail", func(t *testing.T) {
184+
t.Parallel()
185+
186+
assert := target.New(new(testing.T))
187+
if assert.YAMLEqBytesf([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`), yamlCheckMsg, "not equivalent bytes") {
188+
t.Error("YAMLEqBytesf should return false")
189+
}
190+
})
191+
}

enable/yaml/forward_requirements_test.go

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func TestRequireYAMLEqWrapper_EqualYAMLString(t *testing.T) {
1313
t.Parallel()
1414

15-
mock := new(MockT)
15+
mock := new(mockT)
1616
mockRequire := target.New(mock)
1717

1818
mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`)
@@ -24,7 +24,7 @@ func TestRequireYAMLEqWrapper_EqualYAMLString(t *testing.T) {
2424
func TestRequireYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
2525
t.Parallel()
2626

27-
mock := new(MockT)
27+
mock := new(mockT)
2828
mockRequire := target.New(mock)
2929

3030
mockRequire.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
@@ -36,7 +36,7 @@ func TestRequireYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
3636
func TestRequireYAMLEqWrapper_HashOfArraysAndHashes(t *testing.T) {
3737
t.Parallel()
3838

39-
mock := new(MockT)
39+
mock := new(mockT)
4040
mockRequire := target.New(mock)
4141

4242
expected := `
@@ -74,7 +74,7 @@ array:
7474
func TestRequireYAMLEqWrapper_Array(t *testing.T) {
7575
t.Parallel()
7676

77-
mock := new(MockT)
77+
mock := new(mockT)
7878
mockRequire := target.New(mock)
7979

8080
mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`)
@@ -86,7 +86,7 @@ func TestRequireYAMLEqWrapper_Array(t *testing.T) {
8686
func TestRequireYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
8787
t.Parallel()
8888

89-
mock := new(MockT)
89+
mock := new(mockT)
9090
mockRequire := target.New(mock)
9191

9292
mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`)
@@ -98,7 +98,7 @@ func TestRequireYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
9898
func TestRequireYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
9999
t.Parallel()
100100

101-
mock := new(MockT)
101+
mock := new(mockT)
102102
mockRequire := target.New(mock)
103103

104104
mockRequire.YAMLEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`)
@@ -110,7 +110,7 @@ func TestRequireYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
110110
func TestRequireYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
111111
t.Parallel()
112112

113-
mock := new(MockT)
113+
mock := new(mockT)
114114
mockRequire := target.New(mock)
115115

116116
mockRequire.YAMLEq(`{"foo": "bar"}`, "Simple String")
@@ -122,7 +122,7 @@ func TestRequireYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
122122
func TestRequireYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
123123
t.Parallel()
124124

125-
mock := new(MockT)
125+
mock := new(mockT)
126126
mockRequire := target.New(mock)
127127

128128
mockRequire.YAMLEq("Simple String", `{"foo": "bar", "hello": "world"}`)
@@ -134,7 +134,7 @@ func TestRequireYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
134134
func TestRequireYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
135135
t.Parallel()
136136

137-
mock := new(MockT)
137+
mock := new(mockT)
138138
mockRequire := target.New(mock)
139139

140140
mockRequire.YAMLEq("Simple String", "Simple String")
@@ -146,11 +146,95 @@ func TestRequireYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
146146
func TestRequireYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
147147
t.Parallel()
148148

149-
mock := new(MockT)
149+
mock := new(mockT)
150150
mockRequire := target.New(mock)
151151

152152
mockRequire.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`)
153153
if !mock.Failed {
154154
t.Error("Check should fail")
155155
}
156156
}
157+
158+
func TestRequireYAMLEqBytesWrapper(t *testing.T) {
159+
t.Parallel()
160+
161+
t.Run("should pass", func(t *testing.T) {
162+
t.Parallel()
163+
164+
mock := new(mockT)
165+
mockRequire := target.New(mock)
166+
167+
mockRequire.YAMLEqBytes([]byte(expectedYAML), []byte(actualYAML))
168+
if mock.Failed {
169+
t.Error("Check should pass")
170+
}
171+
})
172+
173+
t.Run("should fail", func(t *testing.T) {
174+
t.Parallel()
175+
176+
mock := new(mockT)
177+
mockRequire := target.New(mock)
178+
179+
mockRequire.YAMLEqBytes([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`))
180+
if !mock.Failed {
181+
t.Error("Check should fail")
182+
}
183+
})
184+
}
185+
186+
func TestRequireYAMLEqfWrapper(t *testing.T) {
187+
t.Parallel()
188+
189+
t.Run("should pass", func(t *testing.T) {
190+
t.Parallel()
191+
192+
mock := new(mockT)
193+
mockRequire := target.New(mock)
194+
195+
mockRequire.YAMLEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "equivalent")
196+
if mock.Failed {
197+
t.Error("Check should pass")
198+
}
199+
})
200+
201+
t.Run("should fail", func(t *testing.T) {
202+
t.Parallel()
203+
204+
mock := new(mockT)
205+
mockRequire := target.New(mock)
206+
207+
mockRequire.YAMLEqf(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, yamlCheckMsg, "not equivalent")
208+
if !mock.Failed {
209+
t.Error("Check should fail")
210+
}
211+
})
212+
}
213+
214+
func TestRequireYAMLEqBytesfWrapper(t *testing.T) {
215+
t.Parallel()
216+
217+
t.Run("should pass", func(t *testing.T) {
218+
t.Parallel()
219+
220+
mock := new(mockT)
221+
mockRequire := target.New(mock)
222+
223+
mockRequire.YAMLEqBytesf([]byte(expectedYAML), []byte(actualYAML), yamlCheckMsg, "equivalent bytes")
224+
if mock.Failed {
225+
t.Error("Check should pass")
226+
}
227+
})
228+
229+
t.Run("should fail", func(t *testing.T) {
230+
t.Parallel()
231+
232+
mock := new(mockT)
233+
mockRequire := target.New(mock)
234+
235+
mockRequire.YAMLEqBytesf([]byte(`{"foo": "bar"}`), []byte(`{"foo": "bar", "hello": "world"}`), yamlCheckMsg, "not equivalent bytes")
236+
if !mock.Failed {
237+
t.Error("Check should fail")
238+
}
239+
})
240+
}

0 commit comments

Comments
 (0)