-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99d4a26
commit 67a26dd
Showing
1 changed file
with
22 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"bytes" | ||
"encoding/csv" | ||
"io" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
|
@@ -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 | ||
|
@@ -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"}, | ||
|
@@ -74,7 +84,7 @@ y | |
a, b, c | ||
d,e,f | ||
,h, i | ||
j, , | ||
j, ,\x20 | ||
, , `, | ||
expectedRows: [][]string{ | ||
{"col1", "col2", "col3"}, | ||
|
@@ -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() | ||
|
@@ -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) | ||
} | ||
} | ||
|
@@ -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) | ||
} | ||
} | ||
|
@@ -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, | ||
|
@@ -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) | ||
} | ||
} | ||
|
@@ -459,7 +469,7 @@ func TestGuessFromBeforeAfterQuotes(t *testing.T) { | |
quoted, | ||
text," a | ||
2 "some, | ||
quoted, | ||
quoted,\t | ||
text," b | ||
3 "some, | ||
quoted, | ||
|
@@ -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) | ||
} | ||
} | ||
|