Skip to content

Commit

Permalink
add List tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hsldymq committed Apr 1, 2024
1 parent 80e8b6f commit 6101bc7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ func TestList_SkipLast(t *testing.T) {
}

func TestList_Distinct(t *testing.T) {
// case 1
list := NewList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5)
actual := []int{}
for v := range list.Distinct().Iter() {
Expand All @@ -698,6 +699,7 @@ func TestList_Distinct(t *testing.T) {
t.Fatalf("test List.Distinct expect: %v, actual: %v", expect, actual)
}

// case 2
f := func() {}
list2 := NewList(f, f, f)
actual2 := []func(){}
Expand All @@ -708,4 +710,38 @@ func TestList_Distinct(t *testing.T) {
if expect2 != len(actual2) {
t.Fatalf("test List.Distinct, should return 3 elements")
}

// case 3
list3 := NewList(
&personWithID{ID: "1", Name: "Alice"},
&personWithID{ID: "2", Name: "Bob"},
&personWithID{ID: "3", Name: "Eve"},
&personWithID{ID: "2", Name: "Robert"},
&personWithID{ID: "1", Name: "Ellie"},
&personWithID{ID: "3", Name: "Eva"},
)
actual3 := []personWithID{}
for v := range list3.Distinct().Iter() {
actual3 = append(actual3, *v)
if v.Name == "Ellie" {
break
}
}
expect3 := []personWithID{
{ID: "1", Name: "Alice"},
{ID: "2", Name: "Bob"},
{ID: "3", Name: "Eve"},
}
if !slices.Equal(expect3, actual3) {
t.Fatalf("test List.Distinct expect: %v, actual: %v", expect3, actual3)
}
}

type personWithID struct {
ID string
Name string
}

func (p *personWithID) ComparingKey() any {
return p.ID
}

0 comments on commit 6101bc7

Please sign in to comment.