-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththe_test.go
144 lines (122 loc) · 2.74 KB
/
the_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
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
package u
import (
"testing"
"time"
"github.com/tredeske/u/golum"
"github.com/tredeske/u/uconfig"
"github.com/tredeske/u/ulog"
"github.com/tredeske/u/uregistry"
)
type thing_ struct {
name string
a string
b string
i int64
j int64
mystuff []stuff_
}
type stuff_ struct {
foo string
bar bool
}
func (this *thing_) Help(name string, help *uconfig.Help) {
p := help.Init(name, "This thing is a test")
p.NewItem("a", "string", "test element a")
p.NewItem("b", "string", "test element b")
p.NewItem("i", "int", "test element i")
p.NewItem("j", "int", "test element j")
}
func (this *thing_) Reload(
name string,
config *uconfig.Chain,
) (
rv golum.Reloadable,
err error,
) {
g := &thing_{
name: name,
i: -1,
j: 50,
}
rv = g
err = config.
GetString("a", &g.a, uconfig.StringNotBlank()).
GetString("b", &g.b).
GetInt("i", &g.i).
GetInt("g.j", &g.j).
//
EachSection("stuff",
func(s *uconfig.Section) (err error) {
aStuff := stuff_{}
return s.Chain().
GetString("foo", &aStuff.foo).
GetBool("bar", &aStuff.bar).
Then(func() { g.mystuff = append(g.mystuff, aStuff) }).
Error
}).
//
Then(func() { uregistry.MustPutSingleton(name, g) }).
Error
return
}
func (this *thing_) Start() (err error) { return }
func (this *thing_) Stop() {}
// put together a YAML config
var config_ = `
debug:
enable: ["all"]
components:
- name: thingOne
type: testFactory
config:
a: test
b: "{{ .subValue }}"
i: 100
stuff:
- foo: foo1
bar: true
- foo: 800
- name: thingTwo
type: testFactory
config:
a: test2
i: 101
j: 3
stuff:
- foo: foo2
bar: false
- foo: false
properties:
subValue: a value to sub in
`
// A way to use golum in a test
func TestTesting(t *testing.T) {
//
// install the factory(ies)
//
golum.AddReloadable("testFactory", &thing_{})
//
// start up your stuff from YAML config
//
err := golum.TestLoadAndStart(config_)
defer golum.TestStop()
if err != nil {
t.Fatalf("Unable to config: %s", err)
}
time.Sleep(time.Second)
if !ulog.DebugEnabled {
t.Fatalf("debug not enabled for all")
}
//
// do what you need to do
//
var thingOne, thingTwo *thing_
err = uregistry.GetValid("thingOne", &thingOne)
if err != nil {
t.Fatalf("Unable to get thingOne: %s", err)
}
err = uregistry.GetValid("thingTwo", &thingTwo)
if err != nil {
t.Fatalf("Unable to get thingTwo: %s", err)
}
}