Skip to content

Commit e894f0f

Browse files
committed
feat: add True and NotTrue.
1 parent b393f99 commit e894f0f

File tree

3 files changed

+218
-4
lines changed

3 files changed

+218
-4
lines changed

builtin.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,47 @@ func NotPanicNow(t *testing.T, fn func(), message ...string) error {
107107

108108
return tryNotPanic(t, true, fn, message...)
109109
}
110+
111+
// True tests whether a value is truthy or not. It'll set the result to fail if the value is a
112+
// false value. For most types of value, a falsy value is the zero value for its type. For a
113+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
114+
// value is always falsy.
115+
func True(t *testing.T, val any, message ...string) error {
116+
t.Helper()
117+
118+
return tryTrue(t, false, val, message...)
119+
}
120+
121+
// TrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a
122+
// false value. For most types of value, a falsy value is the zero value for its type. For a
123+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
124+
// value is always falsy.
125+
//
126+
// The function will stop the execution if the value is falsy.
127+
func TrueNow(t *testing.T, val any, message ...string) error {
128+
t.Helper()
129+
130+
return tryTrue(t, true, val, message...)
131+
}
132+
133+
// NotTrue tests whether a value is truthy or not. It'll set the result to fail if the value is a
134+
// truthy value. For most types of value, a falsy value is the zero value for its type. For a
135+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
136+
// value is always falsy.
137+
func NotTrue(t *testing.T, val any, message ...string) error {
138+
t.Helper()
139+
140+
return tryNotTrue(t, false, val, message...)
141+
}
142+
143+
// NotTrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is
144+
// a truthy value. For most types of value, a falsy value is the zero value for its type. For a
145+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
146+
// value is always falsy.
147+
//
148+
// The function will stop the execution if the value is truthy.
149+
func NotTrueNow(t *testing.T, val any, message ...string) error {
150+
t.Helper()
151+
152+
return tryNotTrue(t, true, val, message...)
153+
}

equal.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,75 @@ func tryNotNil(t *testing.T, failedNow bool, val any, message ...string) error {
135135

136136
return err
137137
}
138+
139+
// True tests whether a value is truthy or not. It'll set the result to fail if the value is a
140+
// false value. For most types of value, a falsy value is the zero value for its type. For a
141+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
142+
// value is always falsy.
143+
func (a *Assertion) True(val any, message ...string) error {
144+
a.Helper()
145+
146+
return tryTrue(a.T, false, val, message...)
147+
}
148+
149+
// TrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a
150+
// false value. For most types of value, a falsy value is the zero value for its type. For a
151+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
152+
// value is always falsy.
153+
//
154+
// The function will stop the execution if the value is falsy.
155+
func (a *Assertion) TrueNow(val any, message ...string) error {
156+
a.Helper()
157+
158+
return tryTrue(a.T, true, val, message...)
159+
}
160+
161+
// NotTrue tests whether a value is truthy or not. It'll set the result to fail if the value is a
162+
// truthy value. For most types of value, a falsy value is the zero value for its type. For a
163+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
164+
// value is always falsy.
165+
func (a *Assertion) NotTrue(val any, message ...string) error {
166+
a.Helper()
167+
168+
return tryNotTrue(a.T, false, val, message...)
169+
}
170+
171+
// NotTrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is
172+
// a truthy value. For most types of value, a falsy value is the zero value for its type. For a
173+
// slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the
174+
// value is always falsy.
175+
//
176+
// The function will stop the execution if the value is truthy.
177+
func (a *Assertion) NotTrueNow(val any, message ...string) error {
178+
a.Helper()
179+
180+
return tryNotTrue(a.T, true, val, message...)
181+
}
182+
183+
// tryTrue try to testing a value is truthy or falsy, and it'll fail the value is falsy.
184+
func tryTrue(t *testing.T, failedNow bool, val any, message ...string) error {
185+
t.Helper()
186+
187+
if isTrue(val) {
188+
return nil
189+
}
190+
191+
err := newAssertionError("the expression evaluated to a falsy value")
192+
failed(t, err, failedNow)
193+
194+
return err
195+
}
196+
197+
// tryNotTrue try to testing a value is truthy or falsy, and it'll fail the value is truthy.
198+
func tryNotTrue(t *testing.T, failedNow bool, val any, message ...string) error {
199+
t.Helper()
200+
201+
if !isTrue(val) {
202+
return nil
203+
}
204+
205+
err := newAssertionError("the expression evaluated to a truthy value")
206+
failed(t, err, failedNow)
207+
208+
return err
209+
}

