Skip to content

Commit

Permalink
fix legacy unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Dec 31, 2021
1 parent 99d4a26 commit 67a26dd
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions modules/csv/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"encoding/csv"
"io"
"strconv"
"strings"
"testing"

Expand All @@ -21,14 +22,23 @@ func TestCreateReader(t *testing.T) {
assert.Equal(t, ',', rd.Comma)
}

//nolint
func decodeSlashes(s string) string {
s = strings.ReplaceAll(s, "\n", "\\n")
s = strings.ReplaceAll(s, "\"", "\\\"")
decoded, err := strconv.Unquote(`"` + s + `"`)
if err != nil {
panic("unable to decode string:" + err.Error() + "\n" + s)
}
return decoded
}

func TestCreateReaderAndDetermineDelimiter(t *testing.T) {
var cases = []struct {
csv string
expectedRows [][]string
expectedDelimiter rune
}{
// case 0 - semicolon delmited
// case 0 - semicolon delimited
{
csv: `a;b;c
1;2;3
Expand All @@ -47,11 +57,11 @@ a, b c
e f
g h i
j l
m n,
m n,\t
p q r
u
v w x
y
y\t\t
`,
expectedRows: [][]string{
{"col1", "col2", "col3"},
Expand All @@ -74,7 +84,7 @@ y
a, b, c
d,e,f
,h, i
j, ,
j, ,\x20
, , `,
expectedRows: [][]string{
{"col1", "col2", "col3"},
Expand All @@ -89,7 +99,7 @@ j, ,
}

for n, c := range cases {
rd, err := CreateReaderAndDetermineDelimiter(nil, strings.NewReader(c.csv))
rd, err := CreateReaderAndDetermineDelimiter(nil, strings.NewReader(decodeSlashes(c.csv)))
assert.NoError(t, err, "case %d: should not throw error: %v\n", n, err)
assert.EqualValues(t, c.expectedDelimiter, rd.Comma, "case %d: delimiter should be '%c', got '%c'", n, c.expectedDelimiter, rd.Comma)
rows, err := rd.ReadAll()
Expand Down Expand Up @@ -222,7 +232,7 @@ John Doe [email protected] This,note,had,a,lot,of,commas,to,test,delimters`,
}

for n, c := range cases {
delimiter := determineDelimiter(&markup.RenderContext{Filename: c.filename}, []byte(c.csv))
delimiter := determineDelimiter(&markup.RenderContext{Filename: c.filename}, []byte(decodeSlashes(c.csv)))
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
}
}
Expand Down Expand Up @@ -287,7 +297,7 @@ abc | |123
}

for n, c := range cases {
modifiedText := removeQuotedString(c.text)
modifiedText := removeQuotedString(decodeSlashes(c.text))
assert.EqualValues(t, c.expectedText, modifiedText, "case %d: modified text should be equal", n)
}
}
Expand Down Expand Up @@ -353,7 +363,7 @@ John Doe [email protected] This,note,had,a,lot,of,commas,to,test,delimters`,
quoted,
text," a
2 "some,
quoted,
quoted,\t
text," b
3 "some,
quoted,
Expand Down Expand Up @@ -442,7 +452,7 @@ jkl`,
}

for n, c := range cases {
delimiter := guessDelimiter([]byte(c.csv))
delimiter := guessDelimiter([]byte(decodeSlashes(c.csv)))
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
}
}
Expand All @@ -459,7 +469,7 @@ func TestGuessFromBeforeAfterQuotes(t *testing.T) {
quoted,
text," a
2 "some,
quoted,
quoted,\t
text," b
3 "some,
quoted,
Expand Down Expand Up @@ -534,7 +544,7 @@ a|"he said, ""here I am"""`,
}

for n, c := range cases {
delimiter := guessFromBeforeAfterQuotes([]byte(c.csv))
delimiter := guessFromBeforeAfterQuotes([]byte(decodeSlashes(c.csv)))
assert.EqualValues(t, c.expectedDelimiter, delimiter, "case %d: delimiter should be equal, expected '%c' got '%c'", n, c.expectedDelimiter, delimiter)
}
}
Expand Down

0 comments on commit 67a26dd

Please sign in to comment.