-
Notifications
You must be signed in to change notification settings - Fork 112
/
files_test.go
168 lines (143 loc) · 3.68 KB
/
files_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package mxj
import (
"fmt"
"testing"
)
func TestFilesHeader(t *testing.T) {
fmt.Println("\n---------------- files_test.go ...")
}
func TestNewJsonFile(t *testing.T) {
fmt.Println("NewMapsFromJsonFile()")
am, err := NewMapsFromJsonFile("files_test.json")
if err != nil {
t.Fatal(err.Error())
}
for _, v := range am {
fmt.Printf("%v\n", v)
}
am, err = NewMapsFromJsonFile("nil")
if err == nil {
t.Fatal("no error returned for read of nil file")
}
fmt.Println("caught error: ", err.Error())
am, err = NewMapsFromJsonFile("files_test.badjson")
if err == nil {
t.Fatal("no error returned for read of badjson file")
}
fmt.Println("caught error: ", err.Error())
}
func TestNewJsonFileRaw(t *testing.T) {
fmt.Println("NewMapsFromJsonFileRaw()")
mr, err := NewMapsFromJsonFileRaw("files_test.json")
if err != nil {
t.Fatal(err.Error())
}
for _, v := range mr {
fmt.Printf("%v\n", v)
}
mr, err = NewMapsFromJsonFileRaw("nil")
if err == nil {
t.Fatal("no error returned for read of nil file")
}
fmt.Println("caught error: ", err.Error())
mr, err = NewMapsFromJsonFileRaw("files_test.badjson")
if err == nil {
t.Fatal("no error returned for read of badjson file")
}
fmt.Println("caught error: ", err.Error())
}
func TestNewXmFile(t *testing.T) {
fmt.Println("NewMapsFromXmlFile()")
am, err := NewMapsFromXmlFile("files_test.xml")
if err != nil {
t.Fatal(err.Error())
}
for _, v := range am {
fmt.Printf("%v\n", v)
}
am, err = NewMapsFromXmlFile("nil")
if err == nil {
t.Fatal("no error returned for read of nil file")
}
fmt.Println("caught error: ", err.Error())
am, err = NewMapsFromXmlFile("files_test.badxml")
if err == nil {
t.Fatal("no error returned for read of badjson file")
}
fmt.Println("caught error: ", err.Error())
}
func TestNewXmFileRaw(t *testing.T) {
fmt.Println("NewMapsFromXmlFileRaw()")
mr, err := NewMapsFromXmlFileRaw("files_test.xml")
if err != nil {
t.Fatal(err.Error())
}
for _, v := range mr {
fmt.Printf("%v\n", v)
}
mr, err = NewMapsFromXmlFileRaw("nil")
if err == nil {
t.Fatal("no error returned for read of nil file")
}
fmt.Println("caught error: ", err.Error())
mr, err = NewMapsFromXmlFileRaw("files_test.badxml")
if err == nil {
t.Fatal("no error returned for read of badjson file")
}
fmt.Println("caught error: ", err.Error())
}
func TestMaps(t *testing.T) {
fmt.Println("TestMaps()")
mvs := NewMaps()
for i := 0; i < 2; i++ {
m, _ := NewMapJson([]byte(`{ "this":"is", "a":"test" }`))
mvs = append(mvs, m)
}
fmt.Println("mvs:", mvs)
s, _ := mvs.JsonString()
fmt.Println("JsonString():", s)
s, _ = mvs.JsonStringIndent("", " ")
fmt.Println("JsonStringIndent():", s)
s, _ = mvs.XmlString()
fmt.Println("XmlString():", s)
s, _ = mvs.XmlStringIndent("", " ")
fmt.Println("XmlStringIndent():", s)
}
func TestJsonFile(t *testing.T) {
am, err := NewMapsFromJsonFile("files_test.json")
if err != nil {
t.Fatal(err.Error())
}
for _, v := range am {
fmt.Printf("%v\n", v)
}
err = am.JsonFile("files_test_dup.json")
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("files_test_dup.json written")
err = am.JsonFileIndent("files_test_indent.json", "", " ")
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("files_test_indent.json written")
}
func TestXmlFile(t *testing.T) {
am, err := NewMapsFromXmlFile("files_test.xml")
if err != nil {
t.Fatal(err.Error())
}
for _, v := range am {
fmt.Printf("%v\n", v)
}
err = am.XmlFile("files_test_dup.xml")
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("files_test_dup.xml written")
err = am.XmlFileIndent("files_test_indent.xml", "", " ")
if err != nil {
t.Fatal(err.Error())
}
fmt.Println("files_test_indent.xml written")
}