]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
common/collections: Increase test coverage
authorRuslan Semagin <pixel.365.24@gmail.com>
Tue, 27 May 2025 12:17:21 +0000 (15:17 +0300)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 29 May 2025 07:17:13 +0000 (09:17 +0200)
common/collections/append_test.go
common/collections/slice_test.go
common/collections/stack_test.go [new file with mode: 0644]

index f791b731a041c62df4c6dfdcb4e5184a28bfea76..62d9015cef3558c6fd3b16917b9cd56d448af75c 100644 (file)
@@ -15,6 +15,7 @@ package collections
 
 import (
        "html/template"
+       "reflect"
        "testing"
 
        qt "github.com/frankban/quicktest"
@@ -77,6 +78,7 @@ func TestAppend(t *testing.T) {
                {[]string{"a", "b"}, []any{nil}, []any{"a", "b", nil}},
                {[]string{"a", "b"}, []any{nil, "d", nil}, []any{"a", "b", nil, "d", nil}},
                {[]any{"a", nil, "c"}, []any{"d", nil, "f"}, []any{"a", nil, "c", "d", nil, "f"}},
+               {[]string{"a", "b"}, []any{}, []string{"a", "b"}},
        } {
 
                result, err := Append(test.start, test.addend...)
@@ -146,3 +148,66 @@ func TestAppendShouldMakeACopyOfTheInputSlice(t *testing.T) {
        c.Assert(result, qt.DeepEquals, []string{"a", "b", "c"})
        c.Assert(slice, qt.DeepEquals, []string{"d", "b"})
 }
+
+func TestIndirect(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       type testStruct struct {
+               Field string
+       }
+
+       var (
+               nilPtr      *testStruct
+               nilIface    interface{} = nil
+               nonNilIface interface{} = &testStruct{Field: "hello"}
+       )
+
+       tests := []struct {
+               name     string
+               input    any
+               wantKind reflect.Kind
+               wantNil  bool
+       }{
+               {
+                       name:     "nil pointer",
+                       input:    nilPtr,
+                       wantKind: reflect.Ptr,
+                       wantNil:  true,
+               },
+               {
+                       name:     "nil interface",
+                       input:    nilIface,
+                       wantKind: reflect.Invalid,
+                       wantNil:  false,
+               },
+               {
+                       name:     "non-nil pointer to struct",
+                       input:    &testStruct{Field: "abc"},
+                       wantKind: reflect.Struct,
+                       wantNil:  false,
+               },
+               {
+                       name:     "non-nil interface holding pointer",
+                       input:    nonNilIface,
+                       wantKind: reflect.Struct,
+                       wantNil:  false,
+               },
+               {
+                       name:     "plain value",
+                       input:    testStruct{Field: "xyz"},
+                       wantKind: reflect.Struct,
+                       wantNil:  false,
+               },
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       v := reflect.ValueOf(tt.input)
+                       got, isNil := indirect(v)
+
+                       c.Assert(got.Kind(), qt.Equals, tt.wantKind)
+                       c.Assert(isNil, qt.Equals, tt.wantNil)
+               })
+       }
+}
index 20961aac02027da10b264093be58523be86939f0..4008a5e6cb6da0043c33253341974b027c9246ac 100644 (file)
@@ -136,3 +136,37 @@ func TestSortedStringSlice(t *testing.T) {
        c.Assert(s.Count("z"), qt.Equals, 0)
        c.Assert(s.Count("a"), qt.Equals, 1)
 }
+
+func TestStringSliceToInterfaceSlice(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       tests := []struct {
+               name string
+               in   []string
+               want []any
+       }{
+               {
+                       name: "empty slice",
+                       in:   []string{},
+                       want: []any{},
+               },
+               {
+                       name: "single element",
+                       in:   []string{"hello"},
+                       want: []any{"hello"},
+               },
+               {
+                       name: "multiple elements",
+                       in:   []string{"a", "b", "c"},
+                       want: []any{"a", "b", "c"},
+               },
+       }
+
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       got := StringSliceToInterfaceSlice(tt.in)
+                       c.Assert(got, qt.DeepEquals, tt.want)
+               })
+       }
+}
diff --git a/common/collections/stack_test.go b/common/collections/stack_test.go
new file mode 100644 (file)
index 0000000..965d4db
--- /dev/null
@@ -0,0 +1,77 @@
+package collections
+
+import (
+       "testing"
+
+       qt "github.com/frankban/quicktest"
+)
+
+func TestNewStack(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       s := NewStack[int]()
+
+       c.Assert(s, qt.IsNotNil)
+}
+
+func TestStackBasic(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       s := NewStack[int]()
+
+       c.Assert(s.Len(), qt.Equals, 0)
+
+       s.Push(1)
+       s.Push(2)
+       s.Push(3)
+
+       c.Assert(s.Len(), qt.Equals, 3)
+
+       top, ok := s.Peek()
+       c.Assert(ok, qt.Equals, true)
+       c.Assert(top, qt.Equals, 3)
+
+       popped, ok := s.Pop()
+       c.Assert(ok, qt.Equals, true)
+       c.Assert(popped, qt.Equals, 3)
+
+       c.Assert(s.Len(), qt.Equals, 2)
+
+       _, _ = s.Pop()
+       _, _ = s.Pop()
+       _, ok = s.Pop()
+
+       c.Assert(ok, qt.Equals, false)
+}
+
+func TestStackDrain(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       s := NewStack[string]()
+       s.Push("a")
+       s.Push("b")
+
+       got := s.Drain()
+
+       c.Assert(got, qt.DeepEquals, []string{"a", "b"})
+       c.Assert(s.Len(), qt.Equals, 0)
+}
+
+func TestStackDrainMatching(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+
+       s := NewStack[int]()
+       s.Push(1)
+       s.Push(2)
+       s.Push(3)
+       s.Push(4)
+
+       got := s.DrainMatching(func(v int) bool { return v%2 == 0 })
+
+       c.Assert(got, qt.DeepEquals, []int{4, 2})
+       c.Assert(s.Drain(), qt.DeepEquals, []int{1, 3})
+}