-
Notifications
You must be signed in to change notification settings - Fork 2
/
object_test.go
172 lines (144 loc) · 2.89 KB
/
object_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package moon
import (
"fmt"
"testing"
)
func TestDoc(t *testing.T) {
doc, err := ReadString(`
name: "jordan"
`)
if err != nil {
t.Error(err)
return
}
var name string
if err := doc.Get("name", &name); err != nil {
t.Error(err)
return
}
if name != "jordan" {
t.Errorf("unexpected name value, expected 'jordan', saw '%s'", name)
}
}
func TestFill(t *testing.T) {
doc, err := ReadString(`
# this is just a comment
firstname: jordan
port: 9000
`)
if err != nil {
t.Error(err)
return
}
var dest struct {
FirstName string `name: firstname; required: true`
HostName string `name: hostname; default: localhost`
Port int `name: port; default: 9001`
}
if err := doc.Fill(&dest); err != nil {
t.Error(err)
return
}
if dest.FirstName != "jordan" {
t.Errorf("unexpected firstname value, expected 'jordan', saw '%s'", dest.FirstName)
}
if dest.HostName != "localhost" {
t.Errorf("unexpected hostname value, expected 'localhost', saw '%s'", dest.HostName)
}
if dest.Port != 9000 {
t.Errorf("unexpected port value, expected 9000, saw %d", dest.Port)
}
}
func ExampleDoc_Fill() {
input := `
name: jordan
age: 29
`
var config struct {
Name string `name: "name"`
Age int `name: "age"`
City string `name: "city" default: "Brooklyn"`
}
doc, err := ReadString(input)
if err != nil {
fmt.Printf("error reading input: %s", err)
return
}
if err := doc.Fill(&config); err != nil {
fmt.Printf("error filling config value: %s", err)
return
}
fmt.Println(config)
// Output: {jordan 29 Brooklyn}
}
func ExampleDoc_Get_one() {
input := `
name: jordan
age: 29
`
doc, err := ReadString(input)
if err != nil {
fmt.Printf("error reading input: %s", err)
return
}
var name string
if err := doc.Get("name", &name); err != nil {
fmt.Printf("error filling config value: %s", err)
return
}
fmt.Println(name)
// Output: jordan
}
func ExampleDoc_Get_two() {
input := `
@todd: {
name: todd
age: 38
}
@sean: {
name: sean
age: 34
}
@jordan: {
name: jordan
age: 29
}
brothers: [@todd @sean @jordan]
`
doc, err := ReadString(input)
if err != nil {
fmt.Printf("error reading input: %s", err)
return
}
var name string
if err := doc.Get("brothers/1/name", &name); err != nil {
fmt.Printf("error filling config value: %s", err)
return
}
fmt.Println(name)
// Output: sean
}
func TestFillEmbeds(t *testing.T) {
in := `top: {val: some_data}`
var dest struct {
Top *struct {
Val string `name: val`
} `name: top`
}
doc, err := ReadString(in)
if err != nil {
t.Error(err)
return
}
if err := doc.Fill(&dest); err != nil {
t.Error(err)
return
}
if dest.Top == nil {
t.Error("didn't actually set a value")
return
}
if dest.Top.Val != "some_data" {
t.Errorf("expected some_data, got %v", dest.Top.Val)
}
}