summaryrefslogtreecommitdiff
path: root/compat/json/compat_test.go
blob: 29a8b372e00ba7bad48b43c519cc861297da9ed9 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (C) 2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package json

import (
	"bytes"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestCompatHTMLEscape(t *testing.T) {
	t.Parallel()
	type testcase struct {
		In  string
		Out string
	}
	testcases := map[string]testcase{
		"invalid":   {In: `x`, Out: `x`},
		"hex-lower": {In: `"\uabcd"`, Out: `"\uabcd"`},
		"hex-upper": {In: `"\uABCD"`, Out: `"\uABCD"`},
		"hex-mixed": {In: `"\uAbCd"`, Out: `"\uAbCd"`},
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			t.Parallel()
			t.Logf("in=%q", tc.In)
			var dst bytes.Buffer
			HTMLEscape(&dst, []byte(tc.In))
			assert.Equal(t, tc.Out, dst.String())
		})
	}
}

func TestCompatValid(t *testing.T) {
	t.Parallel()
	type testcase struct {
		In  string
		Exp bool
	}
	testcases := map[string]testcase{
		"empty":     {In: ``, Exp: false},
		"num":       {In: `1`, Exp: true},
		"trunc":     {In: `{`, Exp: false},
		"object":    {In: `{}`, Exp: true},
		"non-utf8":  {In: "\"\x85\xcd\"", Exp: false}, // https://github.com/golang/go/issues/58517
		"hex-lower": {In: `"\uabcd"`, Exp: true},
		"hex-upper": {In: `"\uABCD"`, Exp: true},
		"hex-mixed": {In: `"\uAbCd"`, Exp: true},
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			t.Parallel()
			t.Logf("in=%q", tc.In)
			act := Valid([]byte(tc.In))
			assert.Equal(t, tc.Exp, act)
		})
	}
}

func TestCompatCompact(t *testing.T) {
	t.Parallel()
	type testcase struct {
		In  string
		Out string
		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"`},
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			t.Parallel()
			t.Logf("in=%q", tc.In)
			var out bytes.Buffer
			err := Compact(&out, []byte(tc.In))
			assert.Equal(t, tc.Out, out.String())
			if tc.Err == "" {
				assert.NoError(t, err)
			} else {
				assert.EqualError(t, err, tc.Err)
			}
		})
	}
}

func TestCompatIndent(t *testing.T) {
	t.Parallel()
	type testcase struct {
		In  string
		Out string
		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"`},
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			t.Parallel()
			t.Logf("in=%q", tc.In)
			var out bytes.Buffer
			err := Indent(&out, []byte(tc.In), ">", ".")
			assert.Equal(t, tc.Out, out.String())
			if tc.Err == "" {
				assert.NoError(t, err)
			} else {
				assert.EqualError(t, err, tc.Err)
			}
		})
	}
}

func TestCompatMarshal(t *testing.T) {
	t.Parallel()
	type testcase struct {
		In  any
		Out string
		Err string
	}
	testcases := map[string]testcase{
		"non-utf8": {In: "\x85\xcd", Out: "\"\\ufffd\\ufffd\""},
		"urc":      {In: "\ufffd", Out: "\"\ufffd\""},
		"float":    {In: 1.2e3, Out: `1200`},
	}
	for tcName, tc := range testcases {
		tc := tc
		t.Run(tcName, func(t *testing.T) {
			t.Parallel()
			out, err := Marshal(tc.In)
			assert.Equal(t, tc.Out, string(out))
			if tc.Err == "" {
				assert.NoError(t, err)
			} else {
				assert.EqualError(t, err, tc.Err)
			}
		})
	}
}