-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbool_test.go
118 lines (90 loc) · 2.6 KB
/
bool_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
107
108
109
110
111
112
113
114
115
116
117
118
package validator_test
import (
"errors"
"github.com/gungun974/validator"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type FakeTrueBoolValidator struct{}
func (v FakeTrueBoolValidator) Validate(_ bool) error {
return nil
}
type FakeErrorBoolValidator struct{}
func (v FakeErrorBoolValidator) Validate(_ bool) error {
return errors.New("this bool validator always fail")
}
func boolValidatorTests() {
Describe("ValidateBool", func() {
It("should return a bool", func() {
// arrange
value := true
// act
result, err := validator.ValidateBool(value, validator.BoolValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal(true))
})
It("should not return an bool when input is garbage", func() {
// arrange
value := "garbage"
// act
_, err := validator.ValidateBool(value, validator.BoolValidators{})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("value is not a bool"))
})
It("should return a bool when input string is \"true\"", func() {
// arrange
value := "true"
// act
result, err := validator.ValidateBool(value, validator.BoolValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal(true))
})
It("should return a bool when input string is \"on\" for HTML form suppport", func() {
// arrange
value := "on"
// act
result, err := validator.ValidateBool(value, validator.BoolValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal(true))
})
It("should return a bool when input string is \"false\"", func() {
// arrange
value := "false"
// act
result, err := validator.ValidateBool(value, validator.BoolValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal(false))
})
It("should return a bool when all rules are satisfied", func() {
// arrange
value := false
// act
result, err := validator.ValidateBool(value, validator.BoolValidators{
FakeTrueBoolValidator{},
FakeTrueBoolValidator{},
FakeTrueBoolValidator{},
})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal(false))
})
It("should not return a bool when one rule is not satisfied", func() {
// arrange
value := true
// act
_, err := validator.ValidateBool(value, validator.BoolValidators{
FakeTrueBoolValidator{},
FakeErrorBoolValidator{},
FakeTrueBoolValidator{},
})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("this bool validator always fail"))
})
})
}