summaryrefslogtreecommitdiff
path: root/compat/json/compat_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'compat/json/compat_test.go')
-rw-r--r--compat/json/compat_test.go84
1 files changed, 55 insertions, 29 deletions
diff --git a/compat/json/compat_test.go b/compat/json/compat_test.go
index 098ac85..3de48f7 100644
--- a/compat/json/compat_test.go
+++ b/compat/json/compat_test.go
@@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later
-package json
+package json_test
import (
"bytes"
@@ -11,6 +11,8 @@ import (
"testing"
"github.com/stretchr/testify/assert"
+
+ "git.lukeshu.com/go/lowmemjson/compat/json"
)
func TestCompatHTMLEscape(t *testing.T) {
@@ -31,7 +33,7 @@ func TestCompatHTMLEscape(t *testing.T) {
t.Parallel()
t.Logf("in=%q", tc.In)
var dst bytes.Buffer
- HTMLEscape(&dst, []byte(tc.In))
+ json.HTMLEscape(&dst, []byte(tc.In))
assert.Equal(t, tc.Out, dst.String())
})
}
@@ -58,7 +60,7 @@ func TestCompatValid(t *testing.T) {
t.Run(tcName, func(t *testing.T) {
t.Parallel()
t.Logf("in=%q", tc.In)
- act := Valid([]byte(tc.In))
+ act := json.Valid([]byte(tc.In))
assert.Equal(t, tc.Exp, act)
})
}
@@ -72,13 +74,15 @@ func TestCompatCompact(t *testing.T) {
Err string
}
testcases := map[string]testcase{
- "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`},
- "object": {In: `{}`, Out: `{}`},
- "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""},
- "float": {In: `1.200e003`, Out: `1.200e003`},
- "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`},
- "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`},
- "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`},
+ "empty": {In: ``, Out: ``, Err: `unexpected end of JSON input`},
+ "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`},
+ "object": {In: `{}`, Out: `{}`},
+ "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""},
+ "float": {In: `1.200e003`, Out: `1.200e003`},
+ "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`},
+ "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`},
+ "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`},
+ "invalid-utf8": {In: "\x85", Err: `invalid character '\x85' looking for beginning of value`},
}
for tcName, tc := range testcases {
tc := tc
@@ -86,7 +90,7 @@ func TestCompatCompact(t *testing.T) {
t.Parallel()
t.Logf("in=%q", tc.In)
var out bytes.Buffer
- err := Compact(&out, []byte(tc.In))
+ err := json.Compact(&out, []byte(tc.In))
assert.Equal(t, tc.Out, out.String())
if tc.Err == "" {
assert.NoError(t, err)
@@ -105,20 +109,22 @@ func TestCompatIndent(t *testing.T) {
Err string
}
testcases := map[string]testcase{
- "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`},
- "object": {In: `{}`, Out: `{}`},
- "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""},
- "float": {In: `1.200e003`, Out: `1.200e003`},
- "tailws0": {In: `0`, Out: `0`},
- "tailws1": {In: `0 `, Out: `0 `},
- "tailws2": {In: `0 `, Out: `0 `},
- "tailws3": {In: "0\n", Out: "0\n"},
- "headws1": {In: ` 0`, Out: `0`},
- "objws1": {In: `{"a" : 1}`, Out: "{\n>.\"a\": 1\n>}"},
- "objws2": {In: "{\"a\"\n:\n1}", Out: "{\n>.\"a\": 1\n>}"},
- "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`},
- "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`},
- "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`},
+ "empty": {In: ``, Out: ``, Err: `unexpected end of JSON input`},
+ "trunc": {In: `{`, Out: ``, Err: `unexpected end of JSON input`},
+ "object": {In: `{}`, Out: `{}`},
+ "non-utf8": {In: "\"\x85\xcd\"", Out: "\"\x85\xcd\""},
+ "float": {In: `1.200e003`, Out: `1.200e003`},
+ "tailws0": {In: `0`, Out: `0`},
+ "tailws1": {In: `0 `, Out: `0 `},
+ "tailws2": {In: `0 `, Out: `0 `},
+ "tailws3": {In: "0\n", Out: "0\n"},
+ "headws1": {In: ` 0`, Out: `0`},
+ "objws1": {In: `{"a" : 1}`, Out: "{\n>.\"a\": 1\n>}"},
+ "objws2": {In: "{\"a\"\n:\n1}", Out: "{\n>.\"a\": 1\n>}"},
+ "hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`},
+ "hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`},
+ "hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`},
+ "invalid-utf8": {In: "\x85", Err: `invalid character '\x85' looking for beginning of value`},
}
for tcName, tc := range testcases {
tc := tc
@@ -126,7 +132,7 @@ func TestCompatIndent(t *testing.T) {
t.Parallel()
t.Logf("in=%q", tc.In)
var out bytes.Buffer
- err := Indent(&out, []byte(tc.In), ">", ".")
+ err := json.Indent(&out, []byte(tc.In), ">", ".")
assert.Equal(t, tc.Out, out.String())
if tc.Err == "" {
assert.NoError(t, err)
@@ -153,7 +159,7 @@ func TestCompatMarshal(t *testing.T) {
tc := tc
t.Run(tcName, func(t *testing.T) {
t.Parallel()
- out, err := Marshal(tc.In)
+ out, err := json.Marshal(tc.In)
assert.Equal(t, tc.Out, string(out))
if tc.Err == "" {
assert.NoError(t, err)
@@ -181,10 +187,29 @@ func TestCompatUnmarshal(t *testing.T) {
"two-objs": {In: `{} {}`, ExpOut: nil, ExpErr: `invalid character '{' after top-level value`},
"two-numbers1": {In: `00`, ExpOut: nil, ExpErr: `invalid character '0' after top-level value`},
"two-numbers2": {In: `1 2`, ExpOut: nil, ExpErr: `invalid character '2' after top-level value`},
+ "invalid-utf8": {In: "\x85", ExpErr: `invalid character '\x85' looking for beginning of value`},
// 2e308 is slightly more than math.MaxFloat64 (~1.79e308)
"obj-overflow": {In: `{"foo":"bar", "baz":2e308, "qux": "orb"}`, ExpOut: map[string]any{"foo": "bar", "baz": nil, "qux": "orb"}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
"ary-overflow": {In: `["foo",2e308,"bar",3e308]`, ExpOut: []any{"foo", nil, "bar", nil}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
"existing-overflow": {In: `2e308`, InPtr: func() any { x := 4; return &x }(), ExpOut: 4, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type int`},
+ // syntax error messages
+ "syntax-01": {In: `{}x`, ExpErr: `invalid character 'x' after top-level value`},
+ "syntax-02": {In: `x`, ExpErr: `invalid character 'x' looking for beginning of value`},
+ "syntax-03": {In: `{x`, ExpErr: `invalid character 'x' looking for beginning of object key string`},
+ "syntax-04": {In: `{""x`, ExpErr: `invalid character 'x' after object key`},
+ "syntax-05": {In: `{"":0x`, ExpErr: `invalid character 'x' after object key:value pair`},
+ "syntax-06": {In: `[0x`, ExpErr: `invalid character 'x' after array element`},
+ "syntax-07": {In: "\"\x01\"", ExpErr: `invalid character '\x01' in string literal`},
+ "syntax-08": {In: `"\x`, ExpErr: `invalid character 'x' in string escape code`},
+ "syntax-09": {In: `"\ux`, ExpErr: `invalid character 'x' in \u hexadecimal character escape`},
+ "syntax-10": {In: `"\u0x`, ExpErr: `invalid character 'x' in \u hexadecimal character escape`},
+ "syntax-11": {In: `"\u00x`, ExpErr: `invalid character 'x' in \u hexadecimal character escape`},
+ "syntax-12": {In: `"\u000x`, ExpErr: `invalid character 'x' in \u hexadecimal character escape`},
+ "syntax-13": {In: `-x`, ExpErr: `invalid character 'x' in numeric literal`},
+ "syntax-14": {In: `0.x`, ExpErr: `invalid character 'x' after decimal point in numeric literal`},
+ "syntax-15": {In: `1ex`, ExpErr: `invalid character 'x' in exponent of numeric literal`},
+ "syntax-16": {In: `1e+x`, ExpErr: `invalid character 'x' in exponent of numeric literal`},
+ "syntax-17": {In: `fx`, ExpErr: `invalid character 'x' in literal false (expecting 'a')`},
}
for tcName, tc := range testcases {
tc := tc
@@ -195,7 +220,7 @@ func TestCompatUnmarshal(t *testing.T) {
var out any
ptr = &out
}
- err := Unmarshal([]byte(tc.In), ptr)
+ err := json.Unmarshal([]byte(tc.In), ptr)
assert.Equal(t, tc.ExpOut, reflect.ValueOf(ptr).Elem().Interface())
if tc.ExpErr == "" {
assert.NoError(t, err)
@@ -223,6 +248,7 @@ func TestCompatDecode(t *testing.T) {
"two-objs": {In: `{} {}`, ExpOut: map[string]any{}},
"two-numbers1": {In: `00`, ExpOut: float64(0)},
"two-numbers2": {In: `1 2`, ExpOut: float64(1)},
+ "invalid-utf8": {In: "\x85", ExpErr: `invalid character '\x85' looking for beginning of value`},
// 2e308 is slightly more than math.MaxFloat64 (~1.79e308)
"obj-overflow": {In: `{"foo":"bar", "baz":2e308, "qux": "orb"}`, ExpOut: map[string]any{"foo": "bar", "baz": nil, "qux": "orb"}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
"ary-overflow": {In: `["foo",2e308,"bar",3e308]`, ExpOut: []any{"foo", nil, "bar", nil}, ExpErr: `json: cannot unmarshal number 2e308 into Go value of type float64`},
@@ -237,7 +263,7 @@ func TestCompatDecode(t *testing.T) {
var out any
ptr = &out
}
- err := NewDecoder(strings.NewReader(tc.In)).Decode(ptr)
+ err := json.NewDecoder(strings.NewReader(tc.In)).Decode(ptr)
assert.Equal(t, tc.ExpOut, reflect.ValueOf(ptr).Elem().Interface())
if tc.ExpErr == "" {
assert.NoError(t, err)