-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules_iterator_test.go
85 lines (68 loc) · 1.62 KB
/
rules_iterator_test.go
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
package validator
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
ve "github.com/donatorsky/go-validator/error"
vr "github.com/donatorsky/go-validator/rule"
)
func Test_rulesIterator_CanIterate(t *testing.T) {
// given
var (
ruleMock1 = ruleMock{1}
ruleMock2 = ruleMock{2}
ruleMock3 = ruleMock{3}
)
for ttName, tt := range map[string]struct {
rules []vr.Rule
expectedRules []vr.Rule
}{
"empty slice": {
rules: nil,
expectedRules: []vr.Rule{},
},
"non-empty slice": {
rules: []vr.Rule{ruleMock1, ruleMock2, ruleMock3},
expectedRules: []vr.Rule{ruleMock1, ruleMock2, ruleMock3},
},
} {
t.Run(ttName, func(t *testing.T) {
var rules = make([]vr.Rule, 0, len(tt.expectedRules))
i := newRulesIterator(tt.rules)
// when
for i.Valid() {
rules = append(rules, i.Current())
i.Next(context.TODO(), nil, nil)
}
// then
require.Equal(t, tt.expectedRules, rules)
})
}
}
func Test_rulesIterator_ReturnsNilForEmptyIterator(t *testing.T) {
// given
i := newRulesIterator(nil)
// then
require.Nil(t, i.Current())
require.False(t, i.Valid())
require.Equal(t, 0, i.index)
// when
i.Next(context.TODO(), nil, nil)
// then
require.Nil(t, i.Current())
require.False(t, i.Valid())
require.Equal(t, 0, i.index)
}
type ruleMock struct {
id int
}
func (ruleMock) Apply(_ context.Context, _ any, _ any) (newValue any, err ve.ValidationError) {
panic("unexpected method call")
}
func (ruleMock) Bails() bool {
panic("unexpected method call")
}
func (r ruleMock) String() string {
return fmt.Sprintf("ruleMock{id=%d}", r.id)
}