Browse Source

Adding test cases for keepEmptyRows

pull/72/merge
Matthew Holt 11 years ago
parent
commit
bfdf5a1828
  1. 135
      tests/test-cases.js

135
tests/test-cases.js

@ -5,40 +5,40 @@ var UNIT_SEP = String.fromCharCode(31);
// Tests for Papa.parse() function (CSV to JSON) // Tests for Papa.parse() function (CSV to JSON)
var PARSE_TESTS = [ var PARSE_TESTS = [
{ {
input: 'A,b,c',
description: "One row", description: "One row",
input: 'A,b,c',
expected: { expected: {
data: [['A', 'b', 'c']], data: [['A', 'b', 'c']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,b,c\r\nd,E,f',
description: "Two rows", description: "Two rows",
input: 'A,b,c\r\nd,E,f',
expected: { expected: {
data: [['A', 'b', 'c'], ['d', 'E', 'f']], data: [['A', 'b', 'c'], ['d', 'E', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,b,c\rd,E,f',
description: "Two rows, just \\r", description: "Two rows, just \\r",
input: 'A,b,c\rd,E,f',
expected: { expected: {
data: [['A', 'b', 'c'], ['d', 'E', 'f']], data: [['A', 'b', 'c'], ['d', 'E', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,b,c\nd,E,f',
description: "Two rows, just \\n", description: "Two rows, just \\n",
input: 'A,b,c\nd,E,f',
expected: { expected: {
data: [['A', 'b', 'c'], ['d', 'E', 'f']], data: [['A', 'b', 'c'], ['d', 'E', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a, b ,c',
description: "Whitespace at edges of unquoted field", description: "Whitespace at edges of unquoted field",
input: 'a, b ,c',
notes: "Extra whitespace should graciously be preserved", notes: "Extra whitespace should graciously be preserved",
expected: { expected: {
data: [['a', ' b ', 'c']], data: [['a', ' b ', 'c']],
@ -46,72 +46,72 @@ var PARSE_TESTS = [
} }
}, },
{ {
input: 'A,"B",C',
description: "Quoted field", description: "Quoted field",
input: 'A,"B",C',
expected: { expected: {
data: [['A', 'B', 'C']], data: [['A', 'B', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A," B ",C',
description: "Quoted field with extra whitespace on edges", description: "Quoted field with extra whitespace on edges",
input: 'A," B ",C',
expected: { expected: {
data: [['A', ' B ', 'C']], data: [['A', ' B ', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,"B,B",C',
description: "Quoted field with delimiter", description: "Quoted field with delimiter",
input: 'A,"B,B",C',
expected: { expected: {
data: [['A', 'B,B', 'C']], data: [['A', 'B,B', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,"B\r\nB",C',
description: "Quoted field with \\r\\n", description: "Quoted field with \\r\\n",
input: 'A,"B\r\nB",C',
expected: { expected: {
data: [['A', 'B\r\nB', 'C']], data: [['A', 'B\r\nB', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,"B\rB",C',
description: "Quoted field with \\r", description: "Quoted field with \\r",
input: 'A,"B\rB",C',
expected: { expected: {
data: [['A', 'B\rB', 'C']], data: [['A', 'B\rB', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,"B\nB",C',
description: "Quoted field with \\n", description: "Quoted field with \\n",
input: 'A,"B\nB",C',
expected: { expected: {
data: [['A', 'B\nB', 'C']], data: [['A', 'B\nB', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,"B""B""B",C',
description: "Quoted field with escaped quotes", description: "Quoted field with escaped quotes",
input: 'A,"B""B""B",C',
expected: { expected: {
data: [['A', 'B"B"B', 'C']], data: [['A', 'B"B"B', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,"""B""",C',
description: "Quoted field with escaped quotes at boundaries", description: "Quoted field with escaped quotes at boundaries",
input: 'A,"""B""",C',
expected: { expected: {
data: [['A', '"B"', 'C']], data: [['A', '"B"', 'C']],
errors: [] errors: []
} }
}, },
{ {
input: 'A,""",""",C',
description: "Quoted field with quotes around delimiter", description: "Quoted field with quotes around delimiter",
input: 'A,""",""",C',
notes: "For a boundary to exist immediately before the quotes, we must not already be in quotes", notes: "For a boundary to exist immediately before the quotes, we must not already be in quotes",
expected: { expected: {
data: [['A', '","', 'C']], data: [['A', '","', 'C']],
@ -119,8 +119,8 @@ var PARSE_TESTS = [
} }
}, },
{ {
input: 'A,",""",C',
description: "Quoted field with quotes on one side of delimiter", description: "Quoted field with quotes on one side of delimiter",
input: 'A,",""",C',
notes: "Similar to the test above but with quotes only after the delimiter", notes: "Similar to the test above but with quotes only after the delimiter",
expected: { expected: {
data: [['A', ',"', 'C']], data: [['A', ',"', 'C']],
@ -128,8 +128,8 @@ var PARSE_TESTS = [
} }
}, },
{ {
input: 'A, "B" ,C',
description: "Quoted field with whitespace around quotes", description: "Quoted field with whitespace around quotes",
input: 'A, "B" ,C',
notes: "This is malformed input, but it should be parsed gracefully (with errors)", notes: "This is malformed input, but it should be parsed gracefully (with errors)",
expected: { expected: {
data: [['A', ' "B" ', 'C']], data: [['A', ' "B" ', 'C']],
@ -140,45 +140,45 @@ var PARSE_TESTS = [
} }
}, },
{ {
description: "Tab delimiter",
input: 'a\tb\tc\r\nd\te\tf', input: 'a\tb\tc\r\nd\te\tf',
config: { delimiter: "\t" }, config: { delimiter: "\t" },
description: "Tab delimiter",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "Pipe delimiter",
input: 'a|b|c\r\nd|e|f', input: 'a|b|c\r\nd|e|f',
config: { delimiter: "|" }, config: { delimiter: "|" },
description: "Pipe delimiter",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "ASCII 30 delimiter",
input: 'a'+RECORD_SEP+'b'+RECORD_SEP+'c\r\nd'+RECORD_SEP+'e'+RECORD_SEP+'f', input: 'a'+RECORD_SEP+'b'+RECORD_SEP+'c\r\nd'+RECORD_SEP+'e'+RECORD_SEP+'f',
config: { delimiter: RECORD_SEP }, config: { delimiter: RECORD_SEP },
description: "ASCII 30 delimiter",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "ASCII 31 delimiter",
input: 'a'+UNIT_SEP+'b'+UNIT_SEP+'c\r\nd'+UNIT_SEP+'e'+UNIT_SEP+'f', input: 'a'+UNIT_SEP+'b'+UNIT_SEP+'c\r\nd'+UNIT_SEP+'e'+UNIT_SEP+'f',
config: { delimiter: UNIT_SEP }, config: { delimiter: UNIT_SEP },
description: "ASCII 31 delimiter",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "Bad delimiter",
input: 'a,b,c', input: 'a,b,c',
config: { delimiter: "DELIM" }, config: { delimiter: "DELIM" },
description: "Bad delimiter",
notes: "Should silently default to comma", notes: "Should silently default to comma",
expected: { expected: {
data: [['a', 'b', 'c']], data: [['a', 'b', 'c']],
@ -186,45 +186,45 @@ var PARSE_TESTS = [
} }
}, },
{ {
description: "Commented line at beginning (comments: true)",
input: '# Comment!\r\na,b,c', input: '# Comment!\r\na,b,c',
config: { comments: true }, config: { comments: true },
description: "Commented line at beginning (comments: true)",
expected: { expected: {
data: [['a', 'b', 'c']], data: [['a', 'b', 'c']],
errors: [] errors: []
} }
}, },
{ {
description: "Commented line in middle (comments: true)",
input: 'a,b,c\r\n# Comment\r\nd,e,f', input: 'a,b,c\r\n# Comment\r\nd,e,f',
config: { comments: true }, config: { comments: true },
description: "Commented line in middle (comments: true)",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "Commented line at end (comments: true)",
input: 'a,b,c\r\n# Comment', input: 'a,b,c\r\n# Comment',
config: { comments: true }, config: { comments: true },
description: "Commented line at end (comments: true)",
expected: { expected: {
data: [['a', 'b', 'c']], data: [['a', 'b', 'c']],
errors: [] errors: []
} }
}, },
{ {
description: "Comment with non-default character (comments: '!')",
input: 'a,b,c\r\n!Comment goes here\r\nd,e,f', input: 'a,b,c\r\n!Comment goes here\r\nd,e,f',
config: { comments: '!' }, config: { comments: '!' },
description: "Comment with non-default character (comments: '!')",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "Comment, but bad char specified (comments: \"=N(\")",
input: 'a,b,c\r\n=N(Comment)\r\nd,e,f', input: 'a,b,c\r\n=N(Comment)\r\nd,e,f',
config: { comments: '=N(' }, config: { comments: '=N(' },
description: "Comment, but bad char specified (comments: \"=N(\")",
notes: "Should silently disable comment parsing", notes: "Should silently disable comment parsing",
expected: { expected: {
data: [['a', 'b', 'c'], ['=N(Comment)'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['=N(Comment)'], ['d', 'e', 'f']],
@ -232,27 +232,27 @@ var PARSE_TESTS = [
} }
}, },
{ {
description: "Input with only a commented line (comments: true)",
input: '#commented line\r\n', input: '#commented line\r\n',
config: { comments: true, delimiter: ',' }, config: { comments: true, delimiter: ',' },
description: "Input with only a commented line (comments: true)",
expected: { expected: {
data: [], data: [],
errors: [] errors: []
} }
}, },
{ {
description: "Input with comment without comments enabled",
input: '#commented line', input: '#commented line',
config: { delimiter: ',' }, config: { delimiter: ',' },
description: "Input with comment without comments enabled",
expected: { expected: {
data: [['#commented line']], data: [['#commented line']],
errors: [] errors: []
} }
}, },
{ {
description: "Input without comments with line starting with whitespace",
input: 'a\r\n b\r\nc', input: 'a\r\n b\r\nc',
config: { delimiter: ',' }, config: { delimiter: ',' },
description: "Input without comments with line starting with whitespace",
notes: "\" \" == false, but \" \" !== false, so === comparison is required", notes: "\" \" == false, but \" \" !== false, so === comparison is required",
expected: { expected: {
data: [['a'], [' b'], ['c']], data: [['a'], [' b'], ['c']],
@ -260,9 +260,9 @@ var PARSE_TESTS = [
} }
}, },
{ {
description: "Comment char same as delimiter",
input: 'a#b#c\r\n# Comment', input: 'a#b#c\r\n# Comment',
config: { delimiter: '#', comments: '#' }, config: { delimiter: '#', comments: '#' },
description: "Comment char same as delimiter",
notes: "Comment parsing should automatically be silently disabled in this case", notes: "Comment parsing should automatically be silently disabled in this case",
expected: { expected: {
data: [['a', 'b', 'c'], ['', ' Comment']], data: [['a', 'b', 'c'], ['', ' Comment']],
@ -270,64 +270,64 @@ var PARSE_TESTS = [
} }
}, },
{ {
input: '\r\na,b,c\r\nd,e,f',
description: "Blank line at beginning", description: "Blank line at beginning",
input: '\r\na,b,c\r\nd,e,f',
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a,b,c\r\n\r\nd,e,f',
description: "Blank line in middle", description: "Blank line in middle",
input: 'a,b,c\r\n\r\nd,e,f',
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a,b,c\r\nd,e,f\r\n\r\n',
description: "Blank lines at end", description: "Blank lines at end",
input: 'a,b,c\r\nd,e,f\r\n\r\n',
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a,b,c\r\n \r\nd,e,f',
description: "Blank line in middle with whitespace", description: "Blank line in middle with whitespace",
input: 'a,b,c\r\n \r\nd,e,f',
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a,b,c\r\n,e,f',
description: "First field of a line is empty", description: "First field of a line is empty",
input: 'a,b,c\r\n,e,f',
expected: { expected: {
data: [['a', 'b', 'c'], ['', 'e', 'f']], data: [['a', 'b', 'c'], ['', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a,b,\r\nd,e,f',
description: "Last field of a line is empty", description: "Last field of a line is empty",
input: 'a,b,\r\nd,e,f',
expected: { expected: {
data: [['a', 'b', ''], ['d', 'e', 'f']], data: [['a', 'b', ''], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
input: 'a,,c\r\n,,',
description: "Other fields are empty", description: "Other fields are empty",
input: 'a,,c\r\n,,',
expected: { expected: {
data: [['a', '', 'c'], ['', '', '']], data: [['a', '', 'c'], ['', '', '']],
errors: [] errors: []
} }
}, },
{ {
input: '',
description: "Empty input string", description: "Empty input string",
input: '',
expected: { expected: {
data: [], data: [],
errors: [{ errors: [{
@ -338,16 +338,16 @@ var PARSE_TESTS = [
} }
}, },
{ {
input: ',',
description: "Input is just the delimiter (2 empty fields)", description: "Input is just the delimiter (2 empty fields)",
input: ',',
expected: { expected: {
data: [['', '']], data: [['', '']],
errors: [] errors: []
} }
}, },
{ {
input: 'Abc def',
description: "Input is just a string (a single field)", description: "Input is just a string (a single field)",
input: 'Abc def',
expected: { expected: {
data: [['Abc def']], data: [['Abc def']],
errors: [ errors: [
@ -360,58 +360,101 @@ var PARSE_TESTS = [
} }
}, },
{ {
description: "Preview 0 rows should default to parsing all",
input: 'a,b,c\r\nd,e,f\r\ng,h,i', input: 'a,b,c\r\nd,e,f\r\ng,h,i',
config: { preview: 0 }, config: { preview: 0 },
description: "Preview 0 rows should default to parsing all",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
errors: [] errors: []
} }
}, },
{ {
description: "Preview 1 row",
input: 'a,b,c\r\nd,e,f\r\ng,h,i', input: 'a,b,c\r\nd,e,f\r\ng,h,i',
config: { preview: 1 }, config: { preview: 1 },
description: "Preview 1 row",
expected: { expected: {
data: [['a', 'b', 'c']], data: [['a', 'b', 'c']],
errors: [] errors: []
} }
}, },
{ {
description: "Preview 2 rows",
input: 'a,b,c\r\nd,e,f\r\ng,h,i', input: 'a,b,c\r\nd,e,f\r\ng,h,i',
config: { preview: 2 }, config: { preview: 2 },
description: "Preview 2 rows",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f']], data: [['a', 'b', 'c'], ['d', 'e', 'f']],
errors: [] errors: []
} }
}, },
{ {
description: "Preview all (3) rows",
input: 'a,b,c\r\nd,e,f\r\ng,h,i', input: 'a,b,c\r\nd,e,f\r\ng,h,i',
config: { preview: 3 }, config: { preview: 3 },
description: "Preview all (3) rows",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
errors: [] errors: []
} }
}, },
{ {
description: "Preview more rows than input has",
input: 'a,b,c\r\nd,e,f\r\ng,h,i', input: 'a,b,c\r\nd,e,f\r\ng,h,i',
config: { preview: 4 }, config: { preview: 4 },
description: "Preview more rows than input has",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], data: [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']],
errors: [] errors: []
} }
}, },
{ {
description: "Preview should count rows, not lines",
input: 'a,b,c\r\nd,e,"f\r\nf",g,h,i', input: 'a,b,c\r\nd,e,"f\r\nf",g,h,i',
config: { preview: 2 }, config: { preview: 2 },
description: "Preview should count rows, not lines",
expected: { expected: {
data: [['a', 'b', 'c'], ['d', 'e', 'f\r\nf', 'g', 'h', 'i']], data: [['a', 'b', 'c'], ['d', 'e', 'f\r\nf', 'g', 'h', 'i']],
errors: [] errors: []
} }
},
{
description: "Keep empty rows",
input: 'a,b,c\r\n\r\nd,e,f',
config: { keepEmptyRows: true },
expected: {
data: [['a', 'b', 'c'], [], ['d', 'e', 'f']],
errors: []
}
},
{
description: "Keep empty rows, with newline at end of input",
input: 'a,b,c\r\n\r\nd,e,f\r\n',
config: { keepEmptyRows: true },
expected: {
data: [['a', 'b', 'c'], [], ['d', 'e', 'f'], []],
errors: []
}
},
{
description: "Keep empty rows, with empty input",
input: '',
config: { keepEmptyRows: true },
expected: {
data: [[]],
errors: [
{
"type": "Delimiter",
"code": "UndetectableDelimiter",
"message": "Unable to auto-detect delimiting character; defaulted to comma"
}
]
}
},
{
description: "Keep empty rows, with first line only whitespace empty",
notes: "Even with keepEmptyRows enabled, rows with just a single field,<br>being whitespace, should be stripped of that field",
input: ' \r\na,b,c',
config: { keepEmptyRows: true },
expected: {
data: [[], ['a', 'b', 'c']],
errors: []
}
} }
]; ];

Loading…
Cancel
Save