-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfiller_test.go
76 lines (68 loc) · 1.61 KB
/
filler_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 filler
import (
"errors"
"testing"
)
//go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
var demoFiller = filler{
tag: "demoFiller1",
fn: func(obj interface{}) (interface{}, error) {
return "hello", nil
},
}
type demoStruct struct {
Name string `fill:"demoFiller1:Val"`
Val string `fill:"demoFiller2"`
Ptr *string
XPtr *string `fill:"fillPtr:Ptr"`
Ignore1 string `fill:"-"`
Ignore2 string `fill:""`
}
type notSameTypeStruct struct {
Val int `fill:"demoFiller1"`
}
type errFromFn struct {
Val int `fill:"demoFillerErr"`
}
// RegFiller - register new filler into []fillers
func TestRegFiller(t *testing.T) {
RegFiller("demoFiller1", func(obj interface{}) (interface{}, error) {
return "hello", nil
})
v1, err1 := fillers[0].fn("hello")
v2, err2 := demoFiller.fn("hello")
if fillers[0].tag != demoFiller.tag || v1 != v2 || err1 != err2 {
t.FailNow()
}
}
// Fill - fill the object with all the current fillers
func TestFill(t *testing.T) {
RegFiller("demoFiller1", func(obj interface{}) (interface{}, error) {
return "hello", nil
})
RegFiller("demoFillerErr", func(obj interface{}) (interface{}, error) {
return nil, errors.New("some error")
})
m := demoStruct{
Name: "nameVal",
Val: "valVal",
}
// check non ptr - should return error
if err := Fill(m); err == nil {
t.FailNow()
}
// check if got filled
Fill(&m)
// should be filled
if m.Name != "hello" || m.Val != "valVal" {
t.FailNow()
}
m2 := notSameTypeStruct{}
if err := Fill(&m2); err == nil {
t.FailNow()
}
m3 := errFromFn{}
if err := Fill(&m3); err == nil {
t.FailNow()
}
}