Skip to content

Commit bf9ac10

Browse files
committed
new methods
1 parent e8fe4b7 commit bf9ac10

16 files changed

Lines changed: 1654 additions & 113 deletions

any_all_none_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package utils
2+
3+
import "testing"
4+
5+
func TestAny(t *testing.T) {
6+
type args[T any] struct {
7+
list []T
8+
fn func(T) bool
9+
}
10+
type testCase[T any] struct {
11+
name string
12+
args args[T]
13+
want bool
14+
}
15+
tests := []testCase[string]{
16+
{
17+
name: "empty",
18+
args: struct {
19+
list []string
20+
fn func(string) bool
21+
}{list: make([]string, 0), fn: fnTrue},
22+
want: false,
23+
},
24+
{
25+
name: "not empty",
26+
args: struct {
27+
list []string
28+
fn func(string) bool
29+
}{list: []string{"xd"}, fn: fnTrue},
30+
want: true,
31+
},
32+
{
33+
name: "not length 2",
34+
args: struct {
35+
list []string
36+
fn func(string) bool
37+
}{list: []string{"xdd"}, fn: fnLength2},
38+
want: false,
39+
},
40+
{
41+
name: "length 2",
42+
args: struct {
43+
list []string
44+
fn func(string) bool
45+
}{list: []string{"xdd", "xd"}, fn: fnLength2},
46+
want: true,
47+
},
48+
}
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
if got := Any(tt.args.list, tt.args.fn); got != tt.want {
52+
t.Errorf("Any() = %v, want %v", got, tt.want)
53+
}
54+
})
55+
}
56+
}
57+
58+
func TestAll(t *testing.T) {
59+
type args[T any] struct {
60+
list []T
61+
fn func(T) bool
62+
}
63+
type testCase[T any] struct {
64+
name string
65+
args args[T]
66+
want bool
67+
}
68+
tests := []testCase[string]{
69+
{
70+
name: "empty",
71+
args: struct {
72+
list []string
73+
fn func(string) bool
74+
}{list: make([]string, 0), fn: fnTrue},
75+
want: true,
76+
},
77+
{
78+
name: "not empty",
79+
args: struct {
80+
list []string
81+
fn func(string) bool
82+
}{list: []string{"xd"}, fn: fnTrue},
83+
want: true,
84+
},
85+
{
86+
name: "not empty false",
87+
args: struct {
88+
list []string
89+
fn func(string) bool
90+
}{list: []string{"xd"}, fn: fnFalse},
91+
want: false,
92+
},
93+
{
94+
name: "not length 2",
95+
args: struct {
96+
list []string
97+
fn func(string) bool
98+
}{list: []string{"xdd"}, fn: fnLength2},
99+
want: false,
100+
},
101+
}
102+
for _, tt := range tests {
103+
t.Run(tt.name, func(t *testing.T) {
104+
if got := All(tt.args.list, tt.args.fn); got != tt.want {
105+
t.Errorf("All() = %v, want %v", got, tt.want)
106+
}
107+
})
108+
}
109+
}

count_test.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package utils
2+
3+
import "testing"
4+
5+
func TestCount(t *testing.T) {
6+
type args[T string] struct {
7+
list []T
8+
fn func(T) bool
9+
}
10+
type testCase[T string] struct {
11+
name string
12+
args args[T]
13+
want int
14+
}
15+
tests := []testCase[string]{
16+
{
17+
name: "empty-true",
18+
args: struct {
19+
list []string
20+
fn func(string) bool
21+
}{list: make([]string, 0), fn: fnTrue},
22+
want: 0,
23+
},
24+
{
25+
name: "empty-false",
26+
args: struct {
27+
list []string
28+
fn func(string) bool
29+
}{list: make([]string, 0), fn: fnFalse},
30+
want: 0,
31+
},
32+
{
33+
name: "empty-len2",
34+
args: struct {
35+
list []string
36+
fn func(string) bool
37+
}{list: make([]string, 0), fn: fnLength2},
38+
want: 0,
39+
},
40+
{
41+
name: "one-true",
42+
args: struct {
43+
list []string
44+
fn func(string) bool
45+
}{list: []string{"x"}, fn: fnTrue},
46+
want: 1,
47+
},
48+
{
49+
name: "one-false",
50+
args: struct {
51+
list []string
52+
fn func(string) bool
53+
}{list: []string{"x"}, fn: fnFalse},
54+
want: 0,
55+
},
56+
{
57+
name: "one-len2-true",
58+
args: struct {
59+
list []string
60+
fn func(string) bool
61+
}{list: []string{"xd"}, fn: fnLength2},
62+
want: 1,
63+
},
64+
{
65+
name: "one-len2-false",
66+
args: struct {
67+
list []string
68+
fn func(string) bool
69+
}{list: []string{"xdd"}, fn: fnLength2},
70+
want: 0,
71+
},
72+
{
73+
name: "two-true",
74+
args: struct {
75+
list []string
76+
fn func(string) bool
77+
}{list: []string{"xd", "dx"}, fn: fnTrue},
78+
want: 2,
79+
},
80+
{
81+
name: "two-false",
82+
args: struct {
83+
list []string
84+
fn func(string) bool
85+
}{list: []string{"xd", "dx"}, fn: fnFalse},
86+
want: 0,
87+
},
88+
{
89+
name: "two-len2-0",
90+
args: struct {
91+
list []string
92+
fn func(string) bool
93+
}{list: []string{"x", "d"}, fn: fnLength2},
94+
want: 0,
95+
},
96+
{
97+
name: "two-len2-1",
98+
args: struct {
99+
list []string
100+
fn func(string) bool
101+
}{list: []string{"xd", "d"}, fn: fnLength2},
102+
want: 1,
103+
},
104+
{
105+
name: "two-len2-2",
106+
args: struct {
107+
list []string
108+
fn func(string) bool
109+
}{list: []string{"xd", "dx"}, fn: fnLength2},
110+
want: 2,
111+
},
112+
}
113+
for _, tt := range tests {
114+
t.Run(tt.name, func(t *testing.T) {
115+
if got := Count(tt.args.list, tt.args.fn); got != tt.want {
116+
t.Errorf("Count() = %v, want %v", got, tt.want)
117+
}
118+
})
119+
}
120+
}

