-
Notifications
You must be signed in to change notification settings - Fork 20
/
linter_test.go
85 lines (80 loc) · 2.18 KB
/
linter_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
package csvlint
import (
"encoding/csv"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
var validationTable = []struct {
file string
err error
invalids []CSVError
comma rune
halted bool
}{
{file: "./test_data/perfect.csv", err: nil, invalids: []CSVError{}},
{file: "./test_data/perfect_tab.csv", err: nil, comma: '\t', invalids: []CSVError{}},
{file: "./test_data/perfect_pipe.csv", err: nil, comma: '|', invalids: []CSVError{}},
{file: "./test_data/perfect_colon.csv", err: nil, comma: ':', invalids: []CSVError{}},
{file: "./test_data/perfect_semicolon.csv", err: nil, comma: ';', invalids: []CSVError{}},
{file: "./test_data/one_long_column.csv", err: nil, invalids: []CSVError{{
Record: []string{"d", "e", "f", "g"},
err: csv.ErrFieldCount,
Num: 2,
}}},
{file: "./test_data/mult_long_columns.csv", err: nil, invalids: []CSVError{
{
Record: []string{"d", "e", "f", "g"},
err: csv.ErrFieldCount,
Num: 2,
}, {
Record: []string{"k", "l", "m", "n"},
err: csv.ErrFieldCount,
Num: 4,
}},
},
{file: "./test_data/mult_long_columns_tabs.csv", err: nil, comma: '\t', invalids: []CSVError{
{
Record: []string{"d", "e", "f", "g"},
err: csv.ErrFieldCount,
Num: 2,
}, {
Record: []string{"k", "l", "m", "n"},
err: csv.ErrFieldCount,
Num: 4,
}},
},
}
func TestTable(t *testing.T) {
for _, test := range validationTable {
f, err := os.Open(test.file)
assert.Nil(t, err)
defer f.Close()
comma := test.comma
if test.comma == 0 {
comma = ','
}
invalids, halted, err := Validate(f, comma, false)
assert.Equal(t, test.err, err)
assert.Equal(t, halted, test.halted)
assert.Equal(t, test.invalids, invalids)
}
}
var errTable = []struct {
err error
message string
}{
{
err: CSVError{Record: []string{"a", "b", "c"}, Num: 3, err: csv.ErrFieldCount},
message: "Record #3 has error: wrong number of fields",
},
{
err: CSVError{Record: []string{"d", "e", "f"}, Num: 1, err: csv.ErrBareQuote},
message: `Record #1 has error: bare " in non-quoted-field`,
},
}
func TestErrors(t *testing.T) {
for _, test := range errTable {
assert.Equal(t, test.err.Error(), test.message)
}
}