Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

indent_test.gno

2.14 Kb · 84 lines
 1package json
 2
 3import (
 4	"bytes"
 5	"testing"
 6)
 7
 8func TestIndentJSON(t *testing.T) {
 9	tests := []struct {
10		name     string
11		input    []byte
12		indent   string
13		expected []byte
14	}{
15		{
16			name:     "empty object",
17			input:    []byte(`{}`),
18			indent:   "  ",
19			expected: []byte(`{}`),
20		},
21		{
22			name:     "empty array",
23			input:    []byte(`[]`),
24			indent:   "  ",
25			expected: []byte(`[]`),
26		},
27		{
28			name:     "nested object",
29			input:    []byte(`{{}}`),
30			indent:   "\t",
31			expected: []byte("{\n\t\t{}\n}"),
32		},
33		{
34			name:     "nested array",
35			input:    []byte(`[[[]]]`),
36			indent:   "\t",
37			expected: []byte("[[\n\t\t[\n\t\t\t\t\n\t\t]\n]]"),
38		},
39		{
40			name:     "top-level array",
41			input:    []byte(`["apple","banana","cherry"]`),
42			indent:   "\t",
43			expected: []byte(`["apple","banana","cherry"]`),
44		},
45		{
46			name:     "array of arrays",
47			input:    []byte(`["apple",["banana","cherry"],"date"]`),
48			indent:   "  ",
49			expected: []byte("[\"apple\",[\n    \"banana\",\n    \"cherry\"\n],\"date\"]"),
50		},
51
52		{
53			name:     "nested array in object",
54			input:    []byte(`{"fruits":["apple",["banana","cherry"],"date"]}`),
55			indent:   "  ",
56			expected: []byte("{\n    \"fruits\": [\"apple\",[\n        \"banana\",\n        \"cherry\"\n    ],\"date\"]\n}"),
57		},
58		{
59			name:     "complex nested structure",
60			input:    []byte(`{"data":{"array":[1,2,3],"bool":true,"nestedArray":[["a","b"],"c"]}}`),
61			indent:   "  ",
62			expected: []byte("{\n    \"data\": {\n        \"array\": [1,2,3],\"bool\": true,\"nestedArray\": [[\n            \"a\",\n            \"b\"\n        ],\"c\"]\n    }\n}"),
63		},
64		{
65			name:     "custom ident character",
66			input:    []byte(`{"fruits":["apple",["banana","cherry"],"date"]}`),
67			indent:   "*",
68			expected: []byte("{\n**\"fruits\": [\"apple\",[\n****\"banana\",\n****\"cherry\"\n**],\"date\"]\n}"),
69		},
70	}
71
72	for _, tt := range tests {
73		t.Run(tt.name, func(t *testing.T) {
74			actual, err := Indent(tt.input, tt.indent)
75			if err != nil {
76				t.Errorf("IndentJSON() error = %v", err)
77				return
78			}
79			if !bytes.Equal(actual, tt.expected) {
80				t.Errorf("IndentJSON() = %q, want %q", actual, tt.expected)
81			}
82		})
83	}
84}