Skip to content

Commit

Permalink
add List tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Nov 3, 2024
1 parent 7ab92b1 commit 031ad85
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package gp

import (
"testing"
)

func TestMakeList(t *testing.T) {
tests := []struct {
name string
args []any
wantLen int
wantVals []any
}{
{
name: "empty list",
args: []any{},
wantLen: 0,
wantVals: []any{},
},
{
name: "integers",
args: []any{1, 2, 3},
wantLen: 3,
wantVals: []any{1, 2, 3},
},
{
name: "mixed types",
args: []any{1, "hello", 3.14},
wantLen: 3,
wantVals: []any{1, "hello", 3.14},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
list := MakeList(tt.args...)

if got := list.Len(); got != tt.wantLen {
t.Errorf("MakeList() len = %v, want %v", got, tt.wantLen)
}

for i, want := range tt.wantVals {
got := list.GetItem(i).String()
if got != From(want).String() {
t.Errorf("MakeList() item[%d] = %v, want %v", i, got, want)
}
}
})
}
}

func TestList_SetItem(t *testing.T) {
list := MakeList(1, 2, 3)
list.SetItem(1, From("test"))

// Get the raw value without quotes for comparison
got := list.GetItem(1).String()

if got != "test" {
t.Errorf("List.SetItem() = %v, want %v", got, "test")
}
}

func TestList_Append(t *testing.T) {
list := MakeList(1, 2)
initialLen := list.Len()

list.Append(From(3))

if got := list.Len(); got != initialLen+1 {
t.Errorf("List.Append() length = %v, want %v", got, initialLen+1)
}

if got := list.GetItem(2).String(); got != From(3).String() {
t.Errorf("List.Append() last item = %v, want %v", got, From(3).String())
}
}

func TestList_Len(t *testing.T) {
tests := []struct {
name string
args []any
want int
}{
{"empty list", []any{}, 0},
{"single item", []any{1}, 1},
{"multiple items", []any{1, 2, 3}, 3},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
list := MakeList(tt.args...)
if got := list.Len(); got != tt.want {
t.Errorf("List.Len() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 031ad85

Please sign in to comment.