-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindent_test.go
More file actions
135 lines (131 loc) · 2.64 KB
/
indent_test.go
File metadata and controls
135 lines (131 loc) · 2.64 KB
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
package jsonhelper
import (
"bytes"
"io"
"strings"
"testing"
)
func TestIndentRW(t *testing.T) {
type args struct {
r io.Reader
prefix string
indent string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "1e",
args: args{
r: &strings.Reader{},
prefix: "",
indent: " ",
},
wantErr: true,
},
{name: "1",
args: args{
r: strings.NewReader(`{"i":1,"s":"one"}`),
prefix: "",
indent: " ",
},
want: "{\n \"i\": 1,\n \"s\": \"one\"\n}",
},
{name: "2",
args: args{
r: strings.NewReader(`[{"i":1,"s":"one"}]`),
prefix: "",
indent: " ",
},
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n }\n]",
},
{name: "3",
args: args{
r: strings.NewReader(`[{"i":1,"s":"one"},{"i":2,"s":"two"}]`),
prefix: "",
indent: " ",
},
want: "[\n {\n \"i\": 1,\n \"s\": \"one\"\n },\n {\n \"i\": 2,\n \"s\": \"two\"\n }\n]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := IndentRW(tt.args.r, w, tt.args.prefix, tt.args.indent); (err != nil) != tt.wantErr {
t.Errorf("IndentRW() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got := w.String(); got != tt.want {
t.Errorf("IndentRW() = %v, want %v", got, tt.want)
}
})
}
}
func TestIndentStr(t *testing.T) {
type args struct {
j string
prefix string
indent string
}
tests := []struct {
name string
args args
want string
}{
{name: "1",
args: args{
j: `{"i":1,"s":"one"}`,
prefix: "",
indent: " ",
},
want: "{\n \"i\": 1,\n \"s\": \"one\"\n}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := IndentStr(tt.args.j, tt.args.prefix, tt.args.indent)
if got != tt.want {
t.Errorf("IndentStr() = %v, want %v", got, tt.want)
}
})
}
}
func TestIndentAny(t *testing.T) {
type args struct {
v any
prefix string
indent string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{name: "1",
args: args{
v: struct {
I int `json:"i"`
S string `json:"s"`
}{I: 1, S: "one"},
prefix: "",
indent: " ",
},
want: "{\n \"i\": 1,\n \"s\": \"one\"\n}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IndentAny(tt.args.v, tt.args.prefix, tt.args.indent)
if (err != nil) != tt.wantErr {
t.Errorf("IndentAny() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("IndentAny() = %v, want %v", got, tt.want)
}
})
}
}