Browse Source

Add async parse tests

pull/75/head
Nate McMaster 11 years ago
parent
commit
4c54129036
  1. 2
      tests/sample.csv
  2. 37
      tests/test-cases.js
  3. 105
      tests/test-runner.js

2
tests/sample.csv

@ -0,0 +1,2 @@
A,B,C
X,Y,Z
1 A B C
2 X Y Z

37
tests/test-cases.js

@ -458,6 +458,43 @@ var PARSE_TESTS = [
} }
]; ];
var PARSE_ASYNC_TESTS = [
{
description: "Simple worker",
input: "A,B,C\nX,Y,Z",
config: {
worker: true,
},
expected: {
data: [['A','B','C'],['X','Y','Z']],
errors: []
}
},
{
description: "Simple download",
input: "/tests/sample.csv",
config: {
download: true
},
expected: {
data: [['A','B','C'],['X','Y','Z']],
errors: []
}
},
{
description: "Simple download + worker",
input: "/tests/sample.csv",
config: {
worker: true,
download: true
},
expected: {
data: [['A','B','C'],['X','Y','Z']],
errors: []
}
}
];

105
tests/test-runner.js

@ -35,25 +35,25 @@ $(function()
$('.expanded .rvl', $testGroup).click(); $('.expanded .rvl', $testGroup).click();
}); });
function asyncDone() {
// Finally, show the overall status.
if (failCount == 0)
$('#status').addClass('status-pass').html("All <b>" + passCount + "</b> test" + (passCount == 1 ? "" : "s") + " passed");
else
$('#status').addClass('status-fail').html("<b>" + failCount + "</b> test" + (failCount == 1 ? "" : "s") + " failed; <b>" + passCount + "</b> passed");
}
// Next, run tests and render results! // Next, run tests and render results!
runParseTests(); runParseTests(asyncDone);
runUnparseTests(); runUnparseTests();
// Finally, show the overall status.
if (failCount == 0)
$('#status').addClass('status-pass').html("All <b>"+passCount+"</b> test"+(passCount == 1 ? "" : "s")+" passed");
else
$('#status').addClass('status-fail').html("<b>"+failCount+"</b> test"+(failCount == 1 ? "" : "s")+" failed; <b>"+passCount+"</b> passed");
}); });
// Executes all tests in PARSE_TESTS from test-cases.js // Executes all tests in PARSE_TESTS from test-cases.js
// and renders results in the table. // and renders results in the table.
function runParseTests() function runParseTests(asyncDone)
{ {
for (var i = 0; i < PARSE_TESTS.length; i++) for (var i = 0; i < PARSE_TESTS.length; i++)
{ {
@ -65,24 +65,60 @@ function runParseTests()
failCount++; failCount++;
} }
var asyncRemaining = PARSE_ASYNC_TESTS.length;
function runTest(test) PARSE_ASYNC_TESTS.forEach(function(test) {
{ var config = test.config;
config.complete = function(actual) {
var results = compare(actual.data, actual.errors, test.expected);
displayResults(test, actual, results);
if (results.data.passed && results.errors.passed) {
passCount++;
} else {
failCount++;
}
if (--asyncRemaining === 0) {
asyncDone();
}
}
config.error = function(err) {
failCount++;
displayResults(test, {data:[],errors:err}, test.expected);
if (--asyncRemaining === 0) {
asyncDone();
}
}
Papa.parse(test.input, test.config);
});
function runTest(test) {
var actual; var actual;
try try {
{
actual = Papa.parse(test.input, test.config); actual = Papa.parse(test.input, test.config);
} } catch (e) {
catch (e) if (e instanceof Error) {
{ throw e;
}
actual.data = []; actual.data = [];
actual.errors = [e]; actual.errors = [e];
} }
var testId = testCount++;
var results = compare(actual.data, actual.errors, test.expected); var results = compare(actual.data, actual.errors, test.expected);
displayResults(test, actual, results);
return results.data.passed && results.errors.passed
}
function displayResults(test, actual, results) {
var testId = testCount++;
var testDescription = (test.description || ""); var testDescription = (test.description || "");
if (testDescription.length > 0) if (testDescription.length > 0)
testDescription += '<br>'; testDescription += '<br>';
@ -103,9 +139,8 @@ function runParseTests()
$('#tests-for-parse .results').append(tr); $('#tests-for-parse .results').append(tr);
if (!results.data.passed || !results.errors.passed) if (!results.data.passed || !results.errors.passed)
$('#test-'+testId+' td.rvl').click(); $('#test-' + testId + ' td.rvl').click();
return results.data.passed && results.errors.passed
} }
@ -113,7 +148,7 @@ function runParseTests()
{ {
var data = compareData(actualData, expected.data); var data = compareData(actualData, expected.data);
var errors = compareErrors(actualErrors, expected.errors); var errors = compareErrors(actualErrors, expected.errors);
return { return {
data: data, data: data,
errors: errors errors: errors
@ -124,26 +159,25 @@ function runParseTests()
{ {
var passed = true; var passed = true;
if (actual.length != expected.length) if (actual.length != expected.length) {
passed = false; passed = false;
} else {
for (var row = 0; row < expected.length; row++) for (var row = 0; row < expected.length; row++) {
{ if (actual[row].length != expected[row].length) {
if (actual[row].length != expected[row].length) passed = false;
{ break;
passed = false; }
break;
}
for (var col = 0; col < expected[row].length; col++) for (var col = 0; col < expected[row].length; col++)
{ {
var expectedVal = expected[row][col]; var expectedVal = expected[row][col];
var actualVal = actual[row][col]; var actualVal = actual[row][col];
if (actualVal !== expectedVal) if (actualVal !== expectedVal)
{ {
passed = false; passed = false;
break; break;
}
} }
} }
} }
@ -197,6 +231,9 @@ function runUnparseTests()
} }
catch (e) catch (e)
{ {
if (e instanceof Error) {
throw e;
}
actual = e; actual = e;
} }
@ -222,7 +259,7 @@ function runUnparseTests()
$('#tests-for-unparse .results').append(tr); $('#tests-for-unparse .results').append(tr);
if (!results.passed) if (!results.passed)
$('#test-'+testId+' td.rvl').click(); $('#test-' + testId + ' td.rvl').click();
return results.passed; return results.passed;
} }

Loading…
Cancel
Save