Skip to content

Commit 29daa79

Browse files
committed
Do not use internal package
1 parent 6d1af32 commit 29daa79

2 files changed

Lines changed: 50 additions & 38 deletions

File tree

assert.go

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package assert
22

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

77
// Testing is an interface wrapper around *testing.T
@@ -16,7 +16,7 @@ type Testing interface {
1616
func True(t Testing, condition bool) bool {
1717
t.Helper()
1818

19-
if !internal.Assert(condition) {
19+
if !condition {
2020
return Fail(t, "Should be true")
2121
}
2222

@@ -29,7 +29,7 @@ func True(t Testing, condition bool) bool {
2929
func False(t Testing, condition bool) bool {
3030
t.Helper()
3131

32-
if internal.Assert(condition) {
32+
if condition {
3333
return Fail(t, "Should be false")
3434
}
3535

@@ -45,7 +45,7 @@ func False(t Testing, condition bool) bool {
4545
func Equal(t Testing, actual, expected any) bool {
4646
t.Helper()
4747

48-
if !internal.Equal(actual, expected) {
48+
if !equal(actual, expected) {
4949
return Failf(t, "Should be equal:\n actual: %#v\nexpected: %#v", actual, expected)
5050
}
5151

@@ -61,7 +61,7 @@ func Equal(t Testing, actual, expected any) bool {
6161
func NotEqual(t Testing, actual, expected any) bool {
6262
t.Helper()
6363

64-
if internal.Equal(actual, expected) {
64+
if equal(actual, expected) {
6565
return Failf(t, "Should not be equal\n actual: %#v", actual)
6666
}
6767

@@ -72,12 +72,12 @@ func NotEqual(t Testing, actual, expected any) bool {
7272
//
7373
// assert.Same(t, actual, expected)
7474
//
75-
// Both arguments must be pointer variables. Pointer variable sameness is
75+
// Both arguments must be pointer variables. Pointer variable equality is
7676
// determined based on the equality of both type and value.
7777
func Same(t Testing, actual, expected any) bool {
7878
t.Helper()
7979

80-
if !internal.Same(expected, actual) {
80+
if !same(expected, actual) {
8181
return Failf(t, "Should be same\n actual: %[1]p %#[1]v\nexpected: %[2]p %#[2]v", actual, expected)
8282
}
8383

@@ -88,32 +88,40 @@ func Same(t Testing, actual, expected any) bool {
8888
//
8989
// assert.NotSame(t, actual, expected)
9090
//
91-
// Both arguments must be pointer variables. Pointer variable sameness is
91+
// Both arguments must be pointer variables. Pointer variable equality is
9292
// determined based on the equality of both type and value.
9393
func NotSame(t Testing, actual, expected any) bool {
9494
t.Helper()
9595

96-
if internal.Same(expected, actual) {
96+
if same(expected, actual) {
9797
return Failf(t, "Should not be same\n actual: %[1]p %#[1]v", actual)
9898
}
9999

100100
return true
101101
}
102102

103+
// Length asserts that object have given length.
104+
//
105+
// assert.Length(t, object, expected)
103106
func Length(t Testing, object any, expected int) bool {
104107
t.Helper()
105108

106-
if actual := internal.Length(object); actual != expected {
109+
if actual := length(object); actual != expected {
107110
return Failf(t, "Should have element length\n object: %#v\n actual: %d\nexpected: %d", object, actual, expected)
108111
}
109112

110113
return true
111114
}
112115

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
113121
func Contains(t Testing, object, element any) bool {
114122
t.Helper()
115123

116-
if found, ok := internal.Contains(object, element); !ok {
124+
if found, ok := contains(object, element); !ok {
117125
return Failf(t, "Should be iterable\n object: %#v", object)
118126
} else if !found {
119127
return Failf(t, "Should contain element\n object: %#v\n element: %#v", object, element)
@@ -122,10 +130,15 @@ func Contains(t Testing, object, element any) bool {
122130
return true
123131
}
124132

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
125138
func NotContains(t Testing, object any, element any) bool {
126139
t.Helper()
127140

128-
if found, ok := internal.Contains(object, element); !ok {
141+
if found, ok := contains(object, element); !ok {
129142
return Failf(t, "Should be iterable\n object: %#v", object)
130143
} else if found {
131144
return Failf(t, "Should not contain element\n object: %#v\n element: %#v", object, element)
@@ -134,47 +147,59 @@ func NotContains(t Testing, object any, element any) bool {
134147
return true
135148
}
136149

150+
// Error asserts that error is NOT nil
151+
//
152+
// assert.Error(t, err)
137153
func Error(t Testing, err error) bool {
138154
t.Helper()
139155

140-
if !internal.Error(err) {
156+
if err == nil {
141157
return Failf(t, "Should be error")
142158
}
143159

144160
return true
145161
}
146162

163+
// NoError asserts that error is nil
164+
//
165+
// assert.NoError(t, err)
147166
func NoError(t Testing, err error) bool {
148167
t.Helper()
149168

150-
if internal.Error(err) {
169+
if err != nil {
151170
return Failf(t, "Should not be error\n error: %#v", err)
152171
}
153172

154173
return true
155174
}
156175

176+
// ErrorIs asserts that error is unwrappable to given target
177+
//
178+
// assert.ErrorIs(t, err)
157179
func ErrorIs(t Testing, err error, target error) bool {
158180
t.Helper()
159181

160-
if !internal.ErrorIs(err, target) {
182+
if !errors.Is(err, target) {
161183
return Failf(t, "Should be same error\n error: %#v\n target: %#v", err, target)
162184
}
163185

164186
return true
165187
}
166188

189+
// NotErrorIs asserts that error is NOT unwrappable to given target
190+
//
191+
// assert.NotErrorIs(t, err)
167192
func NotErrorIs(t Testing, err error, target error) bool {
168193
t.Helper()
169194

170-
if internal.ErrorIs(err, target) {
195+
if errors.Is(err, target) {
171196
return Failf(t, "Should not be same error\n error: %#v", err, target)
172197
}
173198

174199
return true
175200
}
176201

177-
// Fail reports a failure through
202+
// Fail reports a failure message through
178203
func Fail(t Testing, message string) bool {
179204
t.Helper()
180205

@@ -183,7 +208,7 @@ func Fail(t Testing, message string) bool {
183208
return false
184209
}
185210

186-
// Failf reports a failure through
211+
// Failf reports a failure formatted message through
187212
func Failf(t Testing, format string, args ...any) bool {
188213
t.Helper()
189214

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
package internal
1+
package assert
22

33
import (
4-
"errors"
54
"reflect"
65
"strings"
76
)
87

9-
func Assert(condition bool) bool {
10-
return condition
11-
}
12-
13-
func Equal(actual, expected any) bool {
8+
func equal(actual, expected any) bool {
149
return reflect.DeepEqual(actual, expected)
1510
}
1611

17-
func Same(actual, expected any) bool {
12+
func same(actual, expected any) bool {
1813
if reflect.ValueOf(actual).Kind() != reflect.Ptr || reflect.ValueOf(expected).Kind() != reflect.Ptr {
1914
return false
2015
}
2116

2217
return actual == expected
2318
}
2419

25-
func Length(object any) int {
20+
func length(object any) int {
2621
return reflect.ValueOf(object).Len()
2722
}
2823

29-
func Contains(object any, element any) (found bool, ok bool) {
24+
func contains(object any, element any) (found bool, ok bool) {
3025
valueOf := reflect.ValueOf(object)
3126
typeOf := reflect.TypeOf(object)
3227
if typeOf == nil {
@@ -41,26 +36,18 @@ func Contains(object any, element any) (found bool, ok bool) {
4136

4237
if kind == reflect.Map {
4338
for _, key := range valueOf.MapKeys() {
44-
if Equal(valueOf.MapIndex(key).Interface(), element) {
39+
if equal(valueOf.MapIndex(key).Interface(), element) {
4540
return true, true
4641
}
4742
}
4843
return false, true
4944
}
5045

5146
for i := 0; i < valueOf.Len(); i++ {
52-
if Equal(valueOf.Index(i).Interface(), element) {
47+
if equal(valueOf.Index(i).Interface(), element) {
5348
return true, true
5449
}
5550
}
5651

5752
return false, true
5853
}
59-
60-
func Error(err error) bool {
61-
return err != nil
62-
}
63-
64-
func ErrorIs(err, target error) bool {
65-
return errors.Is(err, target)
66-
}

0 commit comments

Comments
 (0)