values_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package values
  2. import (
  3. . "github.com/smartystreets/goconvey/convey"
  4. "gopkg.in/yaml.v2"
  5. "os"
  6. "testing"
  7. )
  8. func TestValues(t *testing.T) {
  9. Convey("Values", t, func() {
  10. os.Setenv("INT", "1")
  11. os.Setenv("STRING", "test")
  12. os.Setenv("BOOL", "true")
  13. Convey("IntValue", func() {
  14. type Data struct {
  15. Val IntValue `yaml:"val"`
  16. }
  17. d := &Data{}
  18. Convey("Should unmarshal simple number", func() {
  19. unmarshalingTest(`val: 1`, d)
  20. So(d.Val.Value(), ShouldEqual, 1)
  21. So(d.Val.Raw, ShouldEqual, "1")
  22. })
  23. Convey("Should unmarshal env var", func() {
  24. unmarshalingTest(`val: $INT`, d)
  25. So(d.Val.Value(), ShouldEqual, 1)
  26. So(d.Val.Raw, ShouldEqual, "$INT")
  27. })
  28. Convey("Should ignore empty value", func() {
  29. unmarshalingTest(`val: `, d)
  30. So(d.Val.Value(), ShouldEqual, 0)
  31. So(d.Val.Raw, ShouldEqual, "")
  32. })
  33. })
  34. Convey("StringValue", func() {
  35. type Data struct {
  36. Val StringValue `yaml:"val"`
  37. }
  38. d := &Data{}
  39. Convey("Should unmarshal simple string", func() {
  40. unmarshalingTest(`val: test`, d)
  41. So(d.Val.Value(), ShouldEqual, "test")
  42. So(d.Val.Raw, ShouldEqual, "test")
  43. })
  44. Convey("Should unmarshal env var", func() {
  45. unmarshalingTest(`val: $STRING`, d)
  46. So(d.Val.Value(), ShouldEqual, "test")
  47. So(d.Val.Raw, ShouldEqual, "$STRING")
  48. })
  49. Convey("Should ignore empty value", func() {
  50. unmarshalingTest(`val: `, d)
  51. So(d.Val.Value(), ShouldEqual, "")
  52. So(d.Val.Raw, ShouldEqual, "")
  53. })
  54. })
  55. Convey("BoolValue", func() {
  56. type Data struct {
  57. Val BoolValue `yaml:"val"`
  58. }
  59. d := &Data{}
  60. Convey("Should unmarshal bool value", func() {
  61. unmarshalingTest(`val: true`, d)
  62. So(d.Val.Value(), ShouldBeTrue)
  63. So(d.Val.Raw, ShouldEqual, "true")
  64. })
  65. Convey("Should unmarshal explicit string", func() {
  66. unmarshalingTest(`val: "true"`, d)
  67. So(d.Val.Value(), ShouldBeTrue)
  68. So(d.Val.Raw, ShouldEqual, "true")
  69. })
  70. Convey("Should unmarshal env var", func() {
  71. unmarshalingTest(`val: $BOOL`, d)
  72. So(d.Val.Value(), ShouldBeTrue)
  73. So(d.Val.Raw, ShouldEqual, "$BOOL")
  74. })
  75. Convey("Should ignore empty value", func() {
  76. unmarshalingTest(`val: `, d)
  77. So(d.Val.Value(), ShouldBeFalse)
  78. So(d.Val.Raw, ShouldEqual, "")
  79. })
  80. })
  81. Convey("JSONValue", func() {
  82. type Data struct {
  83. Val JSONValue `yaml:"val"`
  84. }
  85. d := &Data{}
  86. Convey("Should unmarshal variable nesting", func() {
  87. doc := `
  88. val:
  89. one: 1
  90. two: $STRING
  91. three:
  92. - 1
  93. - two
  94. - three:
  95. inside: $STRING
  96. four:
  97. nested:
  98. onemore: $INT
  99. multiline: >
  100. Some text with $STRING
  101. anchor: &label $INT
  102. anchored: *label
  103. `
  104. unmarshalingTest(doc, d)
  105. type anyMap = map[interface{}]interface{}
  106. So(d.Val.Value(), ShouldResemble, map[string]interface{}{
  107. "one": 1,
  108. "two": "test",
  109. "three": []interface{}{
  110. 1, "two", anyMap{
  111. "three": anyMap{
  112. "inside": "test",
  113. },
  114. },
  115. },
  116. "four": anyMap{
  117. "nested": anyMap{
  118. "onemore": "1",
  119. },
  120. },
  121. "multiline": "Some text with test\n",
  122. "anchor": "1",
  123. "anchored": "1",
  124. })
  125. So(d.Val.Raw, ShouldResemble, map[string]interface{}{
  126. "one": 1,
  127. "two": "$STRING",
  128. "three": []interface{}{
  129. 1, "two", anyMap{
  130. "three": anyMap{
  131. "inside": "$STRING",
  132. },
  133. },
  134. },
  135. "four": anyMap{
  136. "nested": anyMap{
  137. "onemore": "$INT",
  138. },
  139. },
  140. "multiline": "Some text with $STRING\n",
  141. "anchor": "$INT",
  142. "anchored": "$INT",
  143. })
  144. })
  145. })
  146. Convey("StringMapValue", func() {
  147. type Data struct {
  148. Val StringMapValue `yaml:"val"`
  149. }
  150. d := &Data{}
  151. Convey("Should unmarshal mapping", func() {
  152. doc := `
  153. val:
  154. one: 1
  155. two: "test string"
  156. three: $STRING
  157. four: true
  158. `
  159. unmarshalingTest(doc, d)
  160. So(d.Val.Value(), ShouldResemble, map[string]string{
  161. "one": "1",
  162. "two": "test string",
  163. "three": "test",
  164. "four": "true",
  165. })
  166. So(d.Val.Raw, ShouldResemble, map[string]string{
  167. "one": "1",
  168. "two": "test string",
  169. "three": "$STRING",
  170. "four": "true",
  171. })
  172. })
  173. })
  174. Reset(func() {
  175. os.Unsetenv("INT")
  176. os.Unsetenv("STRING")
  177. os.Unsetenv("BOOL")
  178. })
  179. })
  180. }
  181. func unmarshalingTest(document string, out interface{}) {
  182. err := yaml.Unmarshal([]byte(document), out)
  183. So(err, ShouldBeNil)
  184. }