-
Notifications
You must be signed in to change notification settings - Fork 2
/
common_test.go
172 lines (142 loc) · 3.17 KB
/
common_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
169
170
171
172
// Copyright 2015-2018, Shulhan <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dsv_test
import (
"bytes"
"fmt"
"github.com/shuLhan/dsv"
"github.com/shuLhan/tabula"
"io"
"io/ioutil"
"reflect"
"runtime/debug"
"testing"
)
func assert(t *testing.T, exp, got interface{}, equal bool) {
if reflect.DeepEqual(exp, got) != equal {
debug.PrintStack()
t.Fatalf("\n"+
">>> Expecting '%v'\n"+
" got '%v'\n", exp, got)
}
}
//
// assertFile compare content of two file, print error message and exit
// when both are different.
//
func assertFile(t *testing.T, a, b string, equal bool) {
out, e := ioutil.ReadFile(a)
if nil != e {
debug.PrintStack()
t.Error(e)
}
exp, e := ioutil.ReadFile(b)
if nil != e {
debug.PrintStack()
t.Error(e)
}
r := bytes.Compare(out, exp)
if equal && 0 != r {
debug.PrintStack()
t.Fatal("Comparing", a, "with", b, ": result is different (",
r, ")")
}
}
func checkDataset(t *testing.T, r *dsv.Reader, exp string) {
var got string
ds := r.GetDataset().(tabula.DatasetInterface)
data := ds.GetData()
switch data.(type) {
case *tabula.Rows:
rows := data.(*tabula.Rows)
got = fmt.Sprint(*rows)
case *tabula.Columns:
cols := data.(*tabula.Columns)
got = fmt.Sprint(*cols)
case *tabula.Matrix:
matrix := data.(*tabula.Matrix)
got = fmt.Sprint(*matrix)
default:
fmt.Println("data type unknown")
}
assert(t, exp, got, true)
}
//
// doReadWrite test reading and writing the DSV data.
//
func doReadWrite(t *testing.T, dsvReader *dsv.Reader, dsvWriter *dsv.Writer,
expectation []string, check bool) {
i := 0
for {
n, e := dsv.Read(dsvReader)
if e == io.EOF || n == 0 {
_, e = dsvWriter.Write(dsvReader)
if e != nil {
t.Fatal(e)
}
break
}
if e != nil {
continue
}
if check {
checkDataset(t, dsvReader, expectation[i])
i++
}
_, e = dsvWriter.Write(dsvReader)
if e != nil {
t.Fatal(e)
}
}
e := dsvWriter.Flush()
if e != nil {
t.Fatal(e)
}
}
var datasetRows = [][]string{
{"0", "1", "A"},
{"1", "1.1", "B"},
{"2", "1.2", "A"},
{"3", "1.3", "B"},
{"4", "1.4", "C"},
{"5", "1.5", "D"},
{"6", "1.6", "C"},
{"7", "1.7", "D"},
{"8", "1.8", "E"},
{"9", "1.9", "F"},
}
var datasetCols = [][]string{
{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"},
{"1", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "1.9"},
{"A", "B", "A", "B", "C", "D", "C", "D", "E", "F"},
}
var datasetTypes = []int{
tabula.TInteger,
tabula.TReal,
tabula.TString,
}
var datasetNames = []string{"int", "real", "string"}
func populateWithRows(t *testing.T, dataset *tabula.Dataset) {
for _, rowin := range datasetRows {
row := make(tabula.Row, len(rowin))
for x, recin := range rowin {
rec, e := tabula.NewRecordBy(recin, datasetTypes[x])
if e != nil {
t.Fatal(e)
}
row[x] = rec
}
dataset.PushRow(&row)
}
}
func populateWithColumns(t *testing.T, dataset *tabula.Dataset) {
for x := range datasetCols {
col, e := tabula.NewColumnString(datasetCols[x], datasetTypes[x],
datasetNames[x])
if e != nil {
t.Fatal(e)
}
dataset.PushColumn(*col)
}
}