Skip to content

Commit bff5d19

Browse files
pixel365bep
authored andcommitted
common/collections: Increase test coverage
1 parent da370d3 commit bff5d19

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

common/collections/append_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package collections
1515

1616
import (
1717
"html/template"
18+
"reflect"
1819
"testing"
1920

2021
qt "github.com/frankban/quicktest"
@@ -77,6 +78,7 @@ func TestAppend(t *testing.T) {
7778
{[]string{"a", "b"}, []any{nil}, []any{"a", "b", nil}},
7879
{[]string{"a", "b"}, []any{nil, "d", nil}, []any{"a", "b", nil, "d", nil}},
7980
{[]any{"a", nil, "c"}, []any{"d", nil, "f"}, []any{"a", nil, "c", "d", nil, "f"}},
81+
{[]string{"a", "b"}, []any{}, []string{"a", "b"}},
8082
} {
8183

8284
result, err := Append(test.start, test.addend...)
@@ -146,3 +148,66 @@ func TestAppendShouldMakeACopyOfTheInputSlice(t *testing.T) {
146148
c.Assert(result, qt.DeepEquals, []string{"a", "b", "c"})
147149
c.Assert(slice, qt.DeepEquals, []string{"d", "b"})
148150
}
151+
152+
func TestIndirect(t *testing.T) {
153+
t.Parallel()
154+
c := qt.New(t)
155+
156+
type testStruct struct {
157+
Field string
158+
}
159+
160+
var (
161+
nilPtr *testStruct
162+
nilIface interface{} = nil
163+
nonNilIface interface{} = &testStruct{Field: "hello"}
164+
)
165+
166+
tests := []struct {
167+
name string
168+
input any
169+
wantKind reflect.Kind
170+
wantNil bool
171+
}{
172+
{
173+
name: "nil pointer",
174+
input: nilPtr,
175+
wantKind: reflect.Ptr,
176+
wantNil: true,
177+
},
178+
{
179+
name: "nil interface",
180+
input: nilIface,
181+
wantKind: reflect.Invalid,
182+
wantNil: false,
183+
},
184+
{
185+
name: "non-nil pointer to struct",
186+
input: &testStruct{Field: "abc"},
187+
wantKind: reflect.Struct,
188+
wantNil: false,
189+
},
190+
{
191+
name: "non-nil interface holding pointer",
192+
input: nonNilIface,
193+
wantKind: reflect.Struct,
194+
wantNil: false,
195+
},
196+
{
197+
name: "plain value",
198+
input: testStruct{Field: "xyz"},
199+
wantKind: reflect.Struct,
200+
wantNil: false,
201+
},
202+
}
203+
204+
for _, tt := range tests {
205+
t.Run(tt.name, func(t *testing.T) {
206+
v := reflect.ValueOf(tt.input)
207+
got, isNil := indirect(v)
208+
209+
c.Assert(got.Kind(), qt.Equals, tt.wantKind)
210+
c.Assert(isNil, qt.Equals, tt.wantNil)
211+
})
212+
}
213+
}

common/collections/slice_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,37 @@ func TestSortedStringSlice(t *testing.T) {
136136
c.Assert(s.Count("z"), qt.Equals, 0)
137137
c.Assert(s.Count("a"), qt.Equals, 1)
138138
}
139+
140+
func TestStringSliceToInterfaceSlice(t *testing.T) {
141+
t.Parallel()
142+
c := qt.New(t)
143+
144+
tests := []struct {
145+
name string
146+
in []string
147+
want []any
148+
}{
149+
{
150+
name: "empty slice",
151+
in: []string{},
152+
want: []any{},
153+
},
154+
{
155+
name: "single element",
156+
in: []string{"hello"},
157+
want: []any{"hello"},
158+
},
159+
{
160+
name: "multiple elements",
161+
in: []string{"a", "b", "c"},
162+
want: []any{"a", "b", "c"},
163+
},
164+
}
165+
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
got := StringSliceToInterfaceSlice(tt.in)
169+
c.Assert(got, qt.DeepEquals, tt.want)
170+
})
171+
}
172+
}

common/collections/stack_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package collections
2+
3+
import (
4+
"testing"
5+
6+
qt "github.com/frankban/quicktest"
7+
)
8+
9+
func TestNewStack(t *testing.T) {
10+
t.Parallel()
11+
c := qt.New(t)
12+
13+
s := NewStack[int]()
14+
15+
c.Assert(s, qt.IsNotNil)
16+
}
17+
18+
func TestStackBasic(t *testing.T) {
19+
t.Parallel()
20+
c := qt.New(t)
21+
22+
s := NewStack[int]()
23+
24+
c.Assert(s.Len(), qt.Equals, 0)
25+
26+
s.Push(1)
27+
s.Push(2)
28+
s.Push(3)
29+
30+
c.Assert(s.Len(), qt.Equals, 3)
31+
32+
top, ok := s.Peek()
33+
c.Assert(ok, qt.Equals, true)
34+
c.Assert(top, qt.Equals, 3)
35+
36+
popped, ok := s.Pop()
37+
c.Assert(ok, qt.Equals, true)
38+
c.Assert(popped, qt.Equals, 3)
39+
40+
c.Assert(s.Len(), qt.Equals, 2)
41+
42+
_, _ = s.Pop()
43+
_, _ = s.Pop()
44+
_, ok = s.Pop()
45+
46+
c.Assert(ok, qt.Equals, false)
47+
}
48+
49+
func TestStackDrain(t *testing.T) {
50+
t.Parallel()
51+
c := qt.New(t)
52+
53+
s := NewStack[string]()
54+
s.Push("a")
55+
s.Push("b")
56+
57+
got := s.Drain()
58+
59+
c.Assert(got, qt.DeepEquals, []string{"a", "b"})
60+
c.Assert(s.Len(), qt.Equals, 0)
61+
}
62+
63+
func TestStackDrainMatching(t *testing.T) {
64+
t.Parallel()
65+
c := qt.New(t)
66+
67+
s := NewStack[int]()
68+
s.Push(1)
69+
s.Push(2)
70+
s.Push(3)
71+
s.Push(4)
72+
73+
got := s.DrainMatching(func(v int) bool { return v%2 == 0 })
74+
75+
c.Assert(got, qt.DeepEquals, []int{4, 2})
76+
c.Assert(s.Drain(), qt.DeepEquals, []int{1, 3})
77+
}

0 commit comments

Comments
 (0)