Skip to content

Commit fb7c4f6

Browse files
committed
feat: add ContainsElement.
1 parent 5bcddd6 commit fb7c4f6

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ func TestExample(t *testing.T) {
148148
- [`Match`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.Match) and [`NotMatch`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotMatch): assert whether the string matches the regular expression pattern or not.
149149
- [`MatchString`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.MatchString) and [`NotMatchString`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotMatchString): compile the regular expression pattern and assert whether the string matches the pattern or not.
150150

151+
### Slice or Array
152+
153+
- [`ContainsElement`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.ContainsElement) and [`NotContainsElement`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotContainsElement): assert whether the array or slice contains the specified element or not.
154+
151155
### Error Handling
152156

153157
- [`Panic`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.Panic) and [`NotPanic`](https://pkg.go.dev/github.com/ghosind/go-assert#Assertion.NotPanic): assert the function will panic or not.

builtin.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,60 @@ import (
55
"testing"
66
)
77

8+
// NotContainsElement tests whether the array or slice contains the specified element or not, and
9+
// it set the result to fail if the array or slice does not contain the specified element. It'll
10+
// panic if the `source` is not an array or a slice.
11+
//
12+
// assertion.ContainsElement([]int{1, 2, 3}, 1) // success
13+
// assertion.ContainsElement([]int{1, 2, 3}, 3) // success
14+
// assertion.ContainsElement([]int{1, 2, 3}, 4) // fail
15+
func ContainsElement(t *testing.T, source, expect any, message ...any) error {
16+
t.Helper()
17+
18+
return tryContainsElement(t, false, source, expect, message...)
19+
}
20+
21+
// ContainsElementNow tests whether the array or slice contains the specified element or not, and
22+
// it will terminate the execution if the array or slice does not contain the specified element.
23+
// It'll panic if the `source` is not an array or a slice.
24+
//
25+
// assertion.ContainsElementNow([]int{1, 2, 3}, 1) // success
26+
// assertion.ContainsElementNow([]int{1, 2, 3}, 3) // success
27+
// assertion.ContainsElementNow([]int{1, 2, 3}, 4) // fail and stop the execution
28+
// // never runs
29+
func ContainsElementNow(t *testing.T, source, expect any, message ...any) error {
30+
t.Helper()
31+
32+
return tryContainsElement(t, true, source, expect, message...)
33+
}
34+
35+
// NotContainsElement tests whether the array or slice contains the specified element or not, and
36+
// it set the result to fail if the array or slice contains the specified element. It'll panic if
37+
// the `source` is not an array or a slice.
38+
//
39+
// assertion.NotContainsElement([]int{1, 2, 3}, 4) // success
40+
// assertion.NotContainsElement([]int{1, 2, 3}, 0) // success
41+
// assertion.NotContainsElement([]int{1, 2, 3}, 1) // fail
42+
func NotContainsElement(t *testing.T, source, expect any, message ...any) error {
43+
t.Helper()
44+
45+
return tryNotContainsElement(t, false, source, expect, message...)
46+
}
47+
48+
// NotContainsElementNow tests whether the array or slice contains the specified element or not,
49+
// and it will terminate the execution if the array or slice contains the specified element. It'll
50+
// panic if the `source` is not an array or a slice.
51+
//
52+
// assertion.NotContainsElementNow([]int{1, 2, 3}, 4) // success
53+
// assertion.NotContainsElementNow([]int{1, 2, 3}, 0) // success
54+
// assertion.NotContainsElementNow([]int{1, 2, 3}, 1) // fail and stop the execution
55+
// // never runs
56+
func NotContainsElementNow(t *testing.T, source, expect any, message ...any) error {
57+
t.Helper()
58+
59+
return tryNotContainsElement(t, true, source, expect, message...)
60+
}
61+
862
// ContainsString tests whether the string contains the substring or not, and it set the result to
963
// fail if the string does not contains the substring.
1064
//

error.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "fmt"
55
const (
66
defaultErrMessageEqual string = "%v == %v"
77
defaultErrMessageNotEqual string = "%v != %v"
8+
defaultErrMessageContainsElement string = "expect contains %v"
9+
defaultErrMessageNotContainsElement string = "expect did not contains %v"
810
defaultErrMessageContainsString string = "expect contains \"%s\""
911
defaultErrMessageNotContainsString string = "expect did not contain \"%s\""
1012
defaultErrMessageHasPrefixString string = "expect has prefix \"%s\""

slice.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package assert
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
// NotContainsElement tests whether the array or slice contains the specified element or not, and
9+
// it set the result to fail if the array or slice does not contain the specified element. It'll
10+
// panic if the `source` is not an array or a slice.
11+
//
12+
// assertion.ContainsElement([]int{1, 2, 3}, 1) // success
13+
// assertion.ContainsElement([]int{1, 2, 3}, 3) // success
14+
// assertion.ContainsElement([]int{1, 2, 3}, 4) // fail
15+
func (a *Assertion) ContainsElement(source, expect any, message ...any) error {
16+
a.Helper()
17+
18+
return tryContainsElement(a.T, false, source, expect, message...)
19+
}
20+
21+
// ContainsElementNow tests whether the array or slice contains the specified element or not, and
22+
// it will terminate the execution if the array or slice does not contain the specified element.
23+
// It'll panic if the `source` is not an array or a slice.
24+
//
25+
// assertion.ContainsElementNow([]int{1, 2, 3}, 1) // success
26+
// assertion.ContainsElementNow([]int{1, 2, 3}, 3) // success
27+
// assertion.ContainsElementNow([]int{1, 2, 3}, 4) // fail and stop the execution
28+
// // never runs
29+
func (a *Assertion) ContainsElementNow(source, expect any, message ...any) error {
30+
a.Helper()
31+
32+
return tryContainsElement(a.T, true, source, expect, message...)
33+
}
34+
35+
// NotContainsElement tests whether the array or slice contains the specified element or not, and
36+
// it set the result to fail if the array or slice contains the specified element. It'll panic if
37+
// the `source` is not an array or a slice.
38+
//
39+
// assertion.NotContainsElement([]int{1, 2, 3}, 4) // success
40+
// assertion.NotContainsElement([]int{1, 2, 3}, 0) // success
41+
// assertion.NotContainsElement([]int{1, 2, 3}, 1) // fail
42+
func (a *Assertion) NotContainsElement(source, expect any, message ...any) error {
43+
a.Helper()
44+
45+
return tryNotContainsElement(a.T, false, source, expect, message...)
46+
}
47+
48+
// NotContainsElementNow tests whether the array or slice contains the specified element or not,
49+
// and it will terminate the execution if the array or slice contains the specified element. It'll
50+
// panic if the `source` is not an array or a slice.
51+
//
52+
// assertion.NotContainsElementNow([]int{1, 2, 3}, 4) // success
53+
// assertion.NotContainsElementNow([]int{1, 2, 3}, 0) // success
54+
// assertion.NotContainsElementNow([]int{1, 2, 3}, 1) // fail and stop the execution
55+
// // never runs
56+
func (a *Assertion) NotContainsElementNow(source, expect any, message ...any) error {
57+
a.Helper()
58+
59+
return tryNotContainsElement(a.T, true, source, expect, message...)
60+
}
61+
62+
// tryContainsElement tries to test whether the array or slice contains the specified element or
63+
// not, and it'll fail if the array or slice does not contains the specified element.
64+
func tryContainsElement(
65+
t *testing.T,
66+
failedNow bool,
67+
src, elem any,
68+
message ...any,
69+
) error {
70+
t.Helper()
71+
72+
return test(
73+
t,
74+
func() bool { return isContainsElement(src, elem) },
75+
failedNow,
76+
fmt.Sprintf(defaultErrMessageContainsElement, elem),
77+
message...,
78+
)
79+
}
80+
81+
// tryNotContainsElement tries to test whether the array or slice contains the specified element
82+
// or not, and it'll fail if the array of slice contains the specified element.
83+
func tryNotContainsElement(
84+
t *testing.T,
85+
failedNow bool,
86+
src, elem any,
87+
message ...any,
88+
) error {
89+
t.Helper()
90+
91+
return test(
92+
t,
93+
func() bool { return !isContainsElement(src, elem) },
94+
failedNow,
95+
fmt.Sprintf(defaultErrMessageNotContainsElement, elem),
96+
message...,
97+
)
98+
}

slice_test.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package assert
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ghosind/go-assert/internal"
7+
)
8+
9+
func TestElementContainsAndNotContains(t *testing.T) {
10+
mockT := new(testing.T)
11+
assert := New(mockT)
12+
13+
testElementContainsAndNotContains(t, assert, []int{1}, 1, true)
14+
testElementContainsAndNotContains(t, assert, []int{1, 2, 3}, 3, true)
15+
testElementContainsAndNotContains(t, assert, []int{1, 2, 3}, 4, false)
16+
testElementContainsAndNotContains(t, assert, []int{}, 1, false)
17+
testElementContainsAndNotContains(t, assert, [1]int{1}, 1, true)
18+
testElementContainsAndNotContains(t, assert, [3]int{1, 2, 3}, 3, true)
19+
testElementContainsAndNotContains(t, assert, [3]int{1, 2, 3}, 4, false)
20+
testElementContainsAndNotContains(t, assert, [0]int{}, 1, false)
21+
}
22+
23+
func testElementContainsAndNotContains(
24+
t *testing.T,
25+
assertion *Assertion,
26+
source, expect any,
27+
isContains bool,
28+
) {
29+
testContainsElement(t, assertion, source, expect, isContains)
30+
31+
testNotContainsElement(t, assertion, source, expect, isContains)
32+
33+
testContainsElementNow(t, assertion, source, expect, isContains)
34+
35+
testNotContainsElementNow(t, assertion, source, expect, isContains)
36+
}
37+
38+
func testContainsElement(
39+
t *testing.T,
40+
assertion *Assertion,
41+
source, expect any,
42+
isContains bool,
43+
) {
44+
err := assertion.ContainsElement(source, expect)
45+
if isContains && err != nil {
46+
t.Errorf("ContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err)
47+
} else if !isContains && err == nil {
48+
t.Errorf("ContainsElement(\"%v\", \"%v\") = nil, want error", source, expect)
49+
}
50+
51+
err = ContainsElement(assertion.T, source, expect)
52+
if isContains && err != nil {
53+
t.Errorf("ContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err)
54+
} else if !isContains && err == nil {
55+
t.Errorf("ContainsElement(\"%v\", \"%v\") = nil, want error", source, expect)
56+
}
57+
}
58+
59+
func testNotContainsElement(
60+
t *testing.T,
61+
assertion *Assertion,
62+
source, expect any,
63+
isContains bool,
64+
) {
65+
err := assertion.NotContainsElement(source, expect)
66+
if isContains && err == nil {
67+
t.Errorf("NotContainsElement(\"%v\", \"%v\") = nil, want error", source, expect)
68+
} else if !isContains && err != nil {
69+
t.Errorf("NotContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err)
70+
}
71+
72+
err = NotContainsElement(assertion.T, source, expect)
73+
if isContains && err == nil {
74+
t.Errorf("NotContainsElement(\"%v\", \"%v\") = nil, want error", source, expect)
75+
} else if !isContains && err != nil {
76+
t.Errorf("NotContainsElement(\"%v\", \"%v\") = %v, want nil", source, expect, err)
77+
}
78+
}
79+
80+
func testContainsElementNow(
81+
t *testing.T,
82+
assertion *Assertion,
83+
source, expect any,
84+
isContains bool,
85+
) {
86+
isTerminated := internal.CheckTermination(func() {
87+
assertion.ContainsElementNow(source, expect)
88+
})
89+
if isContains && isTerminated {
90+
t.Error("execution stopped, want do not stop")
91+
} else if !isContains && !isTerminated {
92+
t.Error("execution do not stopped, want stop")
93+
}
94+
95+
isTerminated = internal.CheckTermination(func() {
96+
ContainsElementNow(assertion.T, source, expect)
97+
})
98+
if isContains && isTerminated {
99+
t.Error("execution stopped, want do not stop")
100+
} else if !isContains && !isTerminated {
101+
t.Error("execution do not stopped, want stop")
102+
}
103+
}
104+
105+
func testNotContainsElementNow(
106+
t *testing.T,
107+
assertion *Assertion,
108+
source, expect any,
109+
isContains bool,
110+
) {
111+
isTerminated := internal.CheckTermination(func() {
112+
assertion.NotContainsElementNow(source, expect)
113+
})
114+
if !isContains && isTerminated {
115+
t.Error("execution stopped, want do not stop")
116+
} else if isContains && !isTerminated {
117+
t.Error("execution do not stopped, want stop")
118+
}
119+
120+
isTerminated = internal.CheckTermination(func() {
121+
NotContainsElementNow(assertion.T, source, expect)
122+
})
123+
if !isContains && isTerminated {
124+
t.Error("execution stopped, want do not stop")
125+
} else if isContains && !isTerminated {
126+
t.Error("execution do not stopped, want stop")
127+
}
128+
}

0 commit comments

Comments
 (0)