Skip to content

Commit

Permalink
Refactor Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evanplaice committed Feb 7, 2020
1 parent 9ce4797 commit 1a0cfbb
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 60 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CSV-ES is a universal JavaScript CSV parser designed specifically to be simple,
[![Latest Status](https://github.com/vanillaes/csv-es/workflows/Latest/badge.svg)](https://github.com/vanillaes/csv-es/actions)
[![Release Status](https://github.com/vanillaes/csv-es/workflows/Release/badge.svg)](https://github.com/vanillaes/csv-es/actions)

## Features

- RFC Compliant
- ECMAScript Module
Expand Down
12 changes: 6 additions & 6 deletions test/amendments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ const rfcA2 = require('./__test__/rfca2.json');
const rfcA3 = require('./__test__/rfca3.json');

test('RFC Amendment #1 - An unquoted field may contain a null (ie empty) value', (t) => {
const result = CSV.parse(rfcA1.csv.join('\n'));
const actual = CSV.parse(rfcA1.csv.join('\n'));
const expect = rfcA1.json;
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('RFC Amendment #2 - A quoted field may contain a null (ie empty) value', (t) => {
const result = CSV.parse(rfcA2.csv.join('\n'));
const actual = CSV.parse(rfcA2.csv.join('\n'));
const expect = rfcA2.json;
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('RFC Amendment #3 - The last field in an entry may contain a null (ie empty) value', (t) => {
const result = CSV.parse(rfcA3.csv.join('\n'));
const actual = CSV.parse(rfcA3.csv.join('\n'));
const expect = rfcA3.json;
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});
32 changes: 16 additions & 16 deletions test/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,72 @@ const eof2 = require('./__test__/eof2.json');

test('Reviver #1 - The reviver should append 1 to each value', (t) => {
const expect = reviver1.json;
const result = CSV.parse(reviver1.csv.join('\n'), null, (value) => value + '1');
const actual = CSV.parse(reviver1.csv.join('\n'), null, (value) => value + '1');

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('Reviver #2 - The reviver should output the row:col values', (t) => {
const expect = reviver2.json;
const result = CSV.parse(reviver2.csv.join('\n'), null, (value, row, col) => `${row}:${col}`);
const actual = CSV.parse(reviver2.csv.join('\n'), null, (value, row, col) => `${row}:${col}`);

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('Typed #1 - When set to true the parser should infer the value types', (t) => {
const expect = typed1.json;
const result = CSV.parse(typed1.csv.join('\n'), { typed: true });
const actual = CSV.parse(typed1.csv.join('\n'), { typed: true });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('Typed #2- When set to false the parser should not infer the value types', (t) => {
const expect = typed2.json;
const result = CSV.parse(typed2.csv.join('\n'), { typed: false });
const actual = CSV.parse(typed2.csv.join('\n'), { typed: false });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('Replacer #1 - The replacer should append 1 to each value', (t) => {
const expect = replacer1.csv.join('\n');
const result = CSV.stringify(replacer1.json, {}, (value) => value + '1');
const actual = CSV.stringify(replacer1.json, {}, (value) => value + '1');

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('Replacer #2 - The replacer should output the row:col values', (t) => {
const expect = replacer2.csv.join('\n');
const result = CSV.stringify(replacer2.json, {}, (value, row, col) => `${row}:${col}`);
const actual = CSV.stringify(replacer2.json, {}, (value, row, col) => `${row}:${col}`);

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('EOF #1 - When set to true the formatter should include a newline at the end of file', (t) => {
const expect = eof1.csv.join('\n');
const result = CSV.stringify(eof1.json, { eof: true });
const actual = CSV.stringify(eof1.json, { eof: true });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('EOF #2- When set to false the formatter should not include a newline at the end of file', (t) => {
const expect = eof2.csv.join('\n');
const result = CSV.stringify(eof2.json, { eof: false });
const actual = CSV.stringify(eof2.json, { eof: false });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});
28 changes: 14 additions & 14 deletions test/parse.rfc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,63 @@ const rfc7 = require('./__test__/rfc7.json');

test('RFC Rule #1 - One entry per line, each line ends with a newline', (t) => {
const expect = rfc1.json;
const result = parse(rfc1.csv.join('\n'));
const actual = parse(rfc1.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #2 - Trailing newline at the end of the file omitted', (t) => {
const expect = rfc2.json;
const result = parse(rfc2.csv.join('\n'));
const actual = parse(rfc2.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #3 - First row contains header data', (t) => {
const expect = rfc3.json;
const result = parse(rfc3.csv.join('\n'));
const actual = parse(rfc3.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #4 - Spaces are considered data and entries should not contain a trailing comma', (t) => {
const expect = rfc4.json;
const result = parse(rfc4.csv.join('\n'));
const actual = parse(rfc4.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #5 - Lines may or may not be delimited by double-quotes', (t) => {
const expect = rfc5.json;
const result = parse(rfc5.csv.join('\n'));
const actual = parse(rfc5.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #6 - Fields containing line breaks, double-quotes, and commas should be enclosed in double-quotes', (t) => {
const expect = rfc6.json;
const result = parse(rfc6.csv.join('\n'));
const actual = parse(rfc6.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #7 - If double-quotes are used to enclose fields, then a double-quote appering inside a field must be escaped by a preceding it with another double-quote', (t) => {
const expect = rfc7.json;
const result = parse(rfc7.csv.join('\n'));
const actual = parse(rfc7.csv.join('\n'));

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});
24 changes: 12 additions & 12 deletions test/stringify.rfc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,53 +11,53 @@ const rfc7 = require('./__test__/rfc7.json');

test('RFC Rule #1 - One entry per line, each line ends with a newline', (t) => {
const expect = rfc1.csv.join('\n');
const result = stringify(rfc1.json);
const actual = stringify(rfc1.json);

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #2 - Trailing newline at the end of the file omitted', (t) => {
const expect = rfc2.csv.join('\n');
const result = stringify(rfc2.json, { eof: false });
const actual = stringify(rfc2.json, { eof: false });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #3 - First row contains header data', (t) => {
const expect = rfc3.csv.join('\n');
const result = stringify(rfc3.json, { eof: false });
const actual = stringify(rfc3.json, { eof: false });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #4 - Spaces are considered data and entries should not contain a trailing comma', (t) => {
const expect = rfc4.csv.join('\n');
const result = stringify(rfc4.json, { eof: false });
t.deepEqual(result, expect);
const actual = stringify(rfc4.json, { eof: false });
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #6 - Fields containing line breaks, double-quotes, and commas should be enclosed in double-quotes', (t) => {
const expect = rfc6.csv.join('\n');
const result = stringify(rfc6.json, { eof: false });
const actual = stringify(rfc6.json, { eof: false });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});

test('RFC Rule #7 - If double-quotes are used to enclose fields, then a double-quote appering inside a field must be escaped by a preceding it with another double-quote', (t) => {
const expect = rfc7.csv.join('\n');
const result = stringify(rfc7.json, { eof: false });
const actual = stringify(rfc7.json, { eof: false });

t.deepEqual(result, expect);
t.deepEqual(actual, expect);

t.end();
});
24 changes: 12 additions & 12 deletions test/typed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,43 @@ const floats = require('./__test__/floats.json');
const bools = require('./__test__/bools.json');

test('Parse Integers - Can parse data containing integers', (t) => {
const result = CSV.parse(integers.csv.join('\n'), { typed: true });
const actual = CSV.parse(integers.csv.join('\n'), { typed: true });
const expect = integers.json;
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('Stringify Integers - Can stringify data containing integers', (t) => {
const result = CSV.stringify(integers.json);
const actual = CSV.stringify(integers.json);
const expect = integers.csv.join('\n');
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('Parse Floats - Can parse data containing floats', (t) => {
const result = CSV.parse(floats.csv.join('\n'), { typed: true });
const actual = CSV.parse(floats.csv.join('\n'), { typed: true });
const expect = floats.json;
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('Stringify Floats - Can stringify data containing floats', (t) => {
const result = CSV.stringify(floats.json);
const actual = CSV.stringify(floats.json);
const expect = floats.csv.join('\n');
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('Parse Bools - Can parse data containing booleans', (t) => {
const result = CSV.parse(bools.csv.join('\n'), { typed: true });
const actual = CSV.parse(bools.csv.join('\n'), { typed: true });
const expect = bools.json;
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

test('Stringify Bools - Can stringify data containing booleans', (t) => {
const result = CSV.stringify(bools.json);
const actual = CSV.stringify(bools.json);
const expect = bools.csv.join('\n');
t.deepEqual(result, expect);
t.deepEqual(actual, expect);
t.end();
});

0 comments on commit 1a0cfbb

Please sign in to comment.