Skip to content

Commit a358757

Browse files
committed
added more assertions
1 parent edd4c75 commit a358757

2 files changed

Lines changed: 91 additions & 47 deletions

File tree

assert.go

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

3-
import "reflect"
3+
import (
4+
"reflect"
5+
"strings"
6+
)
47

58
// Equal compares two values and returns true if they are equal.
69
func Equal[T any](a, b T) bool {
@@ -86,65 +89,65 @@ func Unique[T any](s []T) bool {
8689
return true
8790
}
8891

89-
// Contains returns true if the slice contains the value.
90-
// Items are considered equal if they are deep equal.
91-
func Contains[T any](s []T, v T) bool {
92-
// Use reflection DeepEqual to compare values.
93-
// This is necessary because slices of interfaces cannot be compared with ==.
94-
for _, item := range s {
95-
if reflect.DeepEqual(item, v) {
96-
return true
92+
// Contains returns true if a contains b.
93+
func Contains(a any, b any) bool {
94+
objectValue := reflect.ValueOf(a)
95+
objectKind := reflect.TypeOf(a).Kind()
96+
97+
switch objectKind {
98+
case reflect.String:
99+
return strings.Contains(objectValue.String(), reflect.ValueOf(b).String())
100+
case reflect.Map:
101+
default:
102+
for i := 0; i < objectValue.Len(); i++ {
103+
if Equal(objectValue.Index(i).Interface(), b) {
104+
return true
105+
}
97106
}
98107
}
108+
99109
return false
100110
}
101111

102-
// ContainsAll returns true if the slice contains all the values.
103-
// Items are considered equal if they are deep equal.
104-
func ContainsAll[T any](s []T, v []T) bool {
105-
// Use reflection DeepEqual to compare values.
106-
// This is necessary because slices of interfaces cannot be compared with ==.
107-
for _, item := range v {
108-
found := false
109-
for _, item2 := range s {
110-
if reflect.DeepEqual(item, item2) {
111-
found = true
112-
break
113-
}
114-
}
115-
if !found {
112+
// ContainsAll returns true if all values are contained in a.
113+
func ContainsAll[T any](a T, v []T) bool {
114+
for _, t := range v {
115+
if !Contains(a, t) {
116116
return false
117117
}
118118
}
119+
119120
return true
120121
}
121122

122-
// ContainsAny returns true if the slice contains any of the values.
123-
// Items are considered equal if they are deep equal.
124-
func ContainsAny[T any](s []T, v []T) bool {
125-
// Use reflection DeepEqual to compare values.
126-
// This is necessary because slices of interfaces cannot be compared with ==.
127-
for _, item := range v {
128-
for _, item2 := range s {
129-
if reflect.DeepEqual(item, item2) {
130-
return true
131-
}
123+
// ContainsAny returns true if any of the values are contained in a.
124+
func ContainsAny[T any](a T, v []T) bool {
125+
for _, t := range v {
126+
if Contains(a, t) {
127+
return true
132128
}
133129
}
130+
134131
return false
135132
}
136133

137-
// ContainsNone returns true if the slice contains none of the values.
138-
// Items are considered equal if they are deep equal.
139-
func ContainsNone[T any](s []T, v []T) bool {
140-
// Use reflection DeepEqual to compare values.
141-
// This is necessary because slices of interfaces cannot be compared with ==.
142-
for _, item := range v {
143-
for _, item2 := range s {
144-
if reflect.DeepEqual(item, item2) {
145-
return false
146-
}
134+
// ContainsNone returns true if none of the values are contained in a.
135+
func ContainsNone[T any](a T, v []T) bool {
136+
for _, t := range v {
137+
if Contains(a, t) {
138+
return false
147139
}
148140
}
141+
149142
return true
150143
}
144+
145+
// Uppercase returns true if the string is uppercase.
146+
func Uppercase(s string) bool {
147+
return s == strings.ToUpper(s)
148+
}
149+
150+
// Lowercase returns true if the string is lowercase.
151+
func Lowercase(s string) bool {
152+
return s == strings.ToLower(s)
153+
}

assert_test.go

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,17 @@ func TestUnique(t *testing.T) {
229229

230230
func TestContains(t *testing.T) {
231231
tests := []struct {
232-
a []any
232+
a any
233233
b any
234234
pass bool
235235
}{
236236
{[]any{1, 2, 3}, 1, true},
237237
{[]any{1, 2, 3}, 4, false},
238238
{[]any{"a", "b", "c"}, "a", true},
239239
{[]any{"a", "b", "c"}, "d", false},
240+
{"Hello, World!", "Hello", true},
241+
{"Hello, World!", "Hello, World!", true},
242+
{"Hello", "X", false},
240243
}
241244
for i, test := range tests {
242245
t.Run(strconv.Itoa(i), func(t *testing.T) {
@@ -249,14 +252,15 @@ func TestContains(t *testing.T) {
249252

250253
func TestContainsAll(t *testing.T) {
251254
tests := []struct {
252-
a []any
255+
a any
253256
b []any
254257
pass bool
255258
}{
256259
{[]any{1, 2, 3}, []any{1, 2}, true},
257260
{[]any{1, 2, 3}, []any{1, 4}, false},
258261
{[]any{"a", "b", "c"}, []any{"a", "b"}, true},
259262
{[]any{"a", "b", "c"}, []any{"a", "d"}, false},
263+
{"Hello, World!", []any{"Hello", "World"}, true},
260264
}
261265
for i, test := range tests {
262266
t.Run(strconv.Itoa(i), func(t *testing.T) {
@@ -269,7 +273,7 @@ func TestContainsAll(t *testing.T) {
269273

270274
func TestContainsAny(t *testing.T) {
271275
tests := []struct {
272-
a []any
276+
a any
273277
b []any
274278
pass bool
275279
}{
@@ -279,6 +283,7 @@ func TestContainsAny(t *testing.T) {
279283
{[]any{"a", "b", "c"}, []any{"a", "b"}, true},
280284
{[]any{"a", "b", "c"}, []any{"a", "d"}, true},
281285
{[]any{"a", "b", "c"}, []any{"d", "e"}, false},
286+
{"Hello, World!", []any{"Hello", "World", "XXX"}, true},
282287
}
283288
for i, test := range tests {
284289
t.Run(strconv.Itoa(i), func(t *testing.T) {
@@ -291,7 +296,7 @@ func TestContainsAny(t *testing.T) {
291296

292297
func TestContainsNone(t *testing.T) {
293298
tests := []struct {
294-
a []any
299+
a any
295300
b []any
296301
pass bool
297302
}{
@@ -310,3 +315,39 @@ func TestContainsNone(t *testing.T) {
310315
})
311316
}
312317
}
318+
319+
func TestUppercase(t *testing.T) {
320+
tests := []struct {
321+
a string
322+
pass bool
323+
}{
324+
{"ABC", true},
325+
{"abc", false},
326+
{"Abc", false},
327+
}
328+
for i, test := range tests {
329+
t.Run(strconv.Itoa(i), func(t *testing.T) {
330+
if assert.Uppercase(test.a) != test.pass {
331+
fail(t, fmt.Sprintf("Uppercase(%v) != %v", test.a, test.pass))
332+
}
333+
})
334+
}
335+
}
336+
337+
func TestLowercase(t *testing.T) {
338+
tests := []struct {
339+
a string
340+
pass bool
341+
}{
342+
{"ABC", false},
343+
{"abc", true},
344+
{"Abc", false},
345+
}
346+
for i, test := range tests {
347+
t.Run(strconv.Itoa(i), func(t *testing.T) {
348+
if assert.Lowercase(test.a) != test.pass {
349+
fail(t, fmt.Sprintf("Lowercase(%v) != %v", test.a, test.pass))
350+
}
351+
})
352+
}
353+
}

0 commit comments

Comments
 (0)