encode_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Flow flag
  51. {"a: [1, 2]\n", &struct{A []int "a/f"}{[]int{1, 2}}},
  52. {"a: {b: c}\n",
  53. &struct{A map[string]string "a/f"}{map[string]string{"b": "c"}}},
  54. {"a: {b: c}\n",
  55. &struct{A struct{B string} "a/f"}{struct{B string}{"c"}}},
  56. }
  57. func (s *S) TestMarshal(c *C) {
  58. for _, item := range marshalTests {
  59. data, err := goyaml.Marshal(item.value)
  60. c.Assert(err, IsNil)
  61. c.Assert(string(data), Equals, item.data)
  62. }
  63. }
  64. //var unmarshalErrorTests = []struct{data, error string}{
  65. // {"v: !!float 'error'", "Can't decode !!str 'error' as a !!float"},
  66. //}
  67. //
  68. //func (s *S) TestUnmarshalErrors(c *C) {
  69. // for _, item := range unmarshalErrorTests {
  70. // var value interface{}
  71. // err := goyaml.Unmarshal([]byte(item.data), &value)
  72. // c.Assert(err, Matches, item.error)
  73. // }
  74. //}
  75. var marshalTaggedIfaceTest interface{} = &struct{A string}{"B"}
  76. var getterTests = []struct{data, tag string; value interface{}}{
  77. {"_:\n hi: there\n", "", map[interface{}]interface{}{"hi": "there"}},
  78. {"_:\n- 1\n- A\n", "", []interface{}{1, "A"}},
  79. {"_: 10\n", "", 10},
  80. {"_: null\n", "", nil},
  81. {"_: !foo BAR!\n", "!foo", "BAR!"},
  82. {"_: !foo 1\n", "!foo", "1"},
  83. {"_: !foo '\"1\"'\n", "!foo", "\"1\""},
  84. {"_: !foo 1.1\n", "!foo", 1.1},
  85. {"_: !foo 1\n", "!foo", 1},
  86. {"_: !foo 1\n", "!foo", uint(1)},
  87. {"_: !foo true\n", "!foo", true},
  88. {"_: !foo\n- A\n- B\n", "!foo", []string{"A", "B"}},
  89. {"_: !foo\n A: B\n", "!foo", map[string]string{"A": "B"}},
  90. {"_: !foo\n a: B\n", "!foo", &marshalTaggedIfaceTest},
  91. }
  92. type typeWithGetter struct {
  93. tag string
  94. value interface{}
  95. }
  96. func (o typeWithGetter) GetYAML() (tag string, value interface{}) {
  97. return o.tag, o.value
  98. }
  99. type typeWithGetterField struct {
  100. Field typeWithGetter "_"
  101. }
  102. func (s *S) TestMashalWithGetter(c *C) {
  103. for _, item := range getterTests {
  104. obj := &typeWithGetterField{}
  105. obj.Field.tag = item.tag
  106. obj.Field.value = item.value
  107. data, err := goyaml.Marshal(obj)
  108. c.Assert(err, IsNil)
  109. c.Assert(string(data), Equals, string(item.data))
  110. }
  111. }
  112. func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
  113. obj := &typeWithGetter{}
  114. obj.tag = ""
  115. obj.value = map[string]string{"hello": "world!"}
  116. data, err := goyaml.Marshal(obj)
  117. c.Assert(err, IsNil)
  118. c.Assert(string(data), Equals, "hello: world!\n")
  119. }