-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforeach_test.go
More file actions
149 lines (123 loc) · 2.96 KB
/
Copy pathforeach_test.go
File metadata and controls
149 lines (123 loc) · 2.96 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package enumerators_test
import (
"testing"
"github.com/fgrzl/enumerators"
"github.com/stretchr/testify/assert"
)
func TestForEach_BasicIteration(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3, 4, 5})
var collected []int
// Act
err := enumerators.ForEach(input, func(x int) error {
collected = append(collected, x)
return nil
})
// Assert
assert.NoError(t, err)
assert.Equal(t, []int{1, 2, 3, 4, 5}, collected)
}
func TestForEach_EmptyEnumerator(t *testing.T) {
// Arrange
input := enumerators.Empty[int]()
callCount := 0
// Act
err := enumerators.ForEach(input, func(x int) error {
callCount++
return nil
})
// Assert
assert.NoError(t, err)
assert.Equal(t, 0, callCount)
}
func TestForEach_SingleElement(t *testing.T) {
// Arrange
input := enumerators.Slice([]string{"hello"})
var result string
// Act
err := enumerators.ForEach(input, func(s string) error {
result = s + "-processed"
return nil
})
// Assert
assert.NoError(t, err)
assert.Equal(t, "hello-processed", result)
}
func TestForEach_ActionError(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3, 4, 5})
var collected []int
expectedError := assert.AnError
// Act
err := enumerators.ForEach(input, func(x int) error {
collected = append(collected, x)
if x == 3 {
return expectedError
}
return nil
})
// Assert
assert.Error(t, err)
assert.Equal(t, expectedError, err)
assert.Equal(t, []int{1, 2, 3}, collected) // Should process up to the error
}
func TestForEach_WithSideEffects(t *testing.T) {
// Arrange
input := enumerators.Slice([]int{1, 2, 3})
sum := 0
count := 0
// Act
err := enumerators.ForEach(input, func(x int) error {
sum += x
count++
return nil
})
// Assert
assert.NoError(t, err)
assert.Equal(t, 6, sum) // 1 + 2 + 3
assert.Equal(t, 3, count) // 3 elements
}
func TestForEach_DisposesEnumerator(t *testing.T) {
// Arrange
disposed := false
input := enumerators.Cleanup(
enumerators.Slice([]int{1, 2, 3}),
func() { disposed = true },
)
// Act
err := enumerators.ForEach(input, func(x int) error { return nil })
// Assert
assert.NoError(t, err)
assert.True(t, disposed, "Enumerator should be disposed after ForEach")
}
func TestForEach_DisposesOnError(t *testing.T) {
// Arrange
disposed := false
input := enumerators.Cleanup(
enumerators.Slice([]int{1, 2, 3}),
func() { disposed = true },
)
// Act
err := enumerators.ForEach(input, func(x int) error {
if x == 2 {
return assert.AnError
}
return nil
})
// Assert
assert.Error(t, err)
assert.True(t, disposed, "Enumerator should be disposed even when error occurs")
}
func TestForEach_ProcessingOrder(t *testing.T) {
// Arrange
input := enumerators.Slice([]string{"first", "second", "third"})
var order []string
// Act
err := enumerators.ForEach(input, func(s string) error {
order = append(order, s)
return nil
})
// Assert
assert.NoError(t, err)
assert.Equal(t, []string{"first", "second", "third"}, order)
}