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