14
var marshalIntTest = 123
16
var marshalTests = []struct {
27
map[string]string{"v": "hi"},
30
map[string]interface{}{"v": "hi"},
33
map[string]string{"v": "true"},
36
map[string]string{"v": "false"},
39
map[string]interface{}{"v": true},
42
map[string]interface{}{"v": false},
45
map[string]interface{}{"v": 10},
48
map[string]interface{}{"v": -10},
51
map[string]uint{"v": 42},
54
map[string]interface{}{"v": int64(4294967296)},
57
map[string]int64{"v": int64(4294967296)},
60
map[string]uint64{"v": 4294967296},
63
map[string]interface{}{"v": "10"},
66
map[string]interface{}{"v": 0.1},
69
map[string]interface{}{"v": float64(0.1)},
72
map[string]interface{}{"v": -0.1},
75
map[string]interface{}{"v": math.Inf(+1)},
78
map[string]interface{}{"v": math.Inf(-1)},
81
map[string]interface{}{"v": math.NaN()},
84
map[string]interface{}{"v": nil},
87
map[string]interface{}{"v": ""},
90
map[string][]string{"v": []string{"A", "B"}},
93
map[string][]string{"v": []string{"A", "B\nC"}},
94
"v:\n- A\n- |-\n B\n C\n",
96
map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}},
97
"v:\n- A\n- 1\n- B:\n - 2\n - 3\n",
99
map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
102
map[string]interface{}{"a": "-"},
114
&struct{ Hello string }{"world"},
121
}{struct{ B string }{"c"}},
128
}{&struct{ B string }{"c"}},
141
&struct{ A []int }{[]int{1, 2}},
149
&struct{ A bool }{true},
168
A *struct{ X int } "a,omitempty"
182
A map[string]string "a,flow"
183
}{map[string]string{"b": "c", "d": "e"}},
190
}{struct{ B, D string }{"c", "e"}},
216
C inlineB `yaml:",inline"`
217
}{1, inlineB{2, inlineC{3}}},
218
"a: 1\nb: 2\nc: 3\n",
223
map[string]time.Duration{"a": 3 * time.Second},
227
// Issue #24: bug in map merging logic.
229
map[string]string{"a": "<foo>"},
233
// Issue #34: marshal unsupported base 60 floats quoted for compatibility
234
// with old YAML 1.1 parsers.
236
map[string]string{"a": "1:1"},
242
map[string]string{"a": "\x00"},
245
map[string]string{"a": "\x80\x81\x82"},
246
"a: !!binary gIGC\n",
248
map[string]string{"a": strings.Repeat("\x90", 54)},
249
"a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
251
map[string]interface{}{"a": typeWithGetter{"!!str", "\x80\x81\x82"}},
252
"a: !!binary gIGC\n",
257
map[string]interface{}{"a": typeWithGetter{"foo!bar", 1}},
258
"a: !<foo%21bar> 1\n",
262
func (s *S) TestMarshal(c *C) {
263
for _, item := range marshalTests {
264
data, err := yaml.Marshal(item.value)
266
c.Assert(string(data), Equals, item.data)
270
var marshalErrorTests = []struct {
278
}{1, inlineB{2, inlineC{3}}},
279
panic: `Duplicated key 'b' in struct struct \{ B int; .*`,
281
value: typeWithGetter{"!!binary", "\x80"},
282
error: "YAML error: explicitly tagged !!binary data must be base64-encoded",
284
value: typeWithGetter{"!!float", "\x80"},
285
error: `YAML error: cannot marshal invalid UTF-8 data as !!float`,
288
func (s *S) TestMarshalErrors(c *C) {
289
for _, item := range marshalErrorTests {
290
if item.panic != "" {
291
c.Assert(func() { yaml.Marshal(item.value) }, PanicMatches, item.panic)
293
_, err := yaml.Marshal(item.value)
294
c.Assert(err, ErrorMatches, item.error)
299
var marshalTaggedIfaceTest interface{} = &struct{ A string }{"B"}
301
var getterTests = []struct {
305
{"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
306
{"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
308
{"_: null\n", "", nil},
309
{"_: !foo BAR!\n", "!foo", "BAR!"},
310
{"_: !foo 1\n", "!foo", "1"},
311
{"_: !foo '\"1\"'\n", "!foo", "\"1\""},
312
{"_: !foo 1.1\n", "!foo", 1.1},
313
{"_: !foo 1\n", "!foo", 1},
314
{"_: !foo 1\n", "!foo", uint(1)},
315
{"_: !foo true\n", "!foo", true},
316
{"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
317
{"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
318
{"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
321
func (s *S) TestMarshalTypeCache(c *C) {
325
type T struct{ A int }
326
data, err = yaml.Marshal(&T{})
330
type T struct{ B int }
331
data, err = yaml.Marshal(&T{})
334
c.Assert(string(data), Equals, "b: 0\n")
337
type typeWithGetter struct {
342
func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
343
return o.tag, o.value
346
type typeWithGetterField struct {
347
Field typeWithGetter "_"
350
func (s *S) TestMashalWithGetter(c *C) {
351
for _, item := range getterTests {
352
obj := &typeWithGetterField{}
353
obj.Field.tag = item.tag
354
obj.Field.value = item.value
355
data, err := yaml.Marshal(obj)
357
c.Assert(string(data), Equals, string(item.data))
361
func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
362
obj := &typeWithGetter{}
364
obj.value = map[string]string{"hello": "world!"}
365
data, err := yaml.Marshal(obj)
367
c.Assert(string(data), Equals, "hello: world!\n")
370
func (s *S) TestSortedOutput(c *C) {
371
order := []interface{}{
409
m := make(map[interface{}]int)
410
for _, k := range order {
413
data, err := yaml.Marshal(m)
415
out := "\n" + string(data)
417
for i, k := range order {
418
repr := fmt.Sprint(k)
419
if s, ok := k.(string); ok {
420
if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil {
421
repr = `"` + repr + `"`
424
index := strings.Index(out, "\n"+repr+":")
426
c.Fatalf("%#v is not in the output: %#v", k, out)
429
c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)