-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtake_while_test.go
More file actions
118 lines (94 loc) · 3.22 KB
/
Copy pathtake_while_test.go
File metadata and controls
118 lines (94 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package enumerators_test
import (
"testing"
"github.com/fgrzl/enumerators"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestShouldReturnElementsWhileConditionHoldsWhenInputProvided(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3, 4, 5, 1, 2})
lessThanFour := func(x int) bool { return x < 4 }
// Act
taken := enumerators.TakeWhile(input, lessThanFour)
result, err := enumerators.ToSlice(taken)
// Assert
assert.NoError(t, err)
// Should stop at 4 and return only elements before it
assert.Equal(t, []int{1, 2, 3}, result)
}
func TestShouldReturnEmptyWhenConditionHoldsForNoElements(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{})
alwaysTrue := func(x int) bool { return true }
// Act
taken := enumerators.TakeWhile(input, alwaysTrue)
result, err := enumerators.ToSlice(taken)
// Assert
assert.NoError(t, err)
assert.Empty(t, result)
}
func TestShouldReturnEmptyWhenConditionFailsForFirstElement(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{5, 6, 7, 8})
lessThanFive := func(x int) bool { return x < 5 }
// Act
taken := enumerators.TakeWhile(input, lessThanFive)
result, err := enumerators.ToSlice(taken)
// Assert
assert.NoError(t, err)
assert.Empty(t, result) // First element doesn't match, so none taken
}
func TestShouldReturnAllElementsWhenConditionHoldsForAll(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3, 4})
lessThanTen := func(x int) bool { return x < 10 }
// Act
taken := enumerators.TakeWhile(input, lessThanTen)
result, err := enumerators.ToSlice(taken)
// Assert
assert.NoError(t, err)
assert.Equal(t, []int{1, 2, 3, 4}, result)
}
func TestShouldReturnElementsWhileConditionHoldsWhenStringConditionProvided(t *testing.T) {
// Arrange
input := enumerators.Slice([]string{"a", "b", "c", "d", "A", "B"})
isLowercase := func(s string) bool { return len(s) == 1 && s[0] >= 'a' && s[0] <= 'z' }
// Act
taken := enumerators.TakeWhile(input, isLowercase)
result, err := enumerators.ToSlice(taken)
// Assert
assert.NoError(t, err)
assert.Equal(t, []string{"a", "b", "c", "d"}, result) // Stops at first uppercase
}
func TestShouldAllowStepByStepIterationWhenConditionHolds(t *testing.T) {
input := enumerators.Slice([]int{2, 4, 6, 7, 8})
isEven := func(x int) bool { return x%2 == 0 }
taken := enumerators.TakeWhile(input, isEven)
// Stops before 7 (odd); TakeWhile does not yield 7.
assertIntEnumeratorYields(t, taken, []int{2, 4, 6})
}
func TestShouldReturnSingleElementWhenConditionHoldsForOneElement(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{5})
alwaysTrue := func(x int) bool { return true }
// Act
taken := enumerators.TakeWhile(input, alwaysTrue)
result, err := enumerators.ToSlice(taken)
// Assert
assert.NoError(t, err)
assert.Equal(t, []int{5}, result)
}
func TestShouldAllowDisposeWhenTakeWhileUsed(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3})
alwaysTrue := func(x int) bool { return true }
taken := enumerators.TakeWhile(input, alwaysTrue)
// Act
taken.Dispose() // Should not panic
// Assert - should still work
assert.True(t, taken.MoveNext())
current, err := taken.Current()
require.NoError(t, err)
assert.Equal(t, 1, current)
}