Skip to content

Commit fd8d939

Browse files
committed
New assert methods
1 parent c296499 commit fd8d939

6 files changed

Lines changed: 298 additions & 69 deletions

File tree

CHANGELOG.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,24 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

88

9-
## [Unreleased](https://github.com/gravitton/assert/compare/v0.1.0...master)
9+
## [Unreleased](https://github.com/gravitton/assert/compare/v1.0.0...master)
1010

1111

12-
## v0.1.0 (2025-08-03)
12+
## v1.0.0 (2025-08-05)
1313
### Added
14+
- Added new assert methods
15+
- `True`
16+
- `False`
17+
- `Equal`
18+
- `NotEqual`
19+
- `Same`
20+
- `NotSame`
21+
- `Length`
22+
- `Contains`
23+
- `NotContains`
24+
- `Error`
25+
- `NoError`
26+
- `ErrorIs`
27+
- `NotErrorIs`
28+
- `Fail`
29+
- `Failf`

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func TestSomething(t *testing.T) {
3333
assert.Equal(t, 123, 123)
3434
// assert inequality
3535
assert.NotEqual(t, 123, 456)
36+
// assert object contains element
37+
assert.Contains(t, []int{1, 2, 3}, 2)
3638
}
3739
```
3840

assert.go

Lines changed: 89 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
package assert
22

33
import (
4-
"reflect"
4+
"github.com/gravitton/assert/internal"
55
)
66

77
// Testing is an interface wrapper around *testing.T
88
type Testing interface {
9-
Errorf(format string, args ...any)
10-
}
11-
12-
// helper is an interface wrapper around *testing.T
13-
type helper interface {
149
Helper()
10+
Errorf(format string, args ...any)
1511
}
1612

1713
// True asserts that the specified value is true.
1814
//
1915
// assert.True(t, condition)
20-
func True(t Testing, actual bool) bool {
21-
if h, ok := t.(helper); ok {
22-
h.Helper()
23-
}
16+
func True(t Testing, condition bool) bool {
17+
t.Helper()
2418

25-
if !actual {
19+
if !internal.Assert(condition) {
2620
return Fail(t, "Should be true")
2721
}
2822

@@ -32,12 +26,10 @@ func True(t Testing, actual bool) bool {
3226
// False asserts that the specified value is false.
3327
//
3428
// assert.False(t, condition)
35-
func False(t Testing, actual bool) bool {
36-
if h, ok := t.(helper); ok {
37-
h.Helper()
38-
}
29+
func False(t Testing, condition bool) bool {
30+
t.Helper()
3931

40-
if actual {
32+
if internal.Assert(condition) {
4133
return Fail(t, "Should be false")
4234
}
4335

@@ -51,12 +43,10 @@ func False(t Testing, actual bool) bool {
5143
// Pointer variable equality is determined based on the equality of the
5244
// referenced values (as opposed to the memory addresses).
5345
func Equal(t Testing, actual, expected any) bool {
54-
if h, ok := t.(helper); ok {
55-
h.Helper()
56-
}
46+
t.Helper()
5747

58-
if !equal(actual, expected) {
59-
return Failf(t, "Should be equal: \nactual: %#v\n expected: %#v", actual, expected)
48+
if !internal.Equal(actual, expected) {
49+
return Failf(t, "Should be equal:\n object: %#v\nelement: %#v", actual, expected)
6050
}
6151

6252
return true
@@ -69,38 +59,26 @@ func Equal(t Testing, actual, expected any) bool {
6959
// Pointer variable equality is determined based on the equality of the
7060
// referenced values (as opposed to the memory addresses).
7161
func NotEqual(t Testing, actual, expected any) bool {
72-
if h, ok := t.(helper); ok {
73-
h.Helper()
74-
}
62+
t.Helper()
7563

76-
if equal(actual, expected) {
77-
return Failf(t, "Should not be equal: %#v", actual)
64+
if internal.Equal(actual, expected) {
65+
return Failf(t, "Should not be equal\n object: %#v", actual)
7866
}
7967

8068
return true
8169
}
8270

83-
func equal(actual, expected any) bool {
84-
if actual == nil || expected == nil {
85-
return actual == expected
86-
}
87-
88-
return reflect.DeepEqual(actual, expected)
89-
}
90-
9171
// Same asserts that two pointers reference the same object.
9272
//
9373
// assert.Same(t, actual, expected)
9474
//
9575
// Both arguments must be pointer variables. Pointer variable sameness is
9676
// determined based on the equality of both type and value.
9777
func Same(t Testing, actual, expected any) bool {
98-
if h, ok := t.(helper); ok {
99-
h.Helper()
100-
}
78+
t.Helper()
10179

102-
if !same(expected, actual) {
103-
return Failf(t, "Should be same: \nactual: %[1]p %#[1]v\n expected: %[2]p %#[2]v", actual, expected)
80+
if !internal.Same(expected, actual) {
81+
return Failf(t, "Should be same\n object: %[1]p %#[1]v\nelement: %[2]p %#[2]v", actual, expected)
10482
}
10583

10684
return true
@@ -113,37 +91,92 @@ func Same(t Testing, actual, expected any) bool {
11391
// Both arguments must be pointer variables. Pointer variable sameness is
11492
// determined based on the equality of both type and value.
11593
func NotSame(t Testing, actual, expected any) bool {
116-
if h, ok := t.(helper); ok {
117-
h.Helper()
94+
t.Helper()
95+
96+
if internal.Same(expected, actual) {
97+
return Failf(t, "Should not be same\n object: %[1]p %#[1]v", actual)
98+
}
99+
100+
return true
101+
}
102+
103+
func Length(t Testing, object any, expected int) bool {
104+
t.Helper()
105+
106+
if actual := internal.Length(object); actual != expected {
107+
return Failf(t, "Should have element length\n object: %#v\n object: %d\nelement: %d", object, actual, expected)
108+
}
109+
110+
return true
111+
}
112+
113+
func Contains(t Testing, object, element any) bool {
114+
t.Helper()
115+
116+
if found, ok := internal.Contains(object, element); !ok {
117+
return Failf(t, "Should be iterable\n object: %#v", object)
118+
} else if !found {
119+
return Failf(t, "Should contain element\n object: %#v\n element: %#v", object, element)
118120
}
119121

120-
if same(expected, actual) {
121-
return Failf(t, "Should not be same: %[1]p %#[1]v", actual)
122+
return true
123+
}
124+
125+
func NotContains(t Testing, object any, element any) bool {
126+
t.Helper()
127+
128+
if found, ok := internal.Contains(object, element); !ok {
129+
return Failf(t, "Should be iterable\n object: %#v", object)
130+
} else if found {
131+
return Failf(t, "Should not contain element\n object: %#v\n element: %#v", object, element)
122132
}
123133

124134
return true
125135
}
126136

127-
func same(actual, expected any) bool {
128-
actualPtr, expectedPtr := reflect.ValueOf(actual), reflect.ValueOf(expected)
129-
if actualPtr.Kind() != reflect.Ptr || expectedPtr.Kind() != reflect.Ptr {
130-
return false
137+
func Error(t Testing, err error) bool {
138+
t.Helper()
139+
140+
if !internal.Error(err) {
141+
return Failf(t, "Should be error")
131142
}
132143

133-
actualType, expectedType := reflect.TypeOf(actual), reflect.TypeOf(expected)
134-
if actualType != expectedType {
135-
return false
144+
return true
145+
}
146+
147+
func NoError(t Testing, err error) bool {
148+
t.Helper()
149+
150+
if internal.Error(err) {
151+
return Failf(t, "Should not be error\n error: %#v", err)
136152
}
137153

138-
// compare pointer addresses
139-
return actual == expected
154+
return true
155+
}
156+
157+
func ErrorIs(t Testing, err error, target error) bool {
158+
t.Helper()
159+
160+
if !internal.ErrorIs(err, target) {
161+
return Failf(t, "Should be same error\n error: %#v\n target: %#v", err, target)
162+
}
163+
164+
return true
165+
}
166+
167+
func NotErrorIs(t Testing, err error, target error) bool {
168+
t.Helper()
169+
170+
if internal.ErrorIs(err, target) {
171+
return Failf(t, "Should not be same error\n error: %#v", err, target)
172+
}
173+
174+
return true
140175
}
141176

142177
// Fail reports a failure through
143178
func Fail(t Testing, message string) bool {
144-
if h, ok := t.(helper); ok {
145-
h.Helper()
146-
}
179+
t.Helper()
147180

148181
t.Errorf(message)
149182

@@ -152,9 +185,7 @@ func Fail(t Testing, message string) bool {
152185

153186
// Failf reports a failure through
154187
func Failf(t Testing, format string, args ...any) bool {
155-
if h, ok := t.(helper); ok {
156-
h.Helper()
157-
}
188+
t.Helper()
158189

159190
t.Errorf(format, args...)
160191

0 commit comments

Comments
 (0)