Commit 608902c9 authored by Mike Bostock's avatar Mike Bostock
Browse files

Checkpoint test refactoring.

parent 3442685b
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -31,7 +31,7 @@
"jsdom": "~0.5.2"
},
"devDependencies": {
"smash": "~0.0.5",
"smash": "~0.0.7",
"uglify-js": "2.2.x",
"vows": "0.7.x"
},
......
......@@ -184,6 +184,6 @@ var d3_behavior_zoomInfinity = [0, Infinity]; // default scale extent
// https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/wheel
var d3_behavior_zoomDelta, d3_behavior_zoomWheel
= "onwheel" in document ? (d3_behavior_zoomDelta = function() { return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); }, "wheel")
: "onmousewheel" in document ? (d3_behavior_zoomDelta = function() { return d3.event.wheelDelta; }, "mousewheel")
= "onwheel" in d3_document ? (d3_behavior_zoomDelta = function() { return -d3.event.deltaY * (d3.event.deltaMode ? 120 : 1); }, "wheel")
: "onmousewheel" in d3_document ? (d3_behavior_zoomDelta = function() { return d3.event.wheelDelta; }, "mousewheel")
: (d3_behavior_zoomDelta = function() { return -d3.event.detail; }, "MozMousePixelScroll");
import "dsv";
d3.csv = d3_dsv(",", "text/csv");
import "../arrays/set";
import "../xhr/xhr";
function d3_dsv(delimiter, mimeType) {
var reFormat = new RegExp("[\"" + delimiter + "\n]"),
delimiterCode = delimiter.charCodeAt(0);
......
import "dsv";
d3.tsv = d3_dsv("\t", "text/tab-separated-values");
import "../arrays/merge";
import "../math/trigonometry";
import "clip";
import "clip-polygon";
......
import "../arrays/merge";
import "../core/noop";
import "../math/trigonometry";
import "clip-polygon";
......
import "../math/trigonometry";
import "conic";
import "equirectangular";
import "geo";
import "projection";
......
import "../core/identity";
import "../math/trigonometry";
import "albers-usa";
import "area";
import "bounds";
import "centroid";
import "geo";
import "path-area";
import "path-buffer";
import "path-centroid";
import "path-context";
import "projection";
import "resample";
import "stream";
......
import "../math/trigonometry";
import "equirectangular";
import "geo";
d3.geo.rotation = function(rotate) {
......
import "../arrays/range";
import "../math/trigonometry";
import "geom";
// Adapted from Nicolas Garcia Belmonte's JIT implementation:
......
import "../core/functor";
import "../geom/polygon";
import "../geom/voronoi";
import "../svg/line";
import "layout";
......
import "../core/array";
import "../core/document";
import "selection";
d3_selectionPrototype.on = function(type, listener, capture) {
......@@ -72,7 +73,7 @@ var d3_selection_onFilters = d3.map({
});
d3_selection_onFilters.forEach(function(k) {
if ("on" + k in document) d3_selection_onFilters.remove(k);
if ("on" + k in d3_document) d3_selection_onFilters.remove(k);
});
function d3_selection_onListener(listener, argumentz) {
......
require("../env");
var vows = require("vows"),
load = require("../load"),
xhr = require("../env-xhr"),
assert = require("../env-assert");
var suite = vows.describe("d3.csv");
suite.addBatch({
"csv": {
topic: function() {
var cb = this.callback;
d3.csv("test/data/sample.csv", function(error, csv) {
cb(null, csv);
});
},
"invokes the callback with the parsed CSV": function(csv) {
assert.deepEqual(csv, [{"Hello":"42","World":"\"fish\""}]);
},
"overrides the mime type to text/csv": function(csv) {
assert.equal(XMLHttpRequest._last._info.mimeType, "text/csv");
topic: load("dsv/csv").sandbox({
XMLHttpRequest: xhr,
document: {},
window: {}
}),
"on a sample file": {
topic: function(d3) {
var cb = this.callback;
d3.csv("test/data/sample.csv", function(error, csv) {
cb(null, csv);
});
},
"invokes the callback with the parsed CSV": function(csv) {
assert.deepEqual(csv, [{"Hello":"42","World":"\"fish\""}]);
},
"overrides the mime type to text/csv": function(csv) {
assert.equal(xhr._last._info.mimeType, "text/csv");
}
},
"specifying a row conversion function": {
topic: function() {
"when specifying a row conversion function": {
topic: function(d3) {
var cb = this.callback;
d3.csv("test/data/sample.csv", function(row) {
row.Hello = -row.Hello;
......@@ -33,8 +42,9 @@ suite.addBatch({
assert.strictEqual(csv[0].Hello, -42);
}
},
"attempting to load a file that does not exist": {
topic: function() {
"when loading a file that does not exist": {
topic: function(d3) {
var cb = this.callback;
d3.csv("//does/not/exist.csv", function(error, csv) {
cb(null, csv);
......@@ -43,171 +53,171 @@ suite.addBatch({
"invokes the callback with undefined": function(csv) {
assert.isUndefined(csv);
}
}
},
"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"}
]);
}
},
"parse with row function": {
"invokes the row function for every row in order": function() {
var rows = [];
d3.csv.parse("a\n1\n2\n3\n4", function(d, i) { rows.push({d: d, i: i}); });
assert.deepEqual(rows, [
{d: {a: "1"}, i: 0},
{d: {a: "2"}, i: 1},
{d: {a: "3"}, i: 2},
{d: {a: "4"}, i: 3}
]);
},
"returns an array of the row function return values": function() {
assert.deepEqual(d3.csv.parse("a,b,c\n1,2,3\n", function(row) { return row; }), [{a: "1", b: "2", c: "3"}]);
"parse": {
topic: function(d3) {
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"}
]);
}
},
"skips rows if the row function returns null or undefined": function() {
assert.deepEqual(d3.csv.parse("a,b,c\n1,2,3\n2,3,4", function(row) { return row.a & 1 ? null : row; }), [{a: "2", b: "3", c: "4"}]);
assert.deepEqual(d3.csv.parse("a,b,c\n1,2,3\n2,3,4", function(row) { return row.a & 1 ? undefined : row; }), [{a: "2", b: "3", c: "4"}]);
}
},
"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"]
]);
"parse with row function": {
"invokes the row function for every row in order": function(d3) {
var rows = [];
d3.csv.parse("a\n1\n2\n3\n4", function(d, i) { rows.push({d: d, i: i}); });
assert.deepEqual(rows, [
{d: {a: "1"}, i: 0},
{d: {a: "2"}, i: 1},
{d: {a: "3"}, i: 2},
{d: {a: "4"}, i: 3}
]);
},
"returns an array of the row function return values": function(d3) {
assert.deepEqual(d3.csv.parse("a,b,c\n1,2,3\n", function(row) { return row; }), [{a: "1", b: "2", c: "3"}]);
},
"skips rows if the row function returns null or undefined": function(d3) {
assert.deepEqual(d3.csv.parse("a,b,c\n1,2,3\n2,3,4", function(row) { return row.a & 1 ? null : row; }), [{a: "2", b: "3", c: "4"}]);
assert.deepEqual(d3.csv.parse("a,b,c\n1,2,3\n2,3,4", function(row) { return row.a & 1 ? undefined : row; }), [{a: "2", b: "3", c: "4"}]);
}
},
"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"]
]);
}
},
"format": {
topic: function() {
return d3.csv.format;
},
"takes an array of objects as input": function(format) {
assert.equal(format([{a: 1, b: 2, c: 3}]), "a,b,c\n1,2,3");
},
"escapes field names containing special characters": function(format) {
assert.equal(format([{"foo,bar": true}]), "\"foo,bar\"\ntrue");
},
"computes the union of all fields": function(format) {
assert.equal(format([
{a: 1},
{a: 1, b: 2},
{a: 1, b: 2, c: 3},
{b: 1, c: 2},
{c: 1}
]), "a,b,c\n1,,\n1,2,\n1,2,3\n,1,2\n,,1");
"parseRows": {
topic: function(d3) {
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"]
]);
}
},
"orders field by first-seen": function(format) {
assert.equal(format([
{a: 1, b: 2},
{c: 3, b: 4},
{c: 5, a: 1, b: 2}
]), "a,b,c\n1,2,\n,4,3\n1,2,5");
}
},
"formatRows": {
topic: function() {
return d3.csv.formatRows;
},
"takes an array of arrays as input": function(format) {
assert.equal(format([["a", "b", "c"], ["1", "2", "3"]]), "a,b,c\n1,2,3");
},
"separates lines using unix newline": function(format) {
assert.equal(format([[], []]), "\n");
},
"does not strip whitespace": function(format) {
assert.equal(format([["a ", " b", "c"], ["1", "2", "3 "]]), "a , b,c\n1,2,3 ");
},
"does not quote simple values": function(format) {
assert.equal(format([["a"], [1]]), "a\n1");
},
"escapes double quotes": function(format) {
assert.equal(format([["\"fish\""]]), "\"\"\"fish\"\"\"");
},
"escapes unix newlines": function(format) {
assert.equal(format([["new\nline"]]), "\"new\nline\"");
"format": {
topic: function(d3) {
return d3.csv.format;
},
"takes an array of objects as input": function(format) {
assert.equal(format([{a: 1, b: 2, c: 3}]), "a,b,c\n1,2,3");
},
"escapes field names containing special characters": function(format) {
assert.equal(format([{"foo,bar": true}]), "\"foo,bar\"\ntrue");
},
"computes the union of all fields": function(format) {
assert.equal(format([
{a: 1},
{a: 1, b: 2},
{a: 1, b: 2, c: 3},
{b: 1, c: 2},
{c: 1}
]), "a,b,c\n1,,\n1,2,\n1,2,3\n,1,2\n,,1");
},
"orders field by first-seen": function(format) {
assert.equal(format([
{a: 1, b: 2},
{c: 3, b: 4},
{c: 5, a: 1, b: 2}
]), "a,b,c\n1,2,\n,4,3\n1,2,5");
}
},
"escapes commas": function(format) {
assert.equal(format([["oxford,comma"]]), "\"oxford,comma\"");
"formatRows": {
topic: function(d3) {
return d3.csv.formatRows;
},
"takes an array of arrays as input": function(format) {
assert.equal(format([["a", "b", "c"], ["1", "2", "3"]]), "a,b,c\n1,2,3");
},
"separates lines using unix newline": function(format) {
assert.equal(format([[], []]), "\n");
},
"does not strip whitespace": function(format) {
assert.equal(format([["a ", " b", "c"], ["1", "2", "3 "]]), "a , b,c\n1,2,3 ");
},
"does not quote simple values": function(format) {
assert.equal(format([["a"], [1]]), "a\n1");
},
"escapes double quotes": function(format) {
assert.equal(format([["\"fish\""]]), "\"\"\"fish\"\"\"");
},
"escapes unix newlines": function(format) {
assert.equal(format([["new\nline"]]), "\"new\nline\"");
},
"escapes commas": function(format) {
assert.equal(format([["oxford,comma"]]), "\"oxford,comma\"");
}
}
}
});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
xhr = require("../env-xhr"),
assert = require("../env-assert");
var suite = vows.describe("d3.tsv");
suite.addBatch({
"tsv": {
topic: function() {
var cb = this.callback;
d3.tsv("test/data/sample.tsv", function(error, tsv) {
cb(null, tsv);
});
},
"invokes the callback with the parsed tsv": function(tsv) {
assert.deepEqual(tsv, [{"Hello":42,"World":"\"fish\""}]);
},
"overrides the mime type to text/tab-separated-values": function(tsv) {
assert.equal(XMLHttpRequest._last._info.mimeType, "text/tab-separated-values");
topic: load("dsv/tsv").sandbox({
XMLHttpRequest: xhr,
document: {},
window: {}
}),
"on a sample file": {
topic: function(d3) {
var cb = this.callback;
d3.tsv("test/data/sample.tsv", function(error, tsv) {
cb(null, tsv);
});
},
"invokes the callback with the parsed tsv": function(tsv) {
assert.deepEqual(tsv, [{"Hello":42,"World":"\"fish\""}]);
},
"overrides the mime type to text/tab-separated-values": function(tsv) {
assert.equal(xhr._last._info.mimeType, "text/tab-separated-values");
}
},
"": {
topic: function() {
"on a file that does not exist": {
topic: function(d3) {
var cb = this.callback;
d3.tsv("//does/not/exist.tsv", function(error, tsv) {
cb(null, tsv);
......@@ -29,153 +38,154 @@ suite.addBatch({
"invokes the callback with undefined when an error occurs": function(tsv) {
assert.isUndefined(tsv);
}
}
},
"parse": {
topic: function() {
return d3.tsv.parse;
},
"returns an array of objects": function(parse) {
assert.deepEqual(parse("a\tb\tc\n1\t2\t3\n"), [{a: "1", b: "2", c: "3"}]);
},
"does not strip whitespace": function(parse) {
assert.deepEqual(parse("a\tb\tc\n 1\t 2\t3\n"), [{a: " 1", b: " 2", c: "3"}]);
},
"parses quoted values": function(parse) {
assert.deepEqual(parse("a\tb\tc\n\"1\"\t2\t3"), [{a: "1", b: "2", c: "3"}]);
assert.deepEqual(parse("a\tb\tc\n\"1\"\t2\t3\n"), [{a: "1", b: "2", c: "3"}]);
},
"parses quoted values with quotes": function(parse) {
assert.deepEqual(parse("a\n\"\"\"hello\"\"\""), [{a: "\"hello\""}]);
},