Skip to content

Commit bda11ae

Browse files
committed
More assert methods
1 parent 220a40b commit bda11ae

5 files changed

Lines changed: 49 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2424
- `ErrorIs`
2525
- `NotErrorIs`
2626
- `EqualJSON`
27+
- `JSON`
2728
- `Fail`
2829
- `Failf`

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ import (
2828
"testing"
2929
)
3030

31-
func TestSomething(t *testing.T) {
31+
func Test(t *testing.T) {
3232
// assert equality
3333
assert.Equal(t, 123, 123)
3434
// assert inequality
3535
assert.NotEqual(t, 123, 456)
3636
// assert object contains element
3737
assert.Contains(t, []int{1, 2, 3}, 2)
38+
// assert object can be marshall to given JSON string
39+
assert.JSON(t, &obj, `{"data":1}`)
3840
}
3941
```
4042

assert.go

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,25 @@ type Testing interface {
1111
Errorf(format string, args ...any)
1212
}
1313

14+
// Fail reports a failure message through
15+
func Fail(t Testing, message string) bool {
16+
t.Helper()
17+
18+
t.Errorf(message)
19+
20+
return false
21+
}
22+
23+
// Failf reports a failure formatted message through
24+
func Failf(t Testing, format string, args ...any) bool {
25+
t.Helper()
26+
27+
t.Errorf(format, args...)
28+
29+
return false
30+
}
31+
1432
// True asserts that the specified value is true.
15-
//
16-
// assert.True(t, condition)
1733
func True(t Testing, condition bool) bool {
1834
t.Helper()
1935

@@ -25,8 +41,6 @@ func True(t Testing, condition bool) bool {
2541
}
2642

2743
// False asserts that the specified value is false.
28-
//
29-
// assert.False(t, condition)
3044
func False(t Testing, condition bool) bool {
3145
t.Helper()
3246

@@ -39,8 +53,6 @@ func False(t Testing, condition bool) bool {
3953

4054
// Equal asserts that two objects are equal.
4155
//
42-
// assert.Equal(t, actual, expected)
43-
//
4456
// Pointer variable equality is determined based on the equality of the
4557
// referenced values (as opposed to the memory addresses).
4658
func Equal[T Comparable](t Testing, actual, expected T) bool {
@@ -55,8 +67,6 @@ func Equal[T Comparable](t Testing, actual, expected T) bool {
5567

5668
// NotEqual asserts that the specified values are NOT equal.
5769
//
58-
// assert.NotEqual(t, actual, expected)
59-
//
6070
// Pointer variable equality is determined based on the equality of the
6171
// referenced values (as opposed to the memory addresses).
6272
func NotEqual[T Comparable](t Testing, actual, expected T) bool {
@@ -71,8 +81,6 @@ func NotEqual[T Comparable](t Testing, actual, expected T) bool {
7181

7282
// EqualDelta asserts that two numeric values difference is less then delta.
7383
//
74-
// assert.EqualDelta(t, actual, expected, delta)
75-
//
7684
// Method panics if delta value is not positive.
7785
func EqualDelta[T Numeric](t Testing, actual, expected, delta T) bool {
7886
t.Helper()
@@ -86,8 +94,6 @@ func EqualDelta[T Numeric](t Testing, actual, expected, delta T) bool {
8694

8795
// Same asserts that two pointers reference the same object.
8896
//
89-
// assert.Same(t, actual, expected)
90-
//
9197
// Both arguments must be pointer variables. Pointer variable equality is
9298
// determined based on the equality of both type and value.
9399
func Same[T Reference](t Testing, actual, expected T) bool {
@@ -104,8 +110,6 @@ func Same[T Reference](t Testing, actual, expected T) bool {
104110

105111
// NotSame asserts that two pointers do NOT reference the same object.
106112
//
107-
// assert.NotSame(t, actual, expected)
108-
//
109113
// Both arguments must be pointer variables. Pointer variable equality is
110114
// determined based on the equality of both type and value.
111115
func NotSame[T Reference](t Testing, actual, expected T) bool {
@@ -121,8 +125,6 @@ func NotSame[T Reference](t Testing, actual, expected T) bool {
121125
}
122126

123127
// Length asserts that object have given length.
124-
//
125-
// assert.Length(t, object, expected)
126128
func Length[S Iterable[any]](t Testing, object S, expected int) bool {
127129
t.Helper()
128130

@@ -135,8 +137,6 @@ func Length[S Iterable[any]](t Testing, object S, expected int) bool {
135137

136138
// Contains asserts that object contains given element
137139
//
138-
// assert.Contains(t, object, element)
139-
//
140140
// Works with strings, arrays, slices, maps values and channels
141141
func Contains[S Iterable[E], E Comparable](t Testing, object S, element E) bool {
142142
t.Helper()
@@ -152,8 +152,6 @@ func Contains[S Iterable[E], E Comparable](t Testing, object S, element E) bool
152152

153153
// NotContains asserts that object do NOT contains given element
154154
//
155-
// assert.NotContains(t, object, element)
156-
//
157155
// Works with strings, arrays, slices, maps values and channels
158156
func NotContains[S Iterable[E], E Comparable](t Testing, object S, element E) bool {
159157
t.Helper()
@@ -168,8 +166,6 @@ func NotContains[S Iterable[E], E Comparable](t Testing, object S, element E) bo
168166
}
169167

170168
// Error asserts that error is NOT nil
171-
//
172-
// assert.Error(t, err)
173169
func Error(t Testing, err error) bool {
174170
t.Helper()
175171

@@ -181,8 +177,6 @@ func Error(t Testing, err error) bool {
181177
}
182178

183179
// NoError asserts that error is nil
184-
//
185-
// assert.NoError(t, err)
186180
func NoError(t Testing, err error) bool {
187181
t.Helper()
188182

@@ -194,8 +188,6 @@ func NoError(t Testing, err error) bool {
194188
}
195189

196190
// ErrorIs asserts that error is unwrappable to given target
197-
//
198-
// assert.ErrorIs(t, err)
199191
func ErrorIs(t Testing, err error, target error) bool {
200192
t.Helper()
201193

@@ -207,8 +199,6 @@ func ErrorIs(t Testing, err error, target error) bool {
207199
}
208200

209201
// NotErrorIs asserts that error is NOT unwrappable to given target
210-
//
211-
// assert.NotErrorIs(t, err)
212202
func NotErrorIs(t Testing, err error, target error) bool {
213203
t.Helper()
214204

@@ -220,8 +210,6 @@ func NotErrorIs(t Testing, err error, target error) bool {
220210
}
221211

222212
// EqualJSON asserts that JSON strings are equal
223-
//
224-
// assert.EqualJSON(t, actual, expected)
225213
func EqualJSON(t Testing, actual, expected string) bool {
226214
t.Helper()
227215

@@ -238,20 +226,11 @@ func EqualJSON(t Testing, actual, expected string) bool {
238226
return Equal(t, expectedJSON, actualJSON)
239227
}
240228

241-
// Fail reports a failure message through
242-
func Fail(t Testing, message string) bool {
229+
// JSON asserts that object can be marshall to expected JSON string
230+
func JSON(t Testing, actual any, expected string) bool {
243231
t.Helper()
244232

245-
t.Errorf(message)
246-
247-
return false
248-
}
233+
s, err := json.Marshal(actual)
249234

250-
// Failf reports a failure formatted message through
251-
func Failf(t Testing, format string, args ...any) bool {
252-
t.Helper()
253-
254-
t.Errorf(format, args...)
255-
256-
return false
235+
return NoError(t, err) && EqualJSON(t, string(s), expected)
257236
}

assert_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ func TestEqualJSON(t *testing.T) {
137137
testEqualJSON(t, `{"x":10, "y":16}`, `{"x":10,"y":16.000}`, true)
138138
}
139139

140+
func TestJSON(t *testing.T) {
141+
testJSON(t, map[string]any{"a": 1, "b": true, "c": "Hello"}, `{"a":1,"b":true,"c":"Hello"}`, true)
142+
testJSON(t, "Hello", "", false)
143+
testJSON(t, struct {
144+
A int `json:"a"`
145+
B string
146+
}{
147+
A: 1,
148+
B: "Hello",
149+
}, `{"a":1,"B":"Hello"}`, true)
150+
}
151+
140152
type testType string
141153

142154
type testStruct struct {
@@ -272,6 +284,15 @@ func testEqualJSON(t *testing.T, actual, expected string, result bool) {
272284
}
273285
}
274286

287+
func testJSON(t *testing.T, actual any, expected string, result bool) {
288+
t.Helper()
289+
290+
tt.Clear()
291+
if JSON(tt, actual, expected) != result {
292+
t.Errorf("JSON(%#v,%#v) should return %#v: %s", actual, expected, result, tt.LastError)
293+
}
294+
}
295+
275296
func ptr(i int) *int {
276297
return &i
277298
}

internal.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ func equalDelta[T Numeric](actual, expected, delta T) bool {
2323
actualFloat := float64(actual)
2424
expectedFloat := float64(expected)
2525

26-
fmt.Println(actual, expected)
2726
if math.IsNaN(actualFloat) && math.IsNaN(expectedFloat) {
2827
return true
2928
} else if math.IsNaN(actualFloat) || math.IsNaN(expectedFloat) {
3029
return false
3130
}
3231

3332
diff := expectedFloat - actualFloat
34-
d := float64(delta)
35-
fmt.Println(actual, expected, diff, d)
36-
if diff < -d || diff > d {
33+
deltaFloat := float64(delta)
34+
if diff < -deltaFloat || diff > deltaFloat {
3735
return false
3836
}
3937

0 commit comments

Comments
 (0)