2019-05-24 09:33:42 -04:00
|
|
|
// Test ported from Golang
|
|
|
|
// https://github.com/golang/go/blob/2cc15b1/src/encoding/csv/reader_test.go
|
|
|
|
import { assertEquals, assert } from "../testing/asserts.ts";
|
2019-12-20 15:21:30 -05:00
|
|
|
import { readMatrix, parse } from "./csv.ts";
|
2019-05-24 09:33:42 -04:00
|
|
|
import { StringReader } from "../io/readers.ts";
|
|
|
|
import { BufReader } from "../io/bufio.ts";
|
|
|
|
|
|
|
|
const ErrInvalidDelim = "Invalid Delimiter";
|
|
|
|
const ErrFieldCount = "wrong number of fields";
|
|
|
|
const ErrBareQuote = 'bare " in non-quoted-field';
|
|
|
|
|
|
|
|
// TODO(zekth): Activate remaining tests
|
|
|
|
const testCases = [
|
|
|
|
{
|
|
|
|
Name: "Simple",
|
|
|
|
Input: "a,b,c\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["a", "b", "c"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "CRLF",
|
|
|
|
Input: "a,b\r\nc,d\r\n",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["c", "d"],
|
|
|
|
],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BareCR",
|
|
|
|
Input: "a,b\rc,d\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["a", "b\rc", "d"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "RFC4180test",
|
|
|
|
Input: `#field1,field2,field3
|
|
|
|
"aaa","bbb","ccc"
|
|
|
|
"a,a","bbb","ccc"
|
|
|
|
zzz,yyy,xxx`,
|
|
|
|
UseFieldsPerRecord: true,
|
|
|
|
FieldsPerRecord: 0,
|
|
|
|
Output: [
|
|
|
|
["#field1", "field2", "field3"],
|
|
|
|
["aaa", "bbb", "ccc"],
|
|
|
|
["a,a", `bbb`, "ccc"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["zzz", "yyy", "xxx"],
|
2020-03-15 08:03:25 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "NoEOLTest",
|
|
|
|
Input: "a,b,c",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["a", "b", "c"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Semicolon",
|
|
|
|
Input: "a;b;c\n",
|
|
|
|
Output: [["a", "b", "c"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
Comma: ";",
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "MultiLine",
|
|
|
|
Input: `"two
|
|
|
|
line","one line","three
|
|
|
|
line
|
|
|
|
field"`,
|
|
|
|
Output: [["two\nline"], ["one line"], ["three\nline\nfield"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "BlankLine",
|
|
|
|
Input: "a,b,c\n\nd,e,f\n\n",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b", "c"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["d", "e", "f"],
|
|
|
|
],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BlankLineFieldCount",
|
|
|
|
Input: "a,b,c\n\nd,e,f\n\n",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b", "c"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["d", "e", "f"],
|
2019-11-13 13:42:34 -05:00
|
|
|
],
|
2019-05-24 09:33:42 -04:00
|
|
|
UseFieldsPerRecord: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
FieldsPerRecord: 0,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrimSpace",
|
|
|
|
Input: " a, b, c\n",
|
|
|
|
Output: [["a", "b", "c"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
TrimLeadingSpace: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "LeadingSpace",
|
|
|
|
Input: " a, b, c\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [[" a", " b", " c"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Comment",
|
|
|
|
Input: "#1,2,3\na,b,c\n#comment",
|
|
|
|
Output: [["a", "b", "c"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
Comment: "#",
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "NoComment",
|
|
|
|
Input: "#1,2,3\na,b,c",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["#1", "2", "3"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["a", "b", "c"],
|
|
|
|
],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "LazyQuotes",
|
|
|
|
Input: `a "word","1"2",a","b`,
|
|
|
|
Output: [[`a "word"`, `1"2`, `a"`, `b`]],
|
2020-03-28 13:03:49 -04:00
|
|
|
LazyQuotes: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BareQuotes",
|
|
|
|
Input: `a "word","1"2",a"`,
|
|
|
|
Output: [[`a "word"`, `1"2`, `a"`]],
|
2020-03-28 13:03:49 -04:00
|
|
|
LazyQuotes: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BareDoubleQuotes",
|
|
|
|
Input: `a""b,c`,
|
|
|
|
Output: [[`a""b`, `c`]],
|
2020-03-28 13:03:49 -04:00
|
|
|
LazyQuotes: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadDoubleQuotes",
|
|
|
|
Input: `a""b,c`,
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrBareQuote,
|
2019-05-24 09:33:42 -04:00
|
|
|
// Error: &ParseError{StartLine: 1, Line: 1, Column: 1, Err: ErrBareQuote},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrimQuote",
|
|
|
|
Input: ` "a"," b",c`,
|
|
|
|
Output: [["a", " b", "c"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
TrimLeadingSpace: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadBareQuote",
|
|
|
|
Input: `a "word","b"`,
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrBareQuote,
|
2019-06-19 00:22:01 -04:00
|
|
|
// &ParseError{StartLine: 1, Line: 1, Column: 2, Err: ErrBareQuote}
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadTrailingQuote",
|
|
|
|
Input: `"a word",b"`,
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrBareQuote,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ExtraneousQuote",
|
|
|
|
Input: `"a "word","b"`,
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrBareQuote,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadFieldCount",
|
|
|
|
Input: "a,b,c\nd,e",
|
|
|
|
Error: ErrFieldCount,
|
|
|
|
UseFieldsPerRecord: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
FieldsPerRecord: 0,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadFieldCount1",
|
|
|
|
Input: `a,b,c`,
|
2019-06-19 00:22:01 -04:00
|
|
|
// Error: &ParseError{StartLine: 1, Line: 1, Err: ErrFieldCount},
|
2019-05-24 09:33:42 -04:00
|
|
|
UseFieldsPerRecord: true,
|
|
|
|
FieldsPerRecord: 2,
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrFieldCount,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FieldCount",
|
|
|
|
Input: "a,b,c\nd,e",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b", "c"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["d", "e"],
|
|
|
|
],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrailingCommaEOF",
|
|
|
|
Input: "a,b,c,",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["a", "b", "c", ""]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrailingCommaEOL",
|
|
|
|
Input: "a,b,c,\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["a", "b", "c", ""]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrailingCommaSpaceEOF",
|
|
|
|
Input: "a,b,c, ",
|
|
|
|
Output: [["a", "b", "c", ""]],
|
2020-03-28 13:03:49 -04:00
|
|
|
TrimLeadingSpace: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrailingCommaSpaceEOL",
|
|
|
|
Input: "a,b,c, \n",
|
|
|
|
Output: [["a", "b", "c", ""]],
|
2020-03-28 13:03:49 -04:00
|
|
|
TrimLeadingSpace: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrailingCommaLine3",
|
|
|
|
Input: "a,b,c\nd,e,f\ng,hi,",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b", "c"],
|
|
|
|
["d", "e", "f"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["g", "hi", ""],
|
2019-11-13 13:42:34 -05:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
TrimLeadingSpace: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "NotTrailingComma3",
|
|
|
|
Input: "a,b,c, \n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["a", "b", "c", " "]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "CommaFieldTest",
|
|
|
|
Input: `x,y,z,w
|
|
|
|
x,y,z,
|
|
|
|
x,y,,
|
|
|
|
x,,,
|
|
|
|
,,,
|
|
|
|
"x","y","z","w"
|
|
|
|
"x","y","z",""
|
|
|
|
"x","y","",""
|
|
|
|
"x","","",""
|
|
|
|
"","","",""
|
|
|
|
`,
|
|
|
|
Output: [
|
|
|
|
["x", "y", "z", "w"],
|
|
|
|
["x", "y", "z", ""],
|
|
|
|
["x", "y", "", ""],
|
|
|
|
["x", "", "", ""],
|
|
|
|
["", "", "", ""],
|
|
|
|
["x", "y", "z", "w"],
|
|
|
|
["x", "y", "z", ""],
|
|
|
|
["x", "y", "", ""],
|
|
|
|
["x", "", "", ""],
|
2020-03-28 13:03:49 -04:00
|
|
|
["", "", "", ""],
|
|
|
|
],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TrailingCommaIneffective1",
|
|
|
|
Input: "a,b,\nc,d,e",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b", ""],
|
2020-03-28 13:03:49 -04:00
|
|
|
["c", "d", "e"],
|
2019-11-13 13:42:34 -05:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
TrimLeadingSpace: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ReadAllReuseRecord",
|
|
|
|
Input: "a,b\nc,d",
|
2019-11-13 13:42:34 -05:00
|
|
|
Output: [
|
|
|
|
["a", "b"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["c", "d"],
|
2019-11-13 13:42:34 -05:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
ReuseRecord: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "StartLine1", // Issue 19019
|
|
|
|
Input: 'a,"b\nc"d,e',
|
|
|
|
Error: true,
|
|
|
|
// Error: &ParseError{StartLine: 1, Line: 2, Column: 1, Err: ErrQuote},
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "StartLine2",
|
|
|
|
Input: 'a,b\n"d\n\n,e',
|
|
|
|
Error: true,
|
|
|
|
// Error: &ParseError{StartLine: 2, Line: 5, Column: 0, Err: ErrQuote},
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "CRLFInQuotedField", // Issue 21201
|
|
|
|
Input: 'A,"Hello\r\nHi",B\r\n',
|
|
|
|
Output: [["A", "Hello\nHi", "B"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "BinaryBlobField", // Issue 19410
|
|
|
|
Input: "x09\x41\xb4\x1c,aktau",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["x09A\xb4\x1c", "aktau"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "TrailingCR",
|
|
|
|
Input: "field1,field2\r",
|
|
|
|
Output: [["field1", "field2"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "QuotedTrailingCR",
|
|
|
|
Input: '"field"\r',
|
|
|
|
Output: [['"field"']],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "QuotedTrailingCRCR",
|
|
|
|
Input: '"field"\r\r',
|
|
|
|
Error: true,
|
|
|
|
// Error: &ParseError{StartLine: 1, Line: 1, Column: 6, Err: ErrQuote},
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FieldCR",
|
|
|
|
Input: "field\rfield\r",
|
|
|
|
Output: [["field\rfield"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FieldCRCR",
|
|
|
|
Input: "field\r\rfield\r\r",
|
|
|
|
Output: [["field\r\rfield\r"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "FieldCRCRLF",
|
|
|
|
Input: "field\r\r\nfield\r\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["field\r"], ["field\r"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "FieldCRCRLFCR",
|
|
|
|
Input: "field\r\r\n\rfield\r\r\n\r",
|
2020-03-28 13:03:49 -04:00
|
|
|
Output: [["field\r"], ["\rfield\r"]],
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "FieldCRCRLFCRCR",
|
|
|
|
Input: "field\r\r\n\r\rfield\r\r\n\r\r",
|
|
|
|
Output: [["field\r"], ["\r\rfield\r"], ["\r"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "MultiFieldCRCRLFCRCR",
|
|
|
|
Input: "field1,field2\r\r\n\r\rfield1,field2\r\r\n\r\r,",
|
|
|
|
Output: [
|
|
|
|
["field1", "field2\r"],
|
|
|
|
["\r\rfield1", "field2\r"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["\r\r", ""],
|
2020-03-15 08:03:25 -04:00
|
|
|
],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "NonASCIICommaAndComment",
|
|
|
|
Input: "a£b,c£ \td,e\n€ comment\n",
|
|
|
|
Output: [["a", "b,c", "d,e"]],
|
|
|
|
TrimLeadingSpace: true,
|
|
|
|
Comma: "£",
|
2020-03-28 13:03:49 -04:00
|
|
|
Comment: "€",
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "NonASCIICommaAndCommentWithQuotes",
|
|
|
|
Input: 'a€" b,"€ c\nλ comment\n',
|
|
|
|
Output: [["a", " b,", " c"]],
|
|
|
|
Comma: "€",
|
2020-03-28 13:03:49 -04:00
|
|
|
Comment: "λ",
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
// λ and θ start with the same byte.
|
|
|
|
// This tests that the parser doesn't confuse such characters.
|
|
|
|
Name: "NonASCIICommaConfusion",
|
|
|
|
Input: '"abθcd"λefθgh',
|
|
|
|
Output: [["abθcd", "efθgh"]],
|
|
|
|
Comma: "λ",
|
2020-03-28 13:03:49 -04:00
|
|
|
Comment: "€",
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "NonASCIICommentConfusion",
|
|
|
|
Input: "λ\nλ\nθ\nλ\n",
|
|
|
|
Output: [["λ"], ["λ"], ["λ"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
Comment: "θ",
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "QuotedFieldMultipleLF",
|
|
|
|
Input: '"\n\n\n\n"',
|
|
|
|
Output: [["\n\n\n\n"]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "MultipleCRLF",
|
|
|
|
Input: "\r\n\r\n\r\n\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-06-19 00:22:01 -04:00
|
|
|
/**
|
|
|
|
* The implementation may read each line in several chunks if
|
|
|
|
* it doesn't fit entirely.
|
|
|
|
* in the read buffer, so we should test the code to handle that condition.
|
|
|
|
*/
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "HugeLines",
|
|
|
|
Input:
|
|
|
|
"#ignore\n".repeat(10000) + "@".repeat(5000) + "," + "*".repeat(5000),
|
|
|
|
Output: [["@".repeat(5000), "*".repeat(5000)]],
|
|
|
|
Comment: "#",
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "QuoteWithTrailingCRLF",
|
|
|
|
Input: '"foo"bar"\r\n',
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrBareQuote,
|
2019-05-24 09:33:42 -04:00
|
|
|
// Error: &ParseError{StartLine: 1, Line: 1, Column: 4, Err: ErrQuote},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "LazyQuoteWithTrailingCRLF",
|
|
|
|
Input: '"foo"bar"\r\n',
|
|
|
|
Output: [[`foo"bar`]],
|
2020-03-28 13:03:49 -04:00
|
|
|
LazyQuotes: true,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
2020-03-15 08:03:25 -04:00
|
|
|
{
|
|
|
|
Name: "DoubleQuoteWithTrailingCRLF",
|
|
|
|
Input: '"foo""bar"\r\n',
|
|
|
|
Output: [[`foo"bar`]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "EvenQuotes",
|
|
|
|
Input: `""""""""`,
|
|
|
|
Output: [[`"""`]],
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "OddQuotes",
|
|
|
|
Input: `"""""""`,
|
|
|
|
Error: true,
|
|
|
|
// Error:" &ParseError{StartLine: 1, Line: 1, Column: 7, Err: ErrQuote}",
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "LazyOddQuotes",
|
|
|
|
Input: `"""""""`,
|
|
|
|
Output: [[`"""`]],
|
|
|
|
LazyQuotes: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
ignore: true,
|
2020-03-15 08:03:25 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
{
|
|
|
|
Name: "BadComma1",
|
|
|
|
Comma: "\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrInvalidDelim,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadComma2",
|
|
|
|
Comma: "\r",
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrInvalidDelim,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadComma3",
|
|
|
|
Comma: '"',
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrInvalidDelim,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadComment1",
|
|
|
|
Comment: "\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrInvalidDelim,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadComment2",
|
|
|
|
Comment: "\r",
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrInvalidDelim,
|
2019-05-24 09:33:42 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "BadCommaComment",
|
|
|
|
Comma: "X",
|
|
|
|
Comment: "X",
|
2020-03-28 13:03:49 -04:00
|
|
|
Error: ErrInvalidDelim,
|
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
];
|
|
|
|
for (const t of testCases) {
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test({
|
2020-03-19 05:58:12 -04:00
|
|
|
ignore: !!t.ignore,
|
2019-05-24 09:33:42 -04:00
|
|
|
name: `[CSV] ${t.Name}`,
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
let comma = ",";
|
|
|
|
let comment;
|
|
|
|
let fieldsPerRec;
|
|
|
|
let trim = false;
|
|
|
|
let lazyquote = false;
|
|
|
|
if (t.Comma) {
|
|
|
|
comma = t.Comma;
|
|
|
|
}
|
|
|
|
if (t.Comment) {
|
|
|
|
comment = t.Comment;
|
|
|
|
}
|
|
|
|
if (t.TrimLeadingSpace) {
|
|
|
|
trim = true;
|
|
|
|
}
|
|
|
|
if (t.UseFieldsPerRecord) {
|
|
|
|
fieldsPerRec = t.FieldsPerRecord;
|
|
|
|
}
|
|
|
|
if (t.LazyQuotes) {
|
|
|
|
lazyquote = t.LazyQuotes;
|
|
|
|
}
|
2019-05-23 22:04:06 -04:00
|
|
|
let actual;
|
2019-05-24 09:33:42 -04:00
|
|
|
if (t.Error) {
|
2019-05-23 22:04:06 -04:00
|
|
|
let err;
|
|
|
|
try {
|
2020-02-19 15:36:18 -05:00
|
|
|
actual = await readMatrix(
|
|
|
|
new BufReader(new StringReader(t.Input ?? "")),
|
|
|
|
{
|
|
|
|
comma: comma,
|
|
|
|
comment: comment,
|
|
|
|
trimLeadingSpace: trim,
|
|
|
|
fieldsPerRecord: fieldsPerRec,
|
2020-03-28 13:03:49 -04:00
|
|
|
lazyQuotes: lazyquote,
|
2020-02-19 15:36:18 -05:00
|
|
|
}
|
|
|
|
);
|
2019-05-23 22:04:06 -04:00
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(err);
|
|
|
|
assertEquals(err.message, t.Error);
|
2019-05-24 09:33:42 -04:00
|
|
|
} else {
|
2020-02-19 15:36:18 -05:00
|
|
|
actual = await readMatrix(
|
|
|
|
new BufReader(new StringReader(t.Input ?? "")),
|
|
|
|
{
|
|
|
|
comma: comma,
|
|
|
|
comment: comment,
|
|
|
|
trimLeadingSpace: trim,
|
|
|
|
fieldsPerRecord: fieldsPerRec,
|
2020-03-28 13:03:49 -04:00
|
|
|
lazyQuotes: lazyquote,
|
2020-02-19 15:36:18 -05:00
|
|
|
}
|
|
|
|
);
|
2019-05-23 22:04:06 -04:00
|
|
|
const expected = t.Output;
|
2019-05-24 09:33:42 -04:00
|
|
|
assertEquals(actual, expected);
|
|
|
|
}
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-24 09:33:42 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-30 09:50:29 -04:00
|
|
|
const parseTestCases = [
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
in: "a,b,c",
|
|
|
|
header: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
result: [["a", "b", "c"]],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple Bufreader",
|
|
|
|
in: new BufReader(new StringReader("a,b,c")),
|
|
|
|
header: false,
|
2020-03-28 13:03:49 -04:00
|
|
|
result: [["a", "b", "c"]],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiline",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
header: false,
|
2019-11-13 13:42:34 -05:00
|
|
|
result: [
|
|
|
|
["a", "b", "c"],
|
2020-03-28 13:03:49 -04:00
|
|
|
["e", "f", "g"],
|
|
|
|
],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping boolean",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
header: true,
|
2020-03-28 13:03:49 -04:00
|
|
|
result: [{ a: "e", b: "f", c: "g" }],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping array",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
header: ["this", "is", "sparta"],
|
|
|
|
result: [
|
|
|
|
{ this: "a", is: "b", sparta: "c" },
|
2020-03-28 13:03:49 -04:00
|
|
|
{ this: "e", is: "f", sparta: "g" },
|
|
|
|
],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping object",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
header: [{ name: "this" }, { name: "is" }, { name: "sparta" }],
|
|
|
|
result: [
|
|
|
|
{ this: "a", is: "b", sparta: "c" },
|
2020-03-28 13:03:49 -04:00
|
|
|
{ this: "e", is: "f", sparta: "g" },
|
|
|
|
],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping parse entry",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
header: [
|
|
|
|
{
|
|
|
|
name: "this",
|
|
|
|
parse: (e: string): string => {
|
|
|
|
return `b${e}$$`;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "is",
|
|
|
|
parse: (e: string): number => {
|
|
|
|
return e.length;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sparta",
|
|
|
|
parse: (e: string): unknown => {
|
|
|
|
return { bim: `boom-${e}` };
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
|
|
|
},
|
2019-05-30 09:50:29 -04:00
|
|
|
],
|
|
|
|
result: [
|
|
|
|
{ this: "ba$$", is: 1, sparta: { bim: `boom-c` } },
|
2020-03-28 13:03:49 -04:00
|
|
|
{ this: "be$$", is: 1, sparta: { bim: `boom-g` } },
|
|
|
|
],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiline parse",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
parse: (e: string[]): unknown => {
|
|
|
|
return { super: e[0], street: e[1], fighter: e[2] };
|
|
|
|
},
|
|
|
|
header: false,
|
|
|
|
result: [
|
|
|
|
{ super: "a", street: "b", fighter: "c" },
|
2020-03-28 13:03:49 -04:00
|
|
|
{ super: "e", street: "f", fighter: "g" },
|
|
|
|
],
|
2019-05-30 09:50:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "header mapping object parseline",
|
|
|
|
in: "a,b,c\ne,f,g\n",
|
|
|
|
header: [{ name: "this" }, { name: "is" }, { name: "sparta" }],
|
|
|
|
parse: (e: Record<string, unknown>): unknown => {
|
|
|
|
return { super: e.this, street: e.is, fighter: e.sparta };
|
|
|
|
},
|
|
|
|
result: [
|
|
|
|
{ super: "a", street: "b", fighter: "c" },
|
2020-03-28 13:03:49 -04:00
|
|
|
{ super: "e", street: "f", fighter: "g" },
|
|
|
|
],
|
|
|
|
},
|
2019-05-30 09:50:29 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const testCase of parseTestCases) {
|
2020-02-11 11:24:27 -05:00
|
|
|
Deno.test({
|
2019-05-30 09:50:29 -04:00
|
|
|
name: `[CSV] Parse ${testCase.name}`,
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const r = await parse(testCase.in, {
|
|
|
|
header: testCase.header,
|
2020-03-28 13:03:49 -04:00
|
|
|
parse: testCase.parse as (input: unknown) => unknown,
|
2019-05-30 09:50:29 -04:00
|
|
|
});
|
|
|
|
assertEquals(r, testCase.result);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-05-30 09:50:29 -04:00
|
|
|
});
|
|
|
|
}
|