forked from digisan/json-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_test.go
132 lines (120 loc) · 2.32 KB
/
basic_test.go
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
package jsontool
import (
"fmt"
"os"
"path/filepath"
"testing"
)
func TestIsValid(t *testing.T) {
dir := "./data/"
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
failOnErrWhen(info == nil, "%v", err)
if jsonFile := info.Name(); sHasSuffix(jsonFile, ".json") {
fPln("--->", jsonFile)
bytes, _ := os.ReadFile(dir + jsonFile)
str := string(bytes)
if !IsValidStr(str) {
os.WriteFile(fSf("debug_%s.json", jsonFile), []byte(str), 0666)
panic("error on MkJSON")
}
//if jsonFile == "CensusCollection_0.xml" {
// os.WriteFile(fSf("record_%s.json", jsonFile), []byte(str), 0666)
//}
}
return nil
})
}
func TestMinimize(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
{
name: "OK",
args: args{
str: `{ "Id": 1, "Name": "Ahmad, Ahmad", "Age": "2 1" }`,
},
want: `{"Id":1,"Name":"Ahmad, Ahmad","Age":"2 1"}`,
},
{
name: "OK1",
args: args{
str: ` `,
},
want: ``,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TryMinimize(tt.args.str)
fmt.Println("try:", got)
got = Minimize(tt.args.str, false)
fmt.Println("mini", got)
got = Minimize(tt.args.str, true)
fmt.Println("check:", got)
})
}
}
func TestMarshalRemove(t *testing.T) {
type Object struct {
A int
B string
C bool
}
object := Object{
A: 1,
B: "b",
C: true,
}
type Obj struct {
A int `json:"a"`
B string `json:"b"`
C bool `json:"c"`
}
obj := Obj{
A: 2,
B: "bb",
C: false,
}
type args struct {
v any
mFieldOldNew map[string]string
fields []string
}
tests := []struct {
name string
args args
wantBytes []byte
wantErr bool
}{
// TODO: Add test cases.
{
name: "OK",
args: args{
v: object,
mFieldOldNew: map[string]string{"B": "BB"},
fields: []string{"A"},
},
},
{
name: "OK",
args: args{
v: obj,
mFieldOldNew: map[string]string{"b": "bbb"},
fields: []string{"a"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotBytes, _ := MarshalRemove(tt.args.v, tt.args.mFieldOldNew, tt.args.fields...)
r := string(gotBytes)
fmt.Println(r)
})
}
}