Skip to content

Commit

Permalink
add expect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hsldymq committed Apr 2, 2024
1 parent 4da598b commit 0f3aeef
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions funcs_set_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,63 @@ func TestIntersect(t *testing.T) {
t.Fatal(fmt.Sprintf("expect no elements, got: %v", len(case3Actual)))
}
}

func TestExcept(t *testing.T) {
// case 1
case1S1 := goiter.SliceElem([]int{1, 2, 3, 4, 5})
case1S2 := goiter.SliceElem([]int{4, 5, 6, 7, 8})
case1E := except[int](newEnumerator(case1S1), newEnumerator(case1S2))
case1Actual := make([]int, 0, 3)
for v := range case1E.Iter() {
case1Actual = append(case1Actual, v)
}
case1Expect := []int{1, 2, 3}
if !slices.Equal(case1Expect, case1Actual) {
t.Fatal(fmt.Sprintf("expect: %v, actual: %v", case1Expect, case1Actual))
}

// case 2
case2S1 := goiter.SliceElem([]*personWithID{
{"1", "Alice"},
{"2", "Bob"},
{"3", "Eve"},
{"4", "Charlie"},
{"5", "Helen"},
})
case2S2 := goiter.SliceElem([]*personWithID{
{"5", "Ivy"},
{"6", "Lily"},
})
case2E := except[*personWithID](newEnumerator(case2S1), newEnumerator(case2S2))
case2Actual := make([]personWithID, 0, 3)
for v := range case2E.Iter() {
case2Actual = append(case2Actual, *v)
if v.ID == "3" {
break
}
}
case2Expect := []personWithID{
{"1", "Alice"},
{"2", "Bob"},
{"3", "Eve"},
}
if !slices.Equal(case2Expect, case2Actual) {
t.Fatal(fmt.Sprintf("test intersect, expect: %v, actual: %v", case2Expect, case2Actual))
}

// case 3: func() int is not comparable
funcs := []func() int{
func() int { return 1 },
func() int { return 2 },
}
case3S1 := goiter.SliceElem([]func() int{funcs[0], funcs[1]})
case3S2 := goiter.SliceElem([]func() int{funcs[0], funcs[1]})
case3E := except[func() int](newEnumerator(case3S1), newEnumerator(case3S2))
case3Actual := make([]func() int, 0, 2)
for v := range case3E.Iter() {
case3Actual = append(case3Actual, v)
}
if len(case3Actual) != 2 {
t.Fatal(fmt.Sprintf("expect 2 elements, got: %v", len(case3Actual)))
}
}

0 comments on commit 0f3aeef

Please sign in to comment.