encode_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package goyaml_test
  2. import (
  3. . "gocheck"
  4. "goyaml"
  5. "math"
  6. //"reflect"
  7. )
  8. var marshalIntTest = 123
  9. var marshalTests = []struct{data string; value interface{}}{
  10. {"{}\n", &struct{}{}},
  11. {"v: hi\n", map[string]string{"v": "hi"}},
  12. {"v: hi\n", map[string]interface{}{"v": "hi"}},
  13. {"v: \"true\"\n", map[string]string{"v": "true"}},
  14. {"v: \"false\"\n", map[string]string{"v": "false"}},
  15. {"v: true\n", map[string]interface{}{"v": true}},
  16. {"v: false\n", map[string]interface{}{"v": false}},
  17. {"v: 10\n", map[string]interface{}{"v": 10}},
  18. {"v: -10\n", map[string]interface{}{"v": -10}},
  19. {"v: 42\n", map[string]uint{"v": 42}},
  20. {"v: 4294967296\n", map[string]interface{}{"v": int64(4294967296)}},
  21. {"v: 4294967296\n", map[string]int64{"v": int64(4294967296)}},
  22. {"v: 4294967296\n", map[string]uint64{"v": 4294967296}},
  23. {"v: \"10\"\n", map[string]interface{}{"v": "10"}},
  24. {"v: 0.1\n", map[string]interface{}{"v": 0.1}},
  25. {"v: 0.1\n", map[string]interface{}{"v": float64(0.1)}},
  26. {"v: -0.1\n", map[string]interface{}{"v": -0.1}},
  27. {"v: .inf\n", map[string]interface{}{"v": math.Inf(+1)}},
  28. {"v: -.inf\n", map[string]interface{}{"v": math.Inf(-1)}},
  29. {"v: .nan\n", map[string]interface{}{"v": math.NaN()}},
  30. {"v: null\n", map[string]interface{}{"v": nil}},
  31. {"v: \"\"\n", map[string]interface{}{"v": ""}},
  32. {"v:\n- A\n- B\n", map[string][]string{"v": []string{"A", "B"}}},
  33. {"v:\n- A\n- 1\n", map[string][]interface{}{"v": []interface{}{"A", 1}}},
  34. {"a:\n b: c\n",
  35. map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}},
  36. // Simple values.
  37. {"123\n", &marshalIntTest},
  38. // Structures
  39. {"hello: world\n", &struct{Hello string}{"world"}},
  40. {"a:\n b: c\n", &struct{A struct{B string}}{struct{B string}{"c"}}},
  41. {"a:\n b: c\n", &struct{A *struct{B string}}{&struct{B string}{"c"}}},
  42. {"a: null\n", &struct{A *struct{B string}}{}},
  43. {"a: 1\n", &struct{A int}{1}},
  44. {"a:\n- 1\n- 2\n", &struct{A []int}{[]int{1, 2}}},
  45. {"a: 1\n", &struct{B int "a"}{1}},
  46. {"a: true\n", &struct{A bool}{true}},
  47. // Conditional flag
  48. {"a: 1\n", &struct{A int "a/c"; B int "b/c"}{1, 0}},
  49. {"{}\n", &struct{A int "a/c"; B int "b/c"}{0, 0}},
  50. }
  51. func (s *S) TestMarshal(c *C) {
  52. for _, item := range marshalTests {
  53. data, err := goyaml.Marshal(item.value)
  54. c.Assert(err, IsNil)
  55. c.Assert(string(data), Equals, item.data)
  56. }
  57. }
  58. //var unmarshalErrorTests = []struct{data, error string}{
  59. // {"v: !!float 'error'", "Can't decode !!str 'error' as a !!float"},
  60. //}
  61. //
  62. //func (s *S) TestUnmarshalErrors(c *C) {
  63. // for _, item := range unmarshalErrorTests {
  64. // var value interface{}
  65. // err := goyaml.Unmarshal([]byte(item.data), &value)
  66. // c.Assert(err, Matches, item.error)
  67. // }
  68. //}
  69. var marshalTaggedIfaceTest interface{} = &struct{A string}{"B"}
  70. var getterTests = []struct{data, tag string; value interface{}}{
  71. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  72. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  73. {"_: 10\n", "", 10},
  74. {"_: null\n", "", nil},
  75. {"_: !foo BAR!\n", "!foo", "BAR!"},
  76. {"_: !foo 1\n", "!foo", "1"},
  77. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  78. {"_: !foo 1.1\n", "!foo", 1.1},
  79. {"_: !foo 1\n", "!foo", 1},
  80. {"_: !foo 1\n", "!foo", uint(1)},
  81. {"_: !foo true\n", "!foo", true},
  82. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  83. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  84. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  85. }
  86. type typeWithGetter struct {
  87. tag string
  88. value interface{}
  89. }
  90. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  91. return o.tag, o.value
  92. }
  93. type typeWithGetterField struct {
  94. Field typeWithGetter "_"
  95. }
  96. func (s *S) TestMashalWithGetter(c *C) {
  97. for _, item := range getterTests {
  98. obj := &typeWithGetterField{}
  99. obj.Field.tag = item.tag
  100. obj.Field.value = item.value
  101. data, err := goyaml.Marshal(obj)
  102. c.Assert(err, IsNil)
  103. c.Assert(string(data), Equals, string(item.data))
  104. }
  105. }
  106. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  107. obj := &typeWithGetter{}
  108. obj.tag = ""
  109. obj.value = map[string]string{"hello": "world!"}
  110. data, err := goyaml.Marshal(obj)
  111. c.Assert(err, IsNil)
  112. c.Assert(string(data), Equals, "hello: world!\n")
  113. }