-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.go
More file actions
148 lines (128 loc) · 4.36 KB
/
slice.go
File metadata and controls
148 lines (128 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package assert
import (
"fmt"
"reflect"
"testing"
)
// NotContainsElement tests whether the array or slice contains the specified element or not, and
// it set the result to fail if the array or slice does not contain the specified element. It'll
// panic if the `source` is not an array or a slice.
//
// a := assert.New(t)
// a.ContainsElement([]int{1, 2, 3}, 1) // success
// a.ContainsElement([]int{1, 2, 3}, 3) // success
// a.ContainsElement([]int{1, 2, 3}, 4) // fail
func (a *Assertion) ContainsElement(source, expect any, message ...any) error {
a.Helper()
return tryContainsElement(a.T, false, source, expect, message...)
}
// ContainsElementNow tests whether the array or slice contains the specified element or not, and
// it will terminate the execution if the array or slice does not contain the specified element.
// It'll panic if the `source` is not an array or a slice.
//
// a := assert.New(t)
// a.ContainsElementNow([]int{1, 2, 3}, 1) // success
// a.ContainsElementNow([]int{1, 2, 3}, 3) // success
// a.ContainsElementNow([]int{1, 2, 3}, 4) // fail and stop the execution
// // never runs
func (a *Assertion) ContainsElementNow(source, expect any, message ...any) error {
a.Helper()
return tryContainsElement(a.T, true, source, expect, message...)
}
// NotContainsElement tests whether the array or slice contains the specified element or not, and
// it set the result to fail if the array or slice contains the specified element. It'll panic if
// the `source` is not an array or a slice.
//
// a := assert.New(t)
// a.NotContainsElement([]int{1, 2, 3}, 4) // success
// a.NotContainsElement([]int{1, 2, 3}, 0) // success
// a.NotContainsElement([]int{1, 2, 3}, 1) // fail
func (a *Assertion) NotContainsElement(source, expect any, message ...any) error {
a.Helper()
return tryNotContainsElement(a.T, false, source, expect, message...)
}
// NotContainsElementNow tests whether the array or slice contains the specified element or not,
// and it will terminate the execution if the array or slice contains the specified element. It'll
// panic if the `source` is not an array or a slice.
//
// a := assert.New(t)
// a.NotContainsElementNow([]int{1, 2, 3}, 4) // success
// a.NotContainsElementNow([]int{1, 2, 3}, 0) // success
// a.NotContainsElementNow([]int{1, 2, 3}, 1) // fail and stop the execution
// // never runs
func (a *Assertion) NotContainsElementNow(source, expect any, message ...any) error {
a.Helper()
return tryNotContainsElement(a.T, true, source, expect, message...)
}
// tryContainsElement tries to test whether the array or slice contains the specified element or
// not, and it'll fail if the array or slice does not contains the specified element.
func tryContainsElement(
t *testing.T,
failedNow bool,
src, elem any,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return isContainsElement(src, elem) },
failedNow,
fmt.Sprintf(defaultErrMessageContainsElement, elem),
message...,
)
}
// tryNotContainsElement tries to test whether the array or slice contains the specified element
// or not, and it'll fail if the array of slice contains the specified element.
func tryNotContainsElement(
t *testing.T,
failedNow bool,
src, elem any,
message ...any,
) error {
t.Helper()
return test(
t,
func() bool { return !isContainsElement(src, elem) },
failedNow,
fmt.Sprintf(defaultErrMessageNotContainsElement, elem),
message...,
)
}
// isContainsElement checks whether the array or slice contains the specific element or not. It'll
// panic if the source is not an array or a slice, and it'll also panic if the element's type is
// not the same as the source's element.
func isContainsElement(source, elem any) bool {
st := reflect.ValueOf(source)
if st.Kind() == reflect.Ptr {
st = st.Elem()
}
if st.Kind() != reflect.Array && st.Kind() != reflect.Slice {
panic(ErrNotArray)
}
if ok := isSameType(st.Type().Elem(), reflect.TypeOf(elem)); !ok {
panic(ErrNotSameType)
}
if st.Len() == 0 {
return false
}
ev := reflect.ValueOf(elem)
for i := 0; i < st.Len(); i++ {
ok := isEqual(st.Index(i), ev)
if ok {
return true
}
}
return false
}
// isSliceEqual checks the equality of each elements in the slices.
func isSliceEqual(v1, v2 reflect.Value) bool {
if v1.Len() != v2.Len() {
return false
}
for i := 0; i < v1.Len(); i++ {
if ok := isEqual(v1.Index(i), v2.Index(i)); !ok {
return false
}
}
return true
}