forked from jieliu2000/anyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin_validators_test.go
106 lines (90 loc) · 2.74 KB
/
builtin_validators_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package anyi
import (
"testing"
"github.com/jieliu2000/anyi/flow"
"github.com/stretchr/testify/assert"
)
func TestStringValidator_Init(t *testing.T) {
validator := &StringValidator{
EqualTo: "foo",
}
err := validator.Init()
assert.NoError(t, err, "expected no error, got %s", err)
}
func TestStringValidator_Init_MatchRegex(t *testing.T) {
validator := &StringValidator{
MatchRegex: "[a-z",
}
err := validator.Init()
assert.Error(t, err, "expected error, got nil")
}
func TestStringValidator_Init_BothSet(t *testing.T) {
validator := &StringValidator{
EqualTo: "foo",
MatchRegex: "[a-z]*",
}
err := validator.Init()
assert.Error(t, err, "expected error, got nil")
assert.EqualError(t, err, "StringValidator should have either EqualTo or MatchRegex set, not both")
}
func TestStringValidator_Init_NeitherSet(t *testing.T) {
validator := &StringValidator{}
err := validator.Init()
assert.Error(t, err, "expected error, got nil")
assert.EqualError(t, err, "StringValidator should have either EqualTo or MatchRegex set")
}
func TestStringValidator_Validate(t *testing.T) {
t.Run("EqualTo - Valid", func(t *testing.T) {
validator := StringValidator{
EqualTo: "expected output",
}
step := &flow.Step{}
assert.True(t, validator.Validate("expected output", step))
})
t.Run("EqualTo - Invalid", func(t *testing.T) {
validator := StringValidator{
EqualTo: "expected output",
}
step := &flow.Step{}
assert.False(t, validator.Validate("wrong output", step))
})
t.Run("MatchRegex - Valid", func(t *testing.T) {
validator := StringValidator{
MatchRegex: "[a-z]*",
}
step := &flow.Step{}
assert.True(t, validator.Validate("expected output", step))
})
t.Run("MatchRegex - Invalid", func(t *testing.T) {
validator := StringValidator{
MatchRegex: "^[a-z]*$", // only allow lowercase
}
step := &flow.Step{}
assert.False(t, validator.Validate("1234567890", step))
})
}
func TestStringValidator_Validate_InvalidRegex(t *testing.T) {
validator := StringValidator{
MatchRegex: "[", // Invalid regular expression
}
step := &flow.Step{}
assert.False(t, validator.Validate("any output", step))
}
func TestStringValidator_Validate_NilStep(t *testing.T) {
validator := StringValidator{
EqualTo: "expected output",
}
assert.False(t, validator.Validate("wrong output", nil))
}
func TestStringValidator_Validate_NilValues(t *testing.T) {
validator := StringValidator{}
step := &flow.Step{}
assert.False(t, validator.Validate("any output", step))
}
func TestJsonValidator_Validate(t *testing.T) {
validator := &JsonValidator{}
step := &flow.Step{}
assert.False(t, validator.Validate("", step))
assert.False(t, validator.Validate("not json string", step))
assert.True(t, validator.Validate(`{"key": "value"}`, step))
}