Repository navigation
Expand file tree
/
Copy pathfilter_test.go
More file actions
168 lines (140 loc) · 4.43 KB
/
Copy pathfilter_test.go
File metadata and controls
168 lines (140 loc) · 4.43 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package result
import (
"errors"
"testing"
)
func TestFilter(t *testing.T) {
t.Run("passes when predicate is true", func(t *testing.T) {
result := Ok(42).Filter(func(n int) bool {
return n > 0
}, errors.New("number must be positive"))
if !result.IsOk() {
t.Error("Filter should pass when predicate returns true")
}
if result.Unwrap() != 42 {
t.Errorf("value = %d, want 42", result.Unwrap())
}
})
t.Run("fails when predicate is false", func(t *testing.T) {
expectedErr := errors.New("number must be positive")
result := Ok(-5).Filter(func(n int) bool {
return n > 0
}, expectedErr)
if !result.IsErr() {
t.Error("Filter should fail when predicate returns false")
}
if result.UnwrapErr() != expectedErr {
t.Errorf("error = %v, want %v", result.UnwrapErr(), expectedErr)
}
})
t.Run("propagates existing error", func(t *testing.T) {
originalErr := errors.New("original error")
result := Err[int](originalErr).Filter(func(n int) bool {
return true
}, errors.New("new error"))
if !result.IsErr() {
t.Error("Filter should propagate error")
}
if result.UnwrapErr() != originalErr {
t.Errorf("error = %v, want %v", result.UnwrapErr(), originalErr)
}
})
t.Run("preserves metadata", func(t *testing.T) {
result := Ok(42).
WithOp("test").
WithMeta("key", "value").
Filter(func(n int) bool {
return n < 0
}, errors.New("failed"))
if result.op != "test" {
t.Errorf("op = %s, want test", result.op)
}
if result.meta["key"] != "value" {
t.Error("metadata not preserved")
}
})
}
func TestFilterNot(t *testing.T) {
t.Run("passes when predicate is false", func(t *testing.T) {
result := Ok(42).FilterNot(func(n int) bool {
return n < 0
}, errors.New("number must not be negative"))
if !result.IsOk() {
t.Error("FilterNot should pass when predicate returns false")
}
})
t.Run("fails when predicate is true", func(t *testing.T) {
result := Ok(-5).FilterNot(func(n int) bool {
return n < 0
}, errors.New("number must not be negative"))
if !result.IsErr() {
t.Error("FilterNot should fail when predicate returns true")
}
})
}
func TestWhen(t *testing.T) {
t.Run("passes when condition is true", func(t *testing.T) {
result := Ok(42).When(true, errors.New("condition failed"))
if !result.IsOk() {
t.Error("When should pass when condition is true")
}
if result.Unwrap() != 42 {
t.Errorf("value = %d, want 42", result.Unwrap())
}
})
t.Run("fails when condition is false", func(t *testing.T) {
expectedErr := errors.New("condition failed")
result := Ok(42).When(false, expectedErr)
if !result.IsErr() {
t.Error("When should fail when condition is false")
}
if result.UnwrapErr() != expectedErr {
t.Errorf("error = %v, want %v", result.UnwrapErr(), expectedErr)
}
})
t.Run("propagates existing error", func(t *testing.T) {
originalErr := errors.New("original error")
result := Err[int](originalErr).When(true, errors.New("new error"))
if result.UnwrapErr() != originalErr {
t.Errorf("error = %v, want %v", result.UnwrapErr(), originalErr)
}
})
}
func TestUnless(t *testing.T) {
t.Run("passes when condition is false", func(t *testing.T) {
result := Ok(42).Unless(false, errors.New("condition must be false"))
if !result.IsOk() {
t.Error("Unless should pass when condition is false")
}
})
t.Run("fails when condition is true", func(t *testing.T) {
result := Ok(42).Unless(true, errors.New("condition must be false"))
if !result.IsErr() {
t.Error("Unless should fail when condition is true")
}
})
}
func TestFilterChaining(t *testing.T) {
t.Run("chains multiple filters successfully", func(t *testing.T) {
result := Ok(42).
Filter(func(n int) bool { return n > 0 }, errors.New("must be positive")).
Filter(func(n int) bool { return n < 100 }, errors.New("must be less than 100")).
Filter(func(n int) bool { return n%2 == 0 }, errors.New("must be even"))
if !result.IsOk() {
t.Errorf("chained filters should pass, got error: %v", result.UnwrapErr())
}
})
t.Run("fails at first filter violation", func(t *testing.T) {
err1 := errors.New("must be positive")
err2 := errors.New("must be less than 100")
result := Ok(-5).
Filter(func(n int) bool { return n > 0 }, err1).
Filter(func(n int) bool { return n < 100 }, err2)
if !result.IsErr() {
t.Error("should fail at first filter")
}
if result.UnwrapErr() != err1 {
t.Errorf("error = %v, want %v", result.UnwrapErr(), err1)
}
})
}