1 // Copyright 2017 Frank Schroeder. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func TestDecodeValues(t *testing.T) {
50 TM=2015-01-02T12:34:56Z
69 TM: tm(t, time.RFC3339, "2015-01-02T12:34:56Z"),
71 testDecode(t, in, &S{}, out)
74 func TestDecodeValueDefaults(t *testing.T) {
76 S string `properties:",default=abc"`
77 BT bool `properties:",default=true"`
78 BF bool `properties:",default=false"`
79 I int `properties:",default=-1"`
80 I8 int8 `properties:",default=-8"`
81 I16 int16 `properties:",default=-16"`
82 I32 int32 `properties:",default=-32"`
83 I64 int64 `properties:",default=-64"`
84 U uint `properties:",default=1"`
85 U8 uint8 `properties:",default=8"`
86 U16 uint16 `properties:",default=16"`
87 U32 uint32 `properties:",default=32"`
88 U64 uint64 `properties:",default=64"`
89 F32 float32 `properties:",default=3.2"`
90 F64 float64 `properties:",default=6.4"`
91 D time.Duration `properties:",default=5s"`
92 TM time.Time `properties:",default=2015-01-02T12:34:56Z"`
111 TM: tm(t, time.RFC3339, "2015-01-02T12:34:56Z"),
113 testDecode(t, "", &S{}, out)
116 func TestDecodeArrays(t *testing.T) {
151 TM=2015-01-01T00:00:00Z;2016-01-01T00:00:00Z
154 S: []string{"a", "b"},
155 B: []bool{true, false},
158 I16: []int16{-16, -17},
159 I32: []int32{-32, -33},
160 I64: []int64{-64, -65},
163 U16: []uint16{16, 17},
164 U32: []uint32{32, 33},
165 U64: []uint64{64, 65},
166 F32: []float32{3.2, 3.3},
167 F64: []float64{6.4, 6.5},
168 D: []time.Duration{4 * time.Second, 5 * time.Second},
169 TM: []time.Time{tm(t, time.RFC3339, "2015-01-01T00:00:00Z"), tm(t, time.RFC3339, "2016-01-01T00:00:00Z")},
171 testDecode(t, in, &S{}, out)
174 func TestDecodeArrayDefaults(t *testing.T) {
176 S []string `properties:",default=a;b"`
177 B []bool `properties:",default=true;false"`
178 I []int `properties:",default=-1;-2"`
179 I8 []int8 `properties:",default=-8;-9"`
180 I16 []int16 `properties:",default=-16;-17"`
181 I32 []int32 `properties:",default=-32;-33"`
182 I64 []int64 `properties:",default=-64;-65"`
183 U []uint `properties:",default=1;2"`
184 U8 []uint8 `properties:",default=8;9"`
185 U16 []uint16 `properties:",default=16;17"`
186 U32 []uint32 `properties:",default=32;33"`
187 U64 []uint64 `properties:",default=64;65"`
188 F32 []float32 `properties:",default=3.2;3.3"`
189 F64 []float64 `properties:",default=6.4;6.5"`
190 D []time.Duration `properties:",default=4s;5s"`
191 TM []time.Time `properties:",default=2015-01-01T00:00:00Z;2016-01-01T00:00:00Z"`
194 S: []string{"a", "b"},
195 B: []bool{true, false},
198 I16: []int16{-16, -17},
199 I32: []int32{-32, -33},
200 I64: []int64{-64, -65},
203 U16: []uint16{16, 17},
204 U32: []uint32{32, 33},
205 U64: []uint64{64, 65},
206 F32: []float32{3.2, 3.3},
207 F64: []float64{6.4, 6.5},
208 D: []time.Duration{4 * time.Second, 5 * time.Second},
209 TM: []time.Time{tm(t, time.RFC3339, "2015-01-01T00:00:00Z"), tm(t, time.RFC3339, "2016-01-01T00:00:00Z")},
211 testDecode(t, "", &S{}, out)
214 func TestDecodeSkipUndef(t *testing.T) {
216 X string `properties:"-"`
217 Undef string `properties:",default=some value"`
220 out := &S{"", "some value"}
221 testDecode(t, in, &S{}, out)
224 func TestDecodeStruct(t *testing.T) {
227 T string `properties:"t"`
228 U string `properties:"u,default=uuu"`
241 A{S: "sss", T: "ttt", U: "uuu"},
242 A{S: "SSS", T: "TTT", U: "uuu"},
244 testDecode(t, in, &S{}, out)
247 func TestDecodeMap(t *testing.T) {
249 A string `properties:"a"`
253 B map[string][]string
254 C map[string]map[string]string
257 F map[string]int `properties:"-"`
271 A: map[string]string{"foo": "bar", "bar": "bang"},
272 B: map[string][]string{"foo": []string{"a", "b", "c"}, "bar": []string{"1", "2", "3"}},
273 C: map[string]map[string]string{"foo": map[string]string{"one": "1", "two": "2"}, "bar": map[string]string{"three": "3", "four": "4"}},
274 D: map[string]S{"foo": S{"bar"}},
277 testDecode(t, in, &X{}, out)
280 func testDecode(t *testing.T, in string, v, out interface{}) {
283 t.Fatalf("got %v want nil", err)
285 if err := p.Decode(v); err != nil {
286 t.Fatalf("got %v want nil", err)
288 if got, want := v, out; !reflect.DeepEqual(got, want) {
289 t.Fatalf("\ngot %+v\nwant %+v", got, want)
293 func tm(t *testing.T, layout, s string) time.Time {
294 tm, err := time.Parse(layout, s)
296 t.Fatalf("got %v want nil", err)