Skip to content

Commit 5cf9170

Browse files
committed
Add tests for parser.splitLines
1 parent e163e96 commit 5cf9170

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

test/fixtures/defaults_fivelines.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"1","he said ""you are crazy""","one "", two "", three """
2+
"2","he said ""you are crazy""","one "", two "", three """
3+
"3","he said ""you are crazy""","one "", two "", three """
4+
"4","he said ""you are crazy""","one "", two "", three """
5+
"5","he said ""you are crazy""","one "", two "", three """

test/fixtures/delimiter_fivelines.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'2','he said ''you are crazy''','one '', two '', three '''
2+
'2','he said ''you are crazy''','one '', two '', three '''
3+
'2','he said ''you are crazy''','one '', two '', three '''
4+
'2','he said ''you are crazy''','one '', two '', three '''
5+
'2','he said ''you are crazy''','one '', two '', three '''

test/fixtures/fixtures.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ var fs = require('fs');
4141
newline_mac: csvFixture('newline_mac'),
4242
defaults_csv: csvFixture('defaults'),
4343
defaults_obj: jsonFixture('defaults'),
44+
defaults_fivelines_csv: csvFixture('defaults_fivelines'),
4445
delimiter_csv: csvFixture('delimiter'),
4546
delimiter_obj: jsonFixture('delimiter'),
47+
delimiter_fivelines_csv: csvFixture('delimiter_fivelines'),
4648
separator_csv: csvFixture('separator'),
4749
separator_obj: jsonFixture('separator'),
50+
separator_fivelines_csv: csvFixture('separator_fivelines'),
4851
regex_csv: csvFixture('regex'),
4952
regex_obj: jsonFixture('regex'),
5053
term_arrays_csv: csvFixture('term_arrays'),

test/fixtures/separator_fivelines.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"3";"he said ""you are crazy""";"one "", two "", three """
2+
"3";"he said ""you are crazy""";"one "", two "", three """
3+
"3";"he said ""you are crazy""";"one "", two "", three """
4+
"3";"he said ""you are crazy""";"one "", two "", three """
5+
"3";"he said ""you are crazy""";"one "", two "", three """

test/parser.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* no-unused-vars: 2 */
2+
3+
var assert = require('chai').assert;
4+
5+
var csv = require('../src/jquery.csv.js');
6+
var fixtures = require('./fixtures/fixtures.js');
7+
8+
describe('splitLines', function () {
9+
it('should correctly split lines with default options', function () {
10+
var out = csv.parsers.splitLines(fixtures.defaults_fivelines_csv);
11+
assert(typeof out === 'object' && Array.isArray(out), 'the returned object should be an array');
12+
assert.strictEqual(out.length, 5, 'the returned array should contain 5 lines');
13+
});
14+
15+
it('should correctly split lines with custom separator', function () {
16+
var options = {
17+
separator: ';',
18+
};
19+
20+
var out = csv.parsers.splitLines(fixtures.separator_fivelines_csv, options);
21+
assert(typeof out === 'object' && Array.isArray(out), 'the returned object should be an array');
22+
assert.strictEqual(out.length, 5, 'the returned array should contain 5 lines');
23+
});
24+
25+
it('should throw an error for using the wrong separator', function () {
26+
var options = {
27+
separator: ';'
28+
};
29+
function splitLines() {
30+
csv.parsers.splitLines(fixtures.defaults_fivelines_csv, options);
31+
}
32+
33+
assert.throws(splitLines, Error);
34+
});
35+
36+
it('should correctly split lines with custom delimiter', function () {
37+
var options = {
38+
delimiter: "'",
39+
};
40+
41+
var out = csv.parsers.splitLines(fixtures.delimiter_fivelines_csv, options);
42+
assert(typeof out === 'object' && Array.isArray(out), 'the returned object should be an array');
43+
assert.strictEqual(out.length, 5, 'the returned array should contain 5 lines');
44+
});
45+
46+
it('should return undefined when csv is undefined', function () {
47+
var out = csv.parsers.splitLines();
48+
assert.notExists(out);
49+
});
50+
});

test/test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ var should = require('chai').should();
77
var csv = require('../src/jquery.csv.js');
88
var fixtures = require('./fixtures/fixtures.js');
99

10+
function importTest(name, path) {
11+
describe(name, function () {
12+
require(path);
13+
});
14+
}
15+
1016
describe('core:', function () {
1117
describe('toArray', function () {
1218
it('should be able to parse an entry containing multiple cells', function () {
@@ -153,3 +159,5 @@ describe('custom terminals (ie delimiter, separator', function () {
153159
// it('should start at a certain point when used on toArrays()', function() {
154160
// });
155161
// });
162+
163+
importTest('csv.parser tests:', './parser');

0 commit comments

Comments
 (0)