-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfield_test.go
85 lines (72 loc) · 1.52 KB
/
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
package underscore
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Benchmark_Field(b *testing.B) {
for n := 0; n < b.N; n++ {
Field("id")(testModel{ID: 1, Name: "one"})
}
}
func Benchmark_FieldValue(b *testing.B) {
for n := 0; n < b.N; n++ {
FieldValue("id")(testModel{ID: 1, Name: "one"})
}
}
func Benchmark_FieldValue_InvalidName(b *testing.B) {
for n := 0; n < b.N; n++ {
FieldValue("$$")(testModel{ID: 1, Name: "one"})
}
}
func Benchmark_FieldValue_Nested(b *testing.B) {
for n := 0; n < b.N; n++ {
FieldValue("id")(testNestedModel{
testModel: testModel{
ID: 11,
},
})
}
}
func Benchmark_FieldValue_Ptr(b *testing.B) {
for n := 0; n < b.N; n++ {
FieldValue("id")(&testModel{ID: 1, Name: "ptr"})
}
}
func Test_Field(t *testing.T) {
item := testModel{ID: 1, Name: "one"}
getName := Field("name")
name := getName(item)
assert.Equal(
t,
name,
item.Name,
)
}
func Test_Field_Nested(t *testing.T) {
item := testNestedModel{
testModel: testModel{
ID: 11,
},
}
id, ok := Field("id")(item).(int)
assert.True(t, ok)
assert.Equal(t, id, 11)
}
func Test_Field_Ptr(t *testing.T) {
item := &testModel{ID: 1, Name: "ptr"}
nameGetter := Field("name")
name := nameGetter(item)
assert.Equal(t, name, item.Name)
}
func Test_FieldValue(t *testing.T) {
item := testModel{ID: 1, Name: "one"}
value := FieldValue("$$")(item)
assert.Equal(t, value, nilValue)
getNameValue := FieldValue("name")
nameValue := getNameValue(item)
assert.Equal(
t,
nameValue.String(),
item.Name,
)
}