encoding_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package encoding
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestTomlToJson(t *testing.T) {
  7. tests := []struct {
  8. input string
  9. expect string
  10. }{
  11. {
  12. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"",
  13. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  14. },
  15. {
  16. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"",
  17. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  18. },
  19. {
  20. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"",
  21. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  22. },
  23. {
  24. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"",
  25. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  26. },
  27. {
  28. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"",
  29. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  30. },
  31. {
  32. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"\n",
  33. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  34. },
  35. {
  36. input: "a = \"foo\"\nb = 1\nc = \"${FOO}\"\nd = \"abcd!@#$112\"\n",
  37. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  38. },
  39. }
  40. for _, test := range tests {
  41. test := test
  42. t.Run(test.input, func(t *testing.T) {
  43. t.Parallel()
  44. got, err := TomlToJson([]byte(test.input))
  45. assert.NoError(t, err)
  46. assert.Equal(t, test.expect, string(got))
  47. })
  48. }
  49. }
  50. func TestTomlToJsonError(t *testing.T) {
  51. _, err := TomlToJson([]byte("foo"))
  52. assert.Error(t, err)
  53. }
  54. func TestYamlToJson(t *testing.T) {
  55. tests := []struct {
  56. input string
  57. expect string
  58. }{
  59. {
  60. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112",
  61. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  62. },
  63. {
  64. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112",
  65. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  66. },
  67. {
  68. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112",
  69. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  70. },
  71. {
  72. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112",
  73. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  74. },
  75. {
  76. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112",
  77. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  78. },
  79. {
  80. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112\n",
  81. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  82. },
  83. {
  84. input: "a: foo\nb: 1\nc: ${FOO}\nd: abcd!@#$112\n",
  85. expect: "{\"a\":\"foo\",\"b\":1,\"c\":\"${FOO}\",\"d\":\"abcd!@#$112\"}\n",
  86. },
  87. }
  88. for _, test := range tests {
  89. test := test
  90. t.Run(test.input, func(t *testing.T) {
  91. t.Parallel()
  92. got, err := YamlToJson([]byte(test.input))
  93. assert.NoError(t, err)
  94. assert.Equal(t, test.expect, string(got))
  95. })
  96. }
  97. }
  98. func TestYamlToJsonError(t *testing.T) {
  99. _, err := YamlToJson([]byte("':foo"))
  100. assert.Error(t, err)
  101. }
  102. func TestYamlToJsonSlice(t *testing.T) {
  103. b, err := YamlToJson([]byte(`foo:
  104. - bar
  105. - baz`))
  106. assert.NoError(t, err)
  107. assert.Equal(t, `{"foo":["bar","baz"]}
  108. `, string(b))
  109. }