@@ -33,23 +33,23 @@ func TestIndentRW(t *testing.T) {
33
33
prefix : "" ,
34
34
indent : " " ,
35
35
},
36
- want : "{\n \" i\" : 1,\n \" s\" : \" string\" \n }\n " ,
36
+ want : "{\n \" i\" : 1,\n \" s\" : \" string\" \n }" ,
37
37
},
38
38
{name : "2" ,
39
39
args : args {
40
40
r : strings .NewReader (`[{"i":1,"s":"one"}]` ),
41
41
prefix : "" ,
42
42
indent : " " ,
43
43
},
44
- want : "[\n {\n \" i\" : 1,\n \" s\" : \" one\" \n }\n ]\n " ,
44
+ want : "[\n {\n \" i\" : 1,\n \" s\" : \" one\" \n }\n ]" ,
45
45
},
46
46
{name : "3" ,
47
47
args : args {
48
48
r : strings .NewReader (`[{"i":1,"s":"one"},{"i":2,"s":"two"}]` ),
49
49
prefix : "" ,
50
50
indent : " " ,
51
51
},
52
- want : "[\n {\n \" i\" : 1,\n \" s\" : \" one\" \n },\n {\n \" i\" : 2,\n \" s\" : \" two\" \n }\n ]\n " ,
52
+ want : "[\n {\n \" i\" : 1,\n \" s\" : \" one\" \n },\n {\n \" i\" : 2,\n \" s\" : \" two\" \n }\n ]" ,
53
53
},
54
54
}
55
55
for _ , tt := range tests {
@@ -68,7 +68,7 @@ func TestIndentRW(t *testing.T) {
68
68
69
69
func TestIndentStr (t * testing.T ) {
70
70
type args struct {
71
- json string
71
+ j string
72
72
prefix string
73
73
indent string
74
74
}
@@ -79,18 +79,57 @@ func TestIndentStr(t *testing.T) {
79
79
}{
80
80
{name : "1" ,
81
81
args : args {
82
- json : `{"i":1,"s":"string"}` ,
82
+ j : `{"i":1,"s":"string"}` ,
83
83
prefix : "" ,
84
84
indent : " " ,
85
85
},
86
- want : "{\n \" i\" : 1,\n \" s\" : \" string\" \n }\n " ,
86
+ want : "{\n \" i\" : 1,\n \" s\" : \" string\" \n }" ,
87
87
},
88
88
}
89
89
for _ , tt := range tests {
90
90
t .Run (tt .name , func (t * testing.T ) {
91
- if got , _ := IndentStr (tt .args .json , tt .args .prefix , tt .args .indent ); got != tt .want {
91
+ got , _ := IndentStr (tt .args .j , tt .args .prefix , tt .args .indent )
92
+ if got != tt .want {
92
93
t .Errorf ("IndentStr() = %v, want %v" , got , tt .want )
93
94
}
94
95
})
95
96
}
96
97
}
98
+
99
+ func TestIndentAny (t * testing.T ) {
100
+ type args struct {
101
+ v any
102
+ prefix string
103
+ indent string
104
+ }
105
+ tests := []struct {
106
+ name string
107
+ args args
108
+ want string
109
+ wantErr bool
110
+ }{
111
+ {name : "1" ,
112
+ args : args {
113
+ v : struct {
114
+ I int `json:"i"`
115
+ S string `json:"s"`
116
+ }{I : 1 , S : "string" },
117
+ prefix : "" ,
118
+ indent : " " ,
119
+ },
120
+ want : "{\n \" i\" : 1,\n \" s\" : \" string\" \n }" ,
121
+ },
122
+ }
123
+ for _ , tt := range tests {
124
+ t .Run (tt .name , func (t * testing.T ) {
125
+ got , err := IndentAny (tt .args .v , tt .args .prefix , tt .args .indent )
126
+ if (err != nil ) != tt .wantErr {
127
+ t .Errorf ("IndentAny() error = %v, wantErr %v" , err , tt .wantErr )
128
+ return
129
+ }
130
+ if got != tt .want {
131
+ t .Errorf ("IndentAny() = %v, want %v" , got , tt .want )
132
+ }
133
+ })
134
+ }
135
+ }
0 commit comments