16 var marshalIntTest = 123
18 var marshalTests = []struct {
29 map[string]string{"v": "hi"},
32 map[string]interface{}{"v": "hi"},
35 map[string]string{"v": "true"},
38 map[string]string{"v": "false"},
41 map[string]interface{}{"v": true},
44 map[string]interface{}{"v": false},
47 map[string]interface{}{"v": 10},
50 map[string]interface{}{"v": -10},
53 map[string]uint{"v": 42},
56 map[string]interface{}{"v": int64(4294967296)},
59 map[string]int64{"v": int64(4294967296)},
62 map[string]uint64{"v": 4294967296},
65 map[string]interface{}{"v": "10"},
68 map[string]interface{}{"v": 0.1},
71 map[string]interface{}{"v": float64(0.1)},
74 map[string]interface{}{"v": -0.1},
77 map[string]interface{}{"v": math.Inf(+1)},
80 map[string]interface{}{"v": math.Inf(-1)},
83 map[string]interface{}{"v": math.NaN()},
86 map[string]interface{}{"v": nil},
89 map[string]interface{}{"v": ""},
92 map[string][]string{"v": []string{"A", "B"}},
95 map[string][]string{"v": []string{"A", "B\nC"}},
96 "v:\n- A\n- |-\n B\n C\n",
98 map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}},
99 "v:\n- A\n- 1\n- B:\n - 2\n - 3\n",
101 map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}},
104 map[string]interface{}{"a": "-"},
116 &struct{ Hello string }{"world"},
123 }{struct{ B string }{"c"}},
130 }{&struct{ B string }{"c"}},
143 &struct{ A []int }{[]int{1, 2}},
151 &struct{ A bool }{true},
170 A *struct{ X, y int } "a,omitempty,flow"
171 }{&struct{ X, y int }{1, 2}},
175 A *struct{ X, y int } "a,omitempty,flow"
180 A *struct{ X, y int } "a,omitempty,flow"
181 }{&struct{ X, y int }{}},
185 A struct{ X, y int } "a,omitempty,flow"
186 }{struct{ X, y int }{1, 2}},
190 A struct{ X, y int } "a,omitempty,flow"
191 }{struct{ X, y int }{0, 1}},
195 A float64 "a,omitempty"
196 B float64 "b,omitempty"
209 A map[string]string "a,flow"
210 }{map[string]string{"b": "c", "d": "e"}},
217 }{struct{ B, D string }{"c", "e"}},
243 C inlineB `yaml:",inline"`
244 }{1, inlineB{2, inlineC{3}}},
245 "a: 1\nb: 2\nc: 3\n",
252 C map[string]int `yaml:",inline"`
253 }{1, map[string]int{"b": 2, "c": 3}},
254 "a: 1\nb: 2\nc: 3\n",
259 map[string]time.Duration{"a": 3 * time.Second},
263 // Issue #24: bug in map merging logic.
265 map[string]string{"a": "<foo>"},
269 // Issue #34: marshal unsupported base 60 floats quoted for compatibility
270 // with old YAML 1.1 parsers.
272 map[string]string{"a": "1:1"},
278 map[string]string{"a": "\x00"},
281 map[string]string{"a": "\x80\x81\x82"},
282 "a: !!binary gIGC\n",
284 map[string]string{"a": strings.Repeat("\x90", 54)},
285 "a: !!binary |\n " + strings.Repeat("kJCQ", 17) + "kJ\n CQ\n",
290 &yaml.MapSlice{{"b", 2}, {"a", 1}, {"d", 4}, {"c", 3}, {"sub", yaml.MapSlice{{"e", 5}}}},
291 "b: 2\na: 1\nd: 4\nc: 3\nsub:\n e: 5\n",
294 // Encode unicode as utf-8 rather than in escaped form.
296 map[string]string{"a": "你好"},
300 // Support encoding.TextMarshaler.
302 map[string]net.IP{"a": net.IPv4(1, 2, 3, 4)},
306 map[string]time.Time{"a": time.Unix(1424801979, 0)},
307 "a: 2015-02-24T18:19:39Z\n",
310 // Ensure strings containing ": " are quoted (reported as PR #43, but not reproducible).
312 map[string]string{"a": "b: c"},
316 // Containing hash mark ('#') in string should be quoted
318 map[string]string{"a": "Hello #comment"},
319 "a: 'Hello #comment'\n",
322 map[string]string{"a": "你好 #comment"},
323 "a: '你好 #comment'\n",
327 func (s *S) TestMarshal(c *C) {
328 defer os.Setenv("TZ", os.Getenv("TZ"))
329 os.Setenv("TZ", "UTC")
330 for _, item := range marshalTests {
331 data, err := yaml.Marshal(item.value)
333 c.Assert(string(data), Equals, item.data)
337 var marshalErrorTests = []struct {
345 }{1, inlineB{2, inlineC{3}}},
346 panic: `Duplicated key 'b' in struct struct \{ B int; .*`,
350 B map[string]int ",inline"
351 }{1, map[string]int{"a": 2}},
352 panic: `Can't have key "a" in inlined map; conflicts with struct field`,
355 func (s *S) TestMarshalErrors(c *C) {
356 for _, item := range marshalErrorTests {
357 if item.panic != "" {
358 c.Assert(func() { yaml.Marshal(item.value) }, PanicMatches, item.panic)
360 _, err := yaml.Marshal(item.value)
361 c.Assert(err, ErrorMatches, item.error)
366 func (s *S) TestMarshalTypeCache(c *C) {
370 type T struct{ A int }
371 data, err = yaml.Marshal(&T{})
375 type T struct{ B int }
376 data, err = yaml.Marshal(&T{})
379 c.Assert(string(data), Equals, "b: 0\n")
382 var marshalerTests = []struct {
386 {"_:\n hi: there\n", map[interface{}]interface{}{"hi": "there"}},
387 {"_:\n- 1\n- A\n", []interface{}{1, "A"}},
390 {"_: BAR!\n", "BAR!"},
393 type marshalerType struct {
397 func (o marshalerType) MarshalText() ([]byte, error) {
398 panic("MarshalText called on type with MarshalYAML")
401 func (o marshalerType) MarshalYAML() (interface{}, error) {
405 type marshalerValue struct {
406 Field marshalerType "_"
409 func (s *S) TestMarshaler(c *C) {
410 for _, item := range marshalerTests {
411 obj := &marshalerValue{}
412 obj.Field.value = item.value
413 data, err := yaml.Marshal(obj)
415 c.Assert(string(data), Equals, string(item.data))
419 func (s *S) TestMarshalerWholeDocument(c *C) {
420 obj := &marshalerType{}
421 obj.value = map[string]string{"hello": "world!"}
422 data, err := yaml.Marshal(obj)
424 c.Assert(string(data), Equals, "hello: world!\n")
427 type failingMarshaler struct{}
429 func (ft *failingMarshaler) MarshalYAML() (interface{}, error) {
430 return nil, failingErr
433 func (s *S) TestMarshalerError(c *C) {
434 _, err := yaml.Marshal(&failingMarshaler{})
435 c.Assert(err, Equals, failingErr)
438 func (s *S) TestSortedOutput(c *C) {
439 order := []interface{}{
477 m := make(map[interface{}]int)
478 for _, k := range order {
481 data, err := yaml.Marshal(m)
483 out := "\n" + string(data)
485 for i, k := range order {
486 repr := fmt.Sprint(k)
487 if s, ok := k.(string); ok {
488 if _, err = strconv.ParseFloat(repr, 32); s == "" || err == nil {
489 repr = `"` + repr + `"`
492 index := strings.Index(out, "\n"+repr+":")
494 c.Fatalf("%#v is not in the output: %#v", k, out)
497 c.Fatalf("%#v was generated before %#v: %q", k, order[i-1], out)