-
Notifications
You must be signed in to change notification settings - Fork 3
/
readjson_test.go
64 lines (54 loc) · 1.76 KB
/
readjson_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
package checkjson
import (
"fmt"
"os"
"testing"
)
var data = []string{
`{"test":"data"}`,
`{"test2":"2","again":"3","and more":"5"}`,
`{"test3":[{"sub1":"s1"},{"sub2":"s2","sub3":"s3","3":[{"here":"again"}]}],"some":"more","sentence":"Now is the time for all good men ..."}`,
`{"encrypt":"Encrypt","decrypt":"Decrypt","files":[{"encryptfile":"EncryptFile","encryptjsonfile":"EncryptJsonFile#","decryptfile":"DecryptFile\n","decryptjsonfile":"DecryptJsonFile\""}]}`,
`{"myname":"is","Inigo":"Montoya"}`,
`{"key":"value","key2":{"key3":"value2","key4":"value4"}}`,
}
// read in a file: should see if it will unmarshal properly, then write it
// reread it and compare with original -
// read/write on Buffer are implicit, since used by JsonFile functions
func TestReadJSONFile(t *testing.T) {
fmt.Println("============= TestReadJSONFile ...")
ss, err := ReadJSONFile("data.json")
if err != nil {
t.Errorf("ReadJsonFile err: %s", err.Error())
}
for i := range ss {
if string(ss[i]) != data[i] {
t.Errorf("rwjson ERROR: string mismatch.\nin >>%s\nout>>%s", string(ss[i]), data[i])
}
}
}
func TestReadJSONReader(t *testing.T) {
fmt.Println("============= TestReadJSONReader ...")
fh, err := os.Open("data.json")
if err != nil {
t.Errorf("err opening data.json: %s", err.Error())
}
defer fh.Close()
for i := 0; i < len(data); i++ {
j, err := ReadJSONReader(fh)
if err != nil {
t.Errorf("ReadJSONFromReader err: %s", err.Error())
}
if string(j) != data[i] {
t.Errorf("rwjson ERROR: string mismatch.\nin >>%s\nout>>%s", string(j), data[i])
}
}
}
func TestBadJSON(t *testing.T) {
fmt.Println("============= TestBadJSON ...")
_, err := ReadJSONFile("baddata.json")
if err == nil {
t.Fatal("didn't catch error")
}
fmt.Println("err ok:", err)
}