assert.NotEqual   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
package assert
2
3
import (
4
	"errors"
5
)
6
7
// Testing is an interface wrapper around *testing.T
8
type Testing interface {
9
	Helper()
10
	Errorf(format string, args ...any)
11
}
12
13
// True asserts that the specified value is true.
14
//
15
//	assert.True(t, condition)
16
func True(t Testing, condition bool) bool {
17 2
	t.Helper()
18
19 2
	if !condition {
20 1
		return Fail(t, "Should be true")
21
	}
22
23 1
	return true
24
}
25
26
// False asserts that the specified value is false.
27
//
28
//	assert.False(t, condition)
29
func False(t Testing, condition bool) bool {
30
	t.Helper()
31
32
	if condition {
33
		return Fail(t, "Should be false")
34
	}
35
36
	return true
37
}
38
39
// Equal asserts that two objects are equal.
40
//
41
//	assert.Equal(t, actual, expected)
42
//
43
// Pointer variable equality is determined based on the equality of the
44
// referenced values (as opposed to the memory addresses).
45
func Equal(t Testing, actual, expected any) bool {
46 13
	t.Helper()
47
48 13
	if !equal(actual, expected) {
49 4
		return Failf(t, "Should be equal:\n  actual: %#v\nexpected: %#v", actual, expected)
50
	}
51
52 9
	return true
53
}
54
55
// NotEqual asserts that the specified values are NOT equal.
56
//
57
//	assert.NotEqual(t, actual, expected)
58
//
59
// Pointer variable equality is determined based on the equality of the
60
// referenced values (as opposed to the memory addresses).
61
func NotEqual(t Testing, actual, expected any) bool {
62 13
	t.Helper()
63
64 13
	if equal(actual, expected) {
65 9
		return Failf(t, "Should not be equal\n  actual: %#v", actual)
66
	}
67
68 4
	return true
69
}
70
71
// Same asserts that two pointers reference the same object.
72
//
73
//	assert.Same(t, actual, expected)
74
//
75
// Both arguments must be pointer variables. Pointer variable equality is
76
// determined based on the equality of both type and value.
77
func Same(t Testing, actual, expected any) bool {
78 11
	t.Helper()
79
80 11
	if !same(expected, actual) {
81 8
		return Failf(t, "Should be same\n  actual: %[1]p %#[1]v\nexpected: %[2]p %#[2]v", actual, expected)
82
	}
83
84 3
	return true
85
}
86
87
// NotSame asserts that two pointers do NOT reference the same object.
88
//
89
//	assert.NotSame(t, actual, expected)
90
//
91
// Both arguments must be pointer variables. Pointer variable equality is
92
// determined based on the equality of both type and value.
93
func NotSame(t Testing, actual, expected any) bool {
94 11
	t.Helper()
95
96 11
	if same(expected, actual) {
97 3
		return Failf(t, "Should not be same\n  actual: %[1]p %#[1]v", actual)
98
	}
99
100 8
	return true
101
}
102
103
// Length asserts that object have given length.
104
//
105
//	assert.Length(t, object, expected)
106
func Length(t Testing, object any, expected int) bool {
107 5
	t.Helper()
108
109 5
	if actual := length(object); actual != expected {
110 1
		return Failf(t, "Should have element length\n  object: %#v\n  actual: %d\nexpected: %d", object, actual, expected)
111
	}
112
113 4
	return true
114
}
115
116
// Contains asserts that object contains given element
117
//
118
//	assert.Contains(t, object, element)
119
//
120
// Works with strings, arrays, slices, maps values and channels
121
func Contains(t Testing, object, element any) bool {
122 7
	t.Helper()
123
124 7
	if found, ok := contains(object, element); !ok {
125
		return Failf(t, "Should be iterable\n  object: %#v", object)
126 7
	} else if !found {
127 4
		return Failf(t, "Should contain element\n  object: %#v\n element: %#v", object, element)
128
	}
129
130 3
	return true
131
}
132
133
// NotContains asserts that object do NOT contains given element
134
//
135
//	assert.NotContains(t, object, element)
136
//
137
// Works with strings, arrays, slices, maps values and channels
138
func NotContains(t Testing, object any, element any) bool {
139 7
	t.Helper()
140
141 7
	if found, ok := contains(object, element); !ok {
142
		return Failf(t, "Should be iterable\n  object: %#v", object)
143 7
	} else if found {
144 3
		return Failf(t, "Should not contain element\n  object: %#v\n element: %#v", object, element)
145
	}
146
147 4
	return true
148
}
149
150
// Error asserts that error is NOT nil
151
//
152
//	assert.Error(t, err)
153
func Error(t Testing, err error) bool {
154 2
	t.Helper()
155
156 2
	if err == nil {
157 1
		return Failf(t, "Should be error")
158
	}
159
160 1
	return true
161
}
162
163
// NoError asserts that error is nil
164
//
165
//	assert.NoError(t, err)
166
func NoError(t Testing, err error) bool {
167 2
	t.Helper()
168
169 2
	if err != nil {
170 1
		return Failf(t, "Should not be error\n   error: %#v", err)
171
	}
172
173 1
	return true
174
}
175
176
// ErrorIs asserts that error is unwrappable to given target
177
//
178
//	assert.ErrorIs(t, err)
179
func ErrorIs(t Testing, err error, target error) bool {
180 4
	t.Helper()
181
182 4
	if !errors.Is(err, target) {
183 1
		return Failf(t, "Should be same error\n   error: %#v\n  target: %#v", err, target)
184
	}
185
186 3
	return true
187
}
188
189
// NotErrorIs asserts that error is NOT unwrappable to given target
190
//
191
//	assert.NotErrorIs(t, err)
192
func NotErrorIs(t Testing, err error, target error) bool {
193 4
	t.Helper()
194
195 4
	if errors.Is(err, target) {
196 3
		return Failf(t, "Should not be same error\n   error: %#v", err, target)
197
	}
198
199 1
	return true
200
}
201
202
// Fail reports a failure message through
203
func Fail(t Testing, message string) bool {
204 1
	t.Helper()
205
206 1
	t.Errorf(message)
207
208 1
	return false
209
}
210
211
// Failf reports a failure formatted message through
212
func Failf(t Testing, format string, args ...any) bool {
213 38
	t.Helper()
214
215 38
	t.Errorf(format, args...)
216
217 38
	return false
218
}
219