equal_test.go

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,16 @@ func testNil(t *testing.T, assertion *Assertion, v any, isNil bool) {
157157
func testNotNil(t *testing.T, assertion *Assertion, v any, isNil bool) {
158158
err := assertion.NotNil(v)
159159
if isNil && err == nil {
160-
t.Errorf("Nil(%v) = nil, want error", v)
160+
t.Errorf("NotNil(%v) = nil, want error", v)
161161
} else if !isNil && err != nil {
162-
t.Errorf("Nil(%v) = %v, want nil", v, err)
162+
t.Errorf("NotNil(%v) = %v, want nil", v, err)
163163
}
164164

165165
err = NotNil(assertion.T, v)
166166
if isNil && err == nil {
167-
t.Errorf("Nil(%v) = nil, want error", v)
167+
t.Errorf("NotNil(%v) = nil, want error", v)
168168
} else if !isNil && err != nil {
169-
t.Errorf("Nil(%v) = %v, want nil", v, err)
169+
t.Errorf("NotNil(%v) = %v, want nil", v, err)
170170
}
171171
}
172172

@@ -209,3 +209,101 @@ func testNotNilNow(t *testing.T, assertion *Assertion, v any, isNil bool) {
209209
t.Error("execution do not stopped, want stop")
210210
}
211211
}
212+
213+
func TestTrueAndNotTrue(t *testing.T) {
214+
mockT := new(testing.T)
215+
assert := New(mockT)
216+
217+
testTrueAndNotTrue(t, assert, nil, false)
218+
testTrueAndNotTrue(t, assert, []int{}, false)
219+
testTrueAndNotTrue(t, assert, []int{0}, true)
220+
testTrueAndNotTrue(t, assert, 0, false)
221+
testTrueAndNotTrue(t, assert, 1, true)
222+
testTrueAndNotTrue(t, assert, 0.0, false)
223+
testTrueAndNotTrue(t, assert, 1.0, true)
224+
testTrueAndNotTrue(t, assert, "", false)
225+
testTrueAndNotTrue(t, assert, "test", true)
226+
testTrueAndNotTrue(t, assert, func() {}, true)
227+
}
228+
229+
func testTrueAndNotTrue(t *testing.T, assertion *Assertion, v any, isTruthy bool) {
230+
testTrue(t, assertion, v, isTruthy)
231+
232+
testNotTrue(t, assertion, v, isTruthy)
233+
234+
testTrueNow(t, assertion, v, isTruthy)
235+
236+
testNotTrueNow(t, assertion, v, isTruthy)
237+
}
238+
239+
func testTrue(t *testing.T, assertion *Assertion, v any, isTruthy bool) {
240+
err := assertion.True(v)
241+
if isTruthy && err != nil {
242+
t.Errorf("True(%v) = %v, want nil", v, err)
243+
} else if !isTruthy && err == nil {
244+
t.Errorf("True(%v) = nil, want error", v)
245+
}
246+
247+
err = True(assertion.T, v)
248+
if isTruthy && err != nil {
249+
t.Errorf("True(%v) = %v, want nil", v, err)
250+
} else if !isTruthy && err == nil {
251+
t.Errorf("True(%v) = nil, want error", v)
252+
}
253+
}
254+
255+
func testNotTrue(t *testing.T, assertion *Assertion, v any, isTruthy bool) {
256+
err := assertion.NotTrue(v)
257+
if isTruthy && err == nil {
258+
t.Errorf("NotTrue(%v) = nil, want error", v)
259+
} else if !isTruthy && err != nil {
260+
t.Errorf("NotTrue(%v) = %v, want nil", v, err)
261+
}
262+
263+
err = NotTrue(assertion.T, v)
264+
if isTruthy && err == nil {
265+
t.Errorf("NotTrue(%v) = nil, want error", v)
266+
} else if !isTruthy && err != nil {
267+
t.Errorf("NotTrue(%v) = %v, want nil", v, err)
268+
}
269+
}
270+
271+
func testTrueNow(t *testing.T, assertion *Assertion, v any, isTruthy bool) {
272+
isTerminated := internal.CheckTermination(func() {
273+
assertion.TrueNow(v)
274+
})
275+
if isTruthy && isTerminated {
276+
t.Error("execution stopped, want do not stop")
277+
} else if !isTruthy && !isTerminated {
278+
t.Error("execution do not stopped, want stop")
279+
}
280+
281+
isTerminated = internal.CheckTermination(func() {
282+
TrueNow(assertion.T, v)
283+
})
284+
if isTruthy && isTerminated {
285+
t.Error("execution stopped, want do not stop")
286+
} else if !isTruthy && !isTerminated {
287+
t.Error("execution do not stopped, want stop")
288+
}
289+
}
290+
291+
func testNotTrueNow(t *testing.T, assertion *Assertion, v any, isTruthy bool) {
292+
isTerminated := internal.CheckTermination(func() {
293+
assertion.NotTrueNow(v)
294+
})
295+
if !isTruthy && isTerminated {
296+
t.Error("execution stopped, want do not stop")
297+
} else if isTruthy && !isTerminated {
298+
t.Error("execution do not stopped, want stop")
299+
}
300+
301+
isTerminated = internal.CheckTermination(func() {
302+
NotTrueNow(assertion.T, v)
303+
})
304+
if !isTruthy && isTerminated {
305+
t.Error("execution stopped, want do not stop")
306+
} else if isTruthy && !isTerminated {
307+
t.Error("execution do not stopped, want stop")
308+
}
309+
}

0 commit comments

Comments
 (0)