forked from Antipitch/orwell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
required_test.go
76 lines (73 loc) · 1.58 KB
/
required_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
package orwell
import (
"testing"
)
func TestApplyRequired(t *testing.T) {
r := &required{msg: "test"}
t.Run("Nil value", func(t *testing.T) {
if err := r.Apply(nil); err == nil {
t.Error("Expected error")
}
})
t.Run("Valid int", func(t *testing.T) {
if err := r.Apply(1); err != nil {
t.Error("Expected valid")
}
})
t.Run("Invalid int", func(t *testing.T) {
if err := r.Apply(0); err == nil {
t.Error("Expected error")
}
})
t.Run("Valid string", func(t *testing.T) {
if err := r.Apply("1"); err != nil {
t.Error("Expected valid")
}
})
t.Run("Invalid string", func(t *testing.T) {
if err := r.Apply(""); err == nil {
t.Error("Expected error")
}
})
t.Run("Valid Pointer", func(t *testing.T) {
i := 1
if err := r.Apply(&i); err != nil {
t.Error("Expected valid")
}
})
t.Run("Invalid Pointer", func(t *testing.T) {
var i *int
if err := r.Apply(i); err == nil {
t.Error("Expected error")
}
})
t.Run("Valid Map", func(t *testing.T) {
m := make(map[string]int)
m["test"] = 1
if err := r.Apply(m); err != nil {
t.Error("Expected valid")
}
})
t.Run("Invalid Map", func(t *testing.T) {
m := make(map[string]int)
if err := r.Apply(m); err == nil {
t.Error("Expected error")
}
})
t.Run("Valid Struct", func(t *testing.T) {
type TestStruct struct {
test int
}
if err := r.Apply(TestStruct{test: 1}); err != nil {
t.Error("Expected valid")
}
})
t.Run("Invalid Struct", func(t *testing.T) {
type TestStruct struct {
test int
}
if err := r.Apply(TestStruct{}); err == nil {
t.Error("Expected error")
}
})
}