forked from fernomac/ion-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_test.go
127 lines (107 loc) · 2.29 KB
/
reader_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
package ion
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
var blacklist = map[string]bool{
"ion-tests/iontestdata/good/emptyAnnotatedInt.10n": true,
"ion-tests/iontestdata/good/subfieldVarUInt32bit.ion": true,
"ion-tests/iontestdata/good/utf16.ion": true,
"ion-tests/iontestdata/good/utf32.ion": true,
"ion-tests/iontestdata/good/whitespace.ion": true,
"ion-tests/iontestdata/good/item1.10n": true,
}
type drainfunc func(t *testing.T, r Reader, f string)
func TestReadFiles(t *testing.T) {
testReadDir(t, "ion-tests/iontestdata/good", func(t *testing.T, r Reader, f string) {
drain(t, r, 0)
})
}
func drain(t *testing.T, r Reader, level int) {
for r.Next() {
// print(level, r.Type())
if !r.IsNull() {
switch r.Type() {
case StructType, ListType, SexpType:
if err := r.StepIn(); err != nil {
t.Fatal(err)
}
drain(t, r, level+1)
if err := r.StepOut(); err != nil {
t.Fatal(err)
}
}
}
}
if r.Err() != nil {
t.Fatal(r.Err())
}
}
func print(level int, obj interface{}) {
fmt.Print(" > ")
for i := 0; i < level; i++ {
fmt.Print(" ")
}
fmt.Println(obj)
}
func TestDecodeFiles(t *testing.T) {
testReadDir(t, "ion-tests/iontestdata/good", func(t *testing.T, r Reader, f string) {
// fmt.Println(f)
d := NewDecoder(r)
for {
v, err := d.Decode()
if err == ErrNoInput {
break
}
if err != nil {
t.Fatal(err)
}
// fmt.Println(v)
_ = v
}
})
}
var emptyFiles = []string{
"ion-tests/iontestdata/good/blank.ion",
"ion-tests/iontestdata/good/empty.ion",
}
func isEmptyFile(f string) bool {
for _, s := range emptyFiles {
if f == s {
return true
}
}
return false
}
func testReadDir(t *testing.T, path string, d drainfunc) {
files, err := ioutil.ReadDir(path)
if err != nil {
t.Fatal(err)
}
for _, file := range files {
fp := filepath.Join(path, file.Name())
if file.IsDir() {
testReadDir(t, fp, d)
} else {
t.Run(fp, func(t *testing.T) {
testReadFile(t, fp, d)
})
}
}
}
func testReadFile(t *testing.T, path string, d drainfunc) {
if _, ok := blacklist[path]; ok {
return
}
// fmt.Println(path)
file, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer file.Close()
r := NewReader(file)
d(t, r, path)
}