values_test.go 5.3 KB

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