first_fn_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package utils
2+
3+
import "testing"
4+
5+
func TestFirstFn(t *testing.T) {
6+
type args[T any] struct {
7+
list []T
8+
fn func(T) bool
9+
}
10+
type testCase[T any] struct {
11+
name string
12+
args args[T]
13+
want *T
14+
}
15+
16+
tests := []testCase[string]{
17+
// Existing tests
18+
{name: "empty-true", args: args[string]{list: []string{}, fn: fnTrue}, want: nil},
19+
{name: "empty-false", args: args[string]{list: []string{}, fn: fnFalse}, want: nil},
20+
{name: "single-true", args: args[string]{list: []string{"xd"}, fn: fnTrue}, want: Ptr("xd")},
21+
{name: "single-false", args: args[string]{list: []string{"xd"}, fn: fnFalse}, want: nil},
22+
23+
// Multiple-element tests
24+
{name: "two-true", args: args[string]{list: []string{"xd", "dx"}, fn: fnTrue}, want: Ptr("xd")},
25+
{name: "two-false", args: args[string]{list: []string{"xd", "dx"}, fn: fnFalse}, want: nil},
26+
27+
// Length-based tests
28+
{name: "two-len2-both-1", args: args[string]{list: []string{"xd", "dx"}, fn: fnLength2}, want: Ptr("xd")},
29+
{name: "two-len2-both-2", args: args[string]{list: []string{"dx", "xd"}, fn: fnLength2}, want: Ptr("dx")},
30+
{name: "two-len2-first", args: args[string]{list: []string{"xd", "dxx"}, fn: fnLength2}, want: Ptr("xd")},
31+
{name: "two-len2-second", args: args[string]{list: []string{"xdd", "dx"}, fn: fnLength2}, want: Ptr("dx")},
32+
{name: "two-len2-none", args: args[string]{list: []string{"xdd", "dxx"}, fn: fnLength2}, want: nil},
33+
34+
// Multiple-element variations
35+
{name: "multiple-len2-none", args: args[string]{list: []string{"xdd", "d", "a", "b", "c"}, fn: fnLength2}, want: nil},
36+
{name: "multiple-len2-last", args: args[string]{list: []string{"xdd", "d", "a", "b", "cc"}, fn: fnLength2}, want: Ptr("cc")},
37+
{name: "multiple-len2-last-two", args: args[string]{list: []string{"xdd", "d", "a", "bb", "cc"}, fn: fnLength2}, want: Ptr("bb")},
38+
{name: "multiple-len2-first-last-two", args: args[string]{list: []string{"xd", "d", "a", "bb", "cc"}, fn: fnLength2}, want: Ptr("xd")},
39+
40+
// Additional edge cases
41+
{name: "all-match", args: args[string]{list: []string{"aa", "bb", "cc"}, fn: fnLength2}, want: Ptr("aa")},
42+
{name: "first-match-later", args: args[string]{list: []string{"a", "bb", "ccc"}, fn: fnLength2}, want: Ptr("bb")},
43+
{name: "no-match", args: args[string]{list: []string{"a", "bbb", "cccc"}, fn: fnLength2}, want: nil},
44+
{name: "single-nil-string", args: args[string]{list: []string{""}, fn: fnLength2}, want: nil},
45+
{name: "first-empty-match", args: args[string]{list: []string{"", "aa"}, fn: fnLength2}, want: Ptr("aa")},
46+
}
47+
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
if got := FirstFn(tt.args.list, tt.args.fn); !ValueEqual(got, tt.want) {
51+
t.Errorf("FirstFn() = %v, want %v", got, tt.want)
52+
}
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)