Permalink
Browse files

Add test for csv.parse.

Woot, caught my first bug!
  • Loading branch information...
1 parent 3a5a6c6 commit 26c69879c9227accbbf4489bb49bd4d15015d309 @mbostock mbostock committed Aug 14, 2011
Showing with 104 additions and 30 deletions.
  1. +1 −1 d3.csv.js
  2. +1 −1 d3.csv.min.js
  3. +1 −1 src/csv/parse.js
  4. +101 −0 test/csv/parse-test.js
  5. +0 −18 test/csv/test-parse.js
  6. +0 −9 test/csv/test-parse.out
View
@@ -30,7 +30,7 @@ d3.csv.parseRows = function(text, f) {
/** @private Returns the next token. */
function token() {
- if (re.lastIndex === text.length) return EOF; // special case: end of file
+ if (re.lastIndex >= text.length) return EOF; // special case: end of file
if (eol) { eol = false; return EOL; } // special case: end of line
// special case: quotes
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -25,7 +25,7 @@ d3.csv.parseRows = function(text, f) {
/** @private Returns the next token. */
function token() {
- if (re.lastIndex === text.length) return EOF; // special case: end of file
+ if (re.lastIndex >= text.length) return EOF; // special case: end of file
if (eol) { eol = false; return EOL; } // special case: end of line
// special case: quotes
View
@@ -0,0 +1,101 @@
+require("../env");
+require("../../d3");
+require("../../d3.csv");
+
+var vows = require("vows"),
+ assert = require("assert");
+
+var suite = vows.describe("d3.csv");
+
+suite.addBatch({
+ "parse": {
+ topic: function() {
+ return d3.csv.parse;
+ },
+ "returns an array of objects": function(parse) {
+ assert.deepEqual(parse("a,b,c\n1,2,3\n"), [{a: "1", b: "2", c: "3"}]);
+ },
+ "does not strip whitespace": function(parse) {
+ assert.deepEqual(parse("a,b,c\n 1, 2,3\n"), [{a: " 1", b: " 2", c: "3"}]);
+ },
+ "parses quoted values": function(parse) {
+ assert.deepEqual(parse("a,b,c\n\"1\",2,3"), [{a: "1", b: "2", c: "3"}]);
+ assert.deepEqual(parse("a,b,c\n\"1\",2,3\n"), [{a: "1", b: "2", c: "3"}]);
+ },
+ "parses quoted values with quotes": function(parse) {
+ assert.deepEqual(parse("a\n\"\"\"hello\"\"\""), [{a: "\"hello\""}]);
+ },
+ "parses quoted values with newlines": function(parse) {
+ assert.deepEqual(parse("a\n\"new\nline\""), [{a: "new\nline"}]);
+ assert.deepEqual(parse("a\n\"new\rline\""), [{a: "new\rline"}]);
+ assert.deepEqual(parse("a\n\"new\r\nline\""), [{a: "new\r\nline"}]);
+ },
+ "parses unix newlines": function(parse) {
+ assert.deepEqual(parse("a,b,c\n1,2,3\n4,5,\"6\"\n7,8,9"), [
+ {a: "1", b: "2", c: "3"},
+ {a: "4", b: "5", c: "6"},
+ {a: "7", b: "8", c: "9"}
+ ]);
+ },
+ "parses mac newlines": function(parse) {
+ assert.deepEqual(parse("a,b,c\r1,2,3\r4,5,\"6\"\r7,8,9"), [
+ {a: "1", b: "2", c: "3"},
+ {a: "4", b: "5", c: "6"},
+ {a: "7", b: "8", c: "9"}
+ ]);
+ },
+ "parses dos newlines": function(parse) {
+ assert.deepEqual(parse("a,b,c\r\n1,2,3\r\n4,5,\"6\"\r\n7,8,9"), [
+ {a: "1", b: "2", c: "3"},
+ {a: "4", b: "5", c: "6"},
+ {a: "7", b: "8", c: "9"}
+ ]);
+ }
+ },
+ "parseRows": {
+ topic: function() {
+ return d3.csv.parseRows;
+ },
+ "returns an array of arrays": function(parse) {
+ assert.deepEqual(parse("a,b,c\n"), [["a", "b", "c"]]);
+ },
+ "parses quoted values": function(parse) {
+ assert.deepEqual(parse("\"1\",2,3\n"), [["1", "2", "3"]]);
+ assert.deepEqual(parse("\"hello\""), [["hello"]]);
+ },
+ "parses quoted values with quotes": function(parse) {
+ assert.deepEqual(parse("\"\"\"hello\"\"\""), [["\"hello\""]]);
+ },
+ "parses quoted values with newlines": function(parse) {
+ assert.deepEqual(parse("\"new\nline\""), [["new\nline"]]);
+ assert.deepEqual(parse("\"new\rline\""), [["new\rline"]]);
+ assert.deepEqual(parse("\"new\r\nline\""), [["new\r\nline"]]);
+ },
+ "parses unix newlines": function(parse) {
+ assert.deepEqual(parse("a,b,c\n1,2,3\n4,5,\"6\"\n7,8,9"), [
+ ["a", "b", "c"],
+ ["1", "2", "3"],
+ ["4", "5", "6"],
+ ["7", "8", "9"]
+ ]);
+ },
+ "parses mac newlines": function(parse) {
+ assert.deepEqual(parse("a,b,c\r1,2,3\r4,5,\"6\"\r7,8,9"), [
+ ["a", "b", "c"],
+ ["1", "2", "3"],
+ ["4", "5", "6"],
+ ["7", "8", "9"]
+ ]);
+ },
+ "parses dos newlines": function(parse) {
+ assert.deepEqual(parse("a,b,c\r\n1,2,3\r\n4,5,\"6\"\r\n7,8,9"), [
+ ["a", "b", "c"],
+ ["1", "2", "3"],
+ ["4", "5", "6"],
+ ["7", "8", "9"]
+ ]);
+ }
+ }
+});
+
+suite.export(module);
View
@@ -1,18 +0,0 @@
-require("./../../lib/env-js/envjs/node");
-require("./../../d3");
-require("./../../d3.csv");
-
-var newlines = {
- UNIX: "\n",
- DOS: "\r\n",
- Mac: "\r"
-};
-
-for (os in newlines) {
- console.log("parse " + os + " newlines:");
- var rows = d3.csv.parse(["a,b,c", "1,2,3", "4,5,\"6\"", "7,8,9"].join(newlines[os]));
- console.log(" " + rows.map(function(row) {
- return row.a + ":" + row.b + ":" + row.c;
- }).join("|"));
- console.log("");
-}
View
@@ -1,9 +0,0 @@
-parse UNIX newlines:
- 1:2:3|4:5:6|7:8:9
-
-parse DOS newlines:
- 1:2:3|4:5:6|7:8:9
-
-parse Mac newlines:
- 1:2:3|4:5:6|7:8:9
-

0 comments on commit 26c6987

Please sign in to comment.