json_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package json_test
  2. import (
  3. gojson "encoding/json"
  4. "testing"
  5. "github.com/d5/tengo/v2"
  6. "github.com/d5/tengo/v2/require"
  7. "github.com/d5/tengo/v2/stdlib/json"
  8. )
  9. type ARR = []interface{}
  10. type MAP = map[string]interface{}
  11. func TestJSON(t *testing.T) {
  12. testJSONEncodeDecode(t, nil)
  13. testJSONEncodeDecode(t, 0)
  14. testJSONEncodeDecode(t, 1)
  15. testJSONEncodeDecode(t, -1)
  16. testJSONEncodeDecode(t, 1984)
  17. testJSONEncodeDecode(t, -1984)
  18. testJSONEncodeDecode(t, 0.0)
  19. testJSONEncodeDecode(t, 1.0)
  20. testJSONEncodeDecode(t, -1.0)
  21. testJSONEncodeDecode(t, 19.84)
  22. testJSONEncodeDecode(t, -19.84)
  23. testJSONEncodeDecode(t, "")
  24. testJSONEncodeDecode(t, "foo")
  25. testJSONEncodeDecode(t, "foo bar")
  26. testJSONEncodeDecode(t, "foo \"bar\"")
  27. // See: https://github.com/d5/tengo/issues/268
  28. testJSONEncodeDecode(t, "1\u001C04")
  29. testJSONEncodeDecode(t, "çığöşü")
  30. testJSONEncodeDecode(t, "ç1\u001C04IĞÖŞÜ")
  31. testJSONEncodeDecode(t, "错误测试")
  32. testJSONEncodeDecode(t, true)
  33. testJSONEncodeDecode(t, false)
  34. testJSONEncodeDecode(t, ARR{})
  35. testJSONEncodeDecode(t, ARR{0})
  36. testJSONEncodeDecode(t, ARR{false})
  37. testJSONEncodeDecode(t, ARR{1, 2, 3,
  38. "four", false})
  39. testJSONEncodeDecode(t, ARR{1, 2, 3,
  40. "four", false, MAP{"a": 0, "b": "bee", "bool": true}})
  41. testJSONEncodeDecode(t, MAP{})
  42. testJSONEncodeDecode(t, MAP{"a": 0})
  43. testJSONEncodeDecode(t, MAP{"a": 0, "b": "bee"})
  44. testJSONEncodeDecode(t, MAP{"a": 0, "b": "bee", "bool": true})
  45. testJSONEncodeDecode(t, MAP{"a": 0, "b": "bee",
  46. "arr": ARR{1, 2, 3, "four"}})
  47. testJSONEncodeDecode(t, MAP{"a": 0, "b": "bee",
  48. "arr": ARR{1, 2, 3, MAP{"a": false, "b": 109.4}}})
  49. }
  50. func TestDecode(t *testing.T) {
  51. testDecodeError(t, `{`)
  52. testDecodeError(t, `}`)
  53. testDecodeError(t, `{}a`)
  54. testDecodeError(t, `{{}`)
  55. testDecodeError(t, `{}}`)
  56. testDecodeError(t, `[`)
  57. testDecodeError(t, `]`)
  58. testDecodeError(t, `[]a`)
  59. testDecodeError(t, `[[]`)
  60. testDecodeError(t, `[]]`)
  61. testDecodeError(t, `"`)
  62. testDecodeError(t, `"abc`)
  63. testDecodeError(t, `abc"`)
  64. testDecodeError(t, `.123`)
  65. testDecodeError(t, `123.`)
  66. testDecodeError(t, `1.2.3`)
  67. testDecodeError(t, `'a'`)
  68. testDecodeError(t, `true, false`)
  69. testDecodeError(t, `{"a:"b"}`)
  70. testDecodeError(t, `{a":"b"}`)
  71. testDecodeError(t, `{"a":"b":"c"}`)
  72. }
  73. func testDecodeError(t *testing.T, input string) {
  74. _, err := json.Decode([]byte(input))
  75. require.Error(t, err)
  76. }
  77. func testJSONEncodeDecode(t *testing.T, v interface{}) {
  78. o, err := tengo.FromInterface(v)
  79. require.NoError(t, err)
  80. b, err := json.Encode(o)
  81. require.NoError(t, err)
  82. a, err := json.Decode(b)
  83. require.NoError(t, err, string(b))
  84. vj, err := gojson.Marshal(v)
  85. require.NoError(t, err)
  86. aj, err := gojson.Marshal(tengo.ToInterface(a))
  87. require.NoError(t, err)
  88. require.Equal(t, vj, aj)
  89. }