-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfetch_field_test.go
108 lines (91 loc) · 3.23 KB
/
fetch_field_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
package checker
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type sizeInt int
type field struct {
Name string
Email *string
LinkedList Node
Salary float64
Age uint
Size sizeInt
BirthDate time.Time
Comp Comparable
}
type Node struct {
Val int
Next *Node
}
func TestFetchFieldInStruct(t *testing.T) {
email := "yaopei.liang@foxmail.com"
name := "yaopei"
linkedlist := Node{
Val: 1,
Next: &Node{
Val: 2,
Next: &Node{
Val: 3,
Next: nil,
},
},
}
f := field{Name: name, Email: &email, LinkedList: linkedlist}
fName, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "Name"}})
fEmail, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "Email"}})
node1Val, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "LinkedList.Val"}})
node2Val, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "LinkedList.Next.Val"}})
node3Val, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "LinkedList.Next.Next.Val"}})
nodeNotNil, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "LinkedList.Next.Next"}})
nodeNil, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "LinkedList.Next.Next.Next.Val"}})
assert.Equal(t, name, fName, "error name")
assert.Equal(t, email, fEmail, "error email")
assert.Equal(t, 1, node1Val, "error node1Val")
assert.Equal(t, 2, node2Val, "error node2Val")
assert.Equal(t, 3, node3Val, "error node3Val")
assert.NotNil(t, nodeNotNil, "error nodeNotNil")
assert.Equal(t, nil, nodeNil, "error nodeNil")
}
func TestFetchFieldStr(t *testing.T) {
email := "yaopei.liang@foxmail.com"
name := "yaopei"
f := field{Name: name, Email: &email}
fName, _, _ := fetchFieldStr(f, &ruleWrapper{baseRule{fieldExpr: "Name", name: "ruleWrapper"}})
fEmail, _ := fetchField(f, &ruleWrapper{baseRule{fieldExpr: "Email"}})
assert.Equal(t, fName, name, "error name")
assert.Equal(t, fEmail, email, "error email")
}
func TestFetchFieldStrErrPrompt(t *testing.T) {
email := "yaopei.liang@foxmail.com"
name := "yaopei"
f := field{Name: name, Email: &email}
_, isValid, errMsg := fetchFieldStr(f, &ruleWrapper{baseRule{fieldExpr: "Name.Name", name: "ruleWrapper"}})
assert.Equal(t, false, isValid, "wrong fetchField")
assert.Equal(t, "[ruleWrapper]:Name.Name cannot be found", errMsg, "wrong errMsg")
}
func TestFetchFieldInt(t *testing.T) {
size := 100
f := field{Size: sizeInt(size)}
fSize, _, errMsg := fetchFieldInt(f, &ruleWrapper{baseRule{fieldExpr: "Size", name: "ruleWrapper"}})
assert.Equal(t, fSize, size, errMsg)
}
func TestFetchFieldUInt(t *testing.T) {
var age uint = 35
f := field{Age: 35}
fAge, _, errMsg := fetchFieldUint(f, &ruleWrapper{baseRule{fieldExpr: "Age", name: "ruleWrapper"}})
assert.Equal(t, fAge, age, errMsg)
}
func TestFetchFieldFloat(t *testing.T) {
salary := 100.0
f := field{Salary: salary}
fSalary, _, errMsg := fetchFieldFloat(f, &ruleWrapper{baseRule{fieldExpr: "Salary", name: "ruleWrapper"}})
assert.Equal(t, fSalary, salary, errMsg)
}
func TestFetchFieldTime(t *testing.T) {
birthDate, _ := time.Parse("2006-01-02", "2020-03-01")
f := field{BirthDate: birthDate}
fBirthDate, _, errMsg := fetchFieldTime(f, &ruleWrapper{baseRule{fieldExpr: "BirthDate", name: "ruleWrapper"}})
assert.Equal(t, fBirthDate, birthDate, errMsg)
}