-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoconf_test.go
159 lines (151 loc) · 2.97 KB
/
goconf_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
package goconf
import (
"encoding/json"
"fmt"
"testing"
)
type bar struct{ Code string }
func (b *bar) SetString(input string) error { b.Code = input; return nil }
func (b bar) String() string { return b.Code }
func Test(t *testing.T) {
if ToConfigs(nil) != nil {
t.Error("ToConfigs(nil) should return nil")
}
if _, err := Marshal(nil); err != ErrNotStruct {
t.Error("Marshal(nil) should return ErrNotStruct")
}
if _, err := Marshal(1); err != ErrNotStruct {
t.Error("Marshal(1) should return ErrNotStruct")
}
if _, err := Marshal("a"); err != ErrNotStruct {
t.Error("Marshal(a) should return ErrNotStruct")
}
a := &struct {
Name string
Bar *bar
Old string
}{"foo", &bar{"bar"}, "old"}
c, err := Marshal(a)
if err != nil {
panic(err)
}
var b struct {
Bar *bar
Name string
New string
}
err = Unmarshal(c, &b)
if err != nil {
panic(err)
}
if a.Name != b.Name {
t.Error("name should be equal")
}
if a.Bar.Code != b.Bar.Code {
t.Error("code should be equal")
}
}
func ExampleMarshal() {
a := struct {
Foo string `
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.`
Hello string
Bool bool
Number int
Uint uint32
Float float64
}{
"Bar", "World", true, 123, 22, 1.23,
}
c, err := Marshal(a)
if err != nil {
panic(err)
}
fmt.Println(string(c))
b, err := json.MarshalIndent(ToConfigs(a), "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(b))
// Output:
// package config
//
// const (
// // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
// // incididunt ut labore et dolore magna aliqua.
// Foo = "Bar"
//
// Hello = "World"
//
// Bool = true
//
// Number = 123
//
// Uint = 22
//
// Float = 1.23
// )
//
// [
// {
// "Key": "Foo",
// "Value": "Bar",
// "Comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\nincididunt ut labore et dolore magna aliqua."
// },
// {
// "Key": "Hello",
// "Value": "World",
// "Comment": ""
// },
// {
// "Key": "Bool",
// "Value": "true",
// "Comment": ""
// },
// {
// "Key": "Number",
// "Value": "123",
// "Comment": ""
// },
// {
// "Key": "Uint",
// "Value": "22",
// "Comment": ""
// },
// {
// "Key": "Float",
// "Value": "1.23",
// "Comment": ""
// }
// ]
}
func ExampleUnmarshal() {
var c struct {
Foo string
Hello string
Bool bool
Number int
Uint uint32
Float float64
}
fmt.Printf("before: %+v\n", c)
err := Unmarshal([]byte(`
package config
const Foo = "BAR"
const (
Hello = "WORLD"
Bool = true
Number = 123
Uint = 22
Float = 1.23
)
`), &c)
if err != nil {
panic(err)
}
fmt.Printf("after: %+v\n", c)
// Output:
// before: {Foo: Hello: Bool:false Number:0 Uint:0 Float:0}
// after: {Foo:BAR Hello:WORLD Bool:true Number:123 Uint:22 Float:1.23}
}