This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabula_test.go
96 lines (77 loc) · 1.63 KB
/
tabula_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
// Copyright 2017 M. 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 tabula_test
import (
"os"
"reflect"
"runtime"
"testing"
"github.com/shuLhan/tabula"
)
var (
traces = make([]byte, 1024)
)
func printStackTrace() {
var lines, start, end int
for x, b := range traces {
if b != '\n' {
continue
}
lines++
if lines == 3 {
start = x
} else if lines == 5 {
end = x + 1
break
}
}
os.Stderr.Write(traces[start:end])
}
func assert(t *testing.T, exp, got interface{}, equal bool) {
if reflect.DeepEqual(exp, got) != equal {
runtime.Stack(traces, true)
printStackTrace()
t.Fatalf("\n"+
">>> Expecting '%v'\n"+
" got '%v'\n", exp, got)
}
}
var testColTypes = []int{
tabula.TInteger,
tabula.TInteger,
tabula.TInteger,
tabula.TString,
}
var testColNames = []string{"int01", "int02", "int03", "class"}
// Testing data and function for Rows and MapRows
var rowsData = [][]string{
{"1", "5", "9", "+"},
{"2", "6", "0", "-"},
{"3", "7", "1", "-"},
{"4", "8", "2", "+"},
}
var testClassIdx = 3
var rowsExpect = []string{
"&[1 5 9 +]",
"&[2 6 0 -]",
"&[3 7 1 -]",
"&[4 8 2 +]",
}
var groupByExpect = "[{+ &[1 5 9 +]&[4 8 2 +]} {- &[2 6 0 -]&[3 7 1 -]}]"
func initRows() (rows tabula.Rows, e error) {
for i := range rowsData {
l := len(rowsData[i])
row := make(tabula.Row, 0)
for j := 0; j < l; j++ {
rec, e := tabula.NewRecordBy(rowsData[i][j],
testColTypes[j])
if nil != e {
return nil, e
}
row = append(row, rec)
}
rows.PushBack(&row)
}
return rows, nil
}