Skip to content

Commit

Permalink
Merge pull request #3 from cuonglm/main
Browse files Browse the repository at this point in the history
Simplify ForEach test using generic
  • Loading branch information
ledongthuc authored Jan 3, 2022
2 parents daca286 + 875cf3d commit 33f384b
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions foreach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,24 @@ package goterators

import "testing"

func TestForEachInt(t *testing.T) {
testSource := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}

indexCounter := 0
ForEach(testSource, func(item int) {
if indexCounter >= len(testSource) {
t.Fatalf("Expected max index = %v, but got index = %v", len(testSource), indexCounter)
}
if testSource[indexCounter] != item {
t.Errorf("Index %v - Expected: %+v, but got: %+v", indexCounter, testSource[indexCounter], item)
}
indexCounter++
})

if indexCounter != len(testSource) {
t.Fatalf("Expected number of items = %v, but got = %v", len(testSource), indexCounter)
}
func TestForEach(t *testing.T) {
testForEach([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, t)
testForEach([]string{"basis", "pray", "platform", "top", "border", "pole", "kidnap", "eaux", "stream", "lemon", "helpless", "indulge", "highlight", "city", "miner", "photography", "squeeze", "king", "offender", "rugby"}, t)
}

func TestForEachString(t *testing.T) {
testSource := []string{"basis", "pray", "platform", "top", "border", "pole", "kidnap", "eaux", "stream", "lemon", "helpless", "indulge", "highlight", "city", "miner", "photography", "squeeze", "king", "offender", "rugby"}

func testForEach[K comparable](source []K, t *testing.T) {
indexCounter := 0
ForEach(testSource, func(item string) {
if indexCounter >= len(testSource) {
t.Fatalf("Expected max index = %v, but got index = %v", len(testSource), indexCounter)
ForEach(source, func(item K) {
if indexCounter >= len(source) {
t.Fatalf("Expected max index = %v, but got index = %v", len(source), indexCounter)
}
if testSource[indexCounter] != item {
t.Errorf("Index %v - Expected: %+v, but got: %+v", indexCounter, testSource[indexCounter], item)
if source[indexCounter] != item {
t.Errorf("Index %v - Expected: %+v, but got: %+v", indexCounter, source[indexCounter], item)
}
indexCounter++
})

if indexCounter != len(testSource) {
t.Fatalf("Expected number of items = %v, but got = %v", len(testSource), indexCounter)
if indexCounter != len(source) {
t.Fatalf("Expected number of items = %v, but got = %v", len(source), indexCounter)
}
}

0 comments on commit 33f384b

Please sign in to comment.