-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert_test.go
189 lines (158 loc) · 4.03 KB
/
convert_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/csv"
"io/ioutil"
"strings"
"testing"
)
// test for Convert()
func TestConvert(t *testing.T) {
t.Run("should pass", func(t *testing.T) {
testFile := "./testdata/test1.csv"
args := &Args{
Files: []string{testFile},
Delim: ",",
Pad: 2,
}
expectedTestFile := "./testdata/test1_expected.md"
expected := readFile(expectedTestFile)
md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
}
if md != expected {
t.Errorf("Convert is something wrong. \n expected: %q \n output : %q", expected, md)
}
})
t.Run("should pass", func(t *testing.T) {
testFile := "./testdata/test2.csv"
args := &Args{
Files: []string{testFile},
Delim: ",",
Pad: 2,
}
expectedTestFile := "./testdata/test2_expected.md"
expected := readFile(expectedTestFile)
md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
}
if md != expected {
t.Errorf("Convert is something wrong. \n expected: %q \n output : %q", expected, md)
}
})
t.Run("should pass", func(t *testing.T) {
testFile := "./testdata/test3.csv"
args := &Args{
Files: []string{testFile},
Delim: ",",
Pad: 2,
}
expectedTestFile := "./testdata/test3_expected.md"
expected := readFile(expectedTestFile)
md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
}
if md != expected {
t.Errorf("Convert is something wrong. \n expected: %q \n output : %q", expected, md)
}
})
t.Run("should pass", func(t *testing.T) {
testFile := "./testdata/test4.csv"
args := &Args{
Files: []string{testFile},
Delim: ",",
Pad: 2,
}
expectedTestFile := "./testdata/test4_expected.md"
expected := readFile(expectedTestFile)
md, err := Convert(testFile, args)
if err != nil {
t.Errorf(err.Error())
}
if md != expected {
t.Errorf("Convert is something wrong. \n expected: %q \n output : %q", expected, md)
}
})
}
// test for Modify function.
func TestModifyFunc(t *testing.T) {
in := `first_name,last_name,username,comment
"Rob","Pike",rob,hello
Ken,Thompson,ken,"Hi.
I am good."
"Robert","Griesemer","gri","Nice to meet you.
Thank you"
`
expected := `first_name,last_name,username,comment
"Rob","Pike",rob,hello
Ken,Thompson,ken,"Hi.<br/>I am good."
"Robert","Griesemer","gri","Nice to meet you.<br/>Thank you"
`
r_in := csv.NewReader(strings.NewReader(in))
records, _ := r_in.ReadAll()
r_expected := csv.NewReader(strings.NewReader(expected))
records_expected, _ := r_expected.ReadAll()
out := Modify(records)
if !isSame2dStringArray(out, records_expected) {
t.Errorf("Modify is somethig wrong. \n expected: %q \n output : %q", records_expected, out)
}
}
// test for colMaxSize function.
func TestColMaxSize(t *testing.T) {
t.Run("should pass", func(t *testing.T) {
rows := [][]string{}
row1 := []string{"hello", "ok", "see you soon."}
rows = append(rows, row1)
expected := []int{5, 2, 13}
out := colMaxSize(rows)
if !isSame1dIntArray(expected, out) {
t.Errorf("colMaxSize is something wrong. \n expected: %v \n output : %v", expected, out)
}
})
t.Run("should pass", func(t *testing.T) {
rows := [][]string{}
row1 := []string{"はい", "こんにちは", "お願いします."}
rows = append(rows, row1)
expected := []int{2, 5, 7}
out := colMaxSize(rows)
if !isSame1dIntArray(expected, out) {
t.Errorf("colMaxSize is something wrong. \n expected: %v \n output : %v", expected, out)
}
})
}
func isSame1dIntArray(arr1, arr2 []int) bool {
if len(arr1) != len(arr2) {
return false
}
for i, v := range arr1 {
if v != arr2[i] {
return false
}
}
return true
}
func isSame2dStringArray(arr1, arr2 [][]string) bool {
if len(arr1) != len(arr2) {
return false
}
for i, v := range arr1 {
if len(v) != len(arr2[i]) {
return false
}
for j, x := range v {
if x != arr2[i][j] {
return false
}
}
}
return true
}
func readFile(filename string) string {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
return strings.TrimSpace(string(bytes))
}