summaryrefslogtreecommitdiff
path: root/compat/json/compat_test.go
blob: d989a4dc994d94e687b77c4814317fe83f5ef684 (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
// 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 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
	}
	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\""},
	}
	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\""},
	}
	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\""},
	}
	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)
			}
		})
	}
}