encode_test.go 4.6 KB

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