-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper_test.go
More file actions
147 lines (120 loc) · 3.11 KB
/
helper_test.go
File metadata and controls
147 lines (120 loc) · 3.11 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
package fn
import "testing"
func TestValid_Int(t *testing.T) {
if Valid(0) {
t.Error("0 should not be valid")
}
if !Valid(42) {
t.Error("42 should be valid")
}
if !Valid(-1) {
t.Error("-1 should be valid")
}
}
func TestValid_String(t *testing.T) {
if Valid("") {
t.Error("empty string should not be valid")
}
if !Valid("hello") {
t.Error("non-empty string should be valid")
}
}
func TestValid_Pointer(t *testing.T) {
var p *int
if Valid(p) {
t.Error("nil pointer should not be valid")
}
if !Valid(new(42)) {
t.Error("non-nil pointer should be valid")
}
}
func TestValidReflect_Slice(t *testing.T) {
if ValidReflect([]int{}) {
t.Error("empty slice should not be valid")
}
if !ValidReflect([]int{1, 2, 3}) {
t.Error("non-empty slice should be valid")
}
}
func TestValidReflect_Map(t *testing.T) {
if ValidReflect(map[string]int{}) {
t.Error("empty map should not be valid")
}
if !ValidReflect(map[string]int{"a": 1}) {
t.Error("non-empty map should be valid")
}
}
func TestValidReflect_Int(t *testing.T) {
if ValidReflect(0) {
t.Error("0 should not be valid")
}
if !ValidReflect(42) {
t.Error("42 should be valid")
}
}
type boolImpl struct{ v bool }
func (b boolImpl) Bool() bool { return b.v }
func TestValidReflect_BoolInterface(t *testing.T) {
if ValidReflect(boolImpl{false}) {
t.Error("Bool() false should not be valid")
}
if !ValidReflect(boolImpl{true}) {
t.Error("Bool() true should be valid")
}
}
type okImpl struct{ v bool }
func (o okImpl) Ok() bool { return o.v }
func TestValidReflect_OkInterface(t *testing.T) {
if ValidReflect(okImpl{false}) {
t.Error("Ok() false should not be valid")
}
if !ValidReflect(okImpl{true}) {
t.Error("Ok() true should be valid")
}
}
type validateErrImpl struct{ err error }
func (v validateErrImpl) Validate() error { return v.err }
func TestValidReflect_ValidateErrorInterface(t *testing.T) {
if ValidReflect(validateErrImpl{err: nil}) != true {
t.Error("Validate() nil should be valid")
}
if ValidReflect(validateErrImpl{err: errTest}) {
t.Error("Validate() error should not be valid")
}
}
var errTest = &testError{}
type testError struct{}
func (e *testError) Error() string { return "test" }
type validateBoolImpl struct{ v bool }
func (v validateBoolImpl) Validate() bool { return v.v }
func TestValidReflect_ValidateBoolInterface(t *testing.T) {
if ValidReflect(validateBoolImpl{false}) {
t.Error("Validate() false should not be valid")
}
if !ValidReflect(validateBoolImpl{true}) {
t.Error("Validate() true should be valid")
}
}
type isZeroImpl struct{ zero bool }
func (i isZeroImpl) IsZero() bool { return i.zero }
func TestValidReflect_IsZeroInterface(t *testing.T) {
if ValidReflect(isZeroImpl{true}) {
t.Error("IsZero() true should not be valid")
}
if !ValidReflect(isZeroImpl{false}) {
t.Error("IsZero() false should be valid")
}
}
func TestSafeDeref_NonNil(t *testing.T) {
result := Deref(new(42), 99)
if result != 42 {
t.Errorf("expected 42, got %d", result)
}
}
func TestSafeDeref_Nil(t *testing.T) {
var p *int
result := Deref(p, 99)
if result != 99 {
t.Errorf("expected 99, got %d", result)
}
}