Commit a6fa0c4f authored by Mike Bostock's avatar Mike Bostock
Browse files

Refactor layout tests for minimal loading.

parent dd971666
......@@ -6,26 +6,26 @@ var suite = vows.describe("d3.layout.cluster");
suite.addBatch({
"cluster": {
topic: load("layout/cluster"),
"can handle an empty children array": function(d3) {
var cluster = d3.layout.cluster();
assert.deepEqual(cluster.nodes({value: 1, children: [{value: 1, children: []}, {value: 1}]}).map(layout), [
topic: load("layout/cluster").expression("d3.layout.cluster"),
"can handle an empty children array": function(cluster) {
var c = cluster();
assert.deepEqual(c.nodes({value: 1, children: [{value: 1, children: []}, {value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0},
{value: 1, depth: 1, x: 0.25, y: 1},
{value: 1, depth: 1, x: 0.75, y: 1}
]);
},
"can handle zero-valued nodes": function(d3) {
var cluster = d3.layout.cluster();
assert.deepEqual(cluster.nodes({value: 0, children: [{value: 0}, {value: 1}]}).map(layout), [
"can handle zero-valued nodes": function(cluster) {
var c = cluster();
assert.deepEqual(c.nodes({value: 0, children: [{value: 0}, {value: 1}]}).map(layout), [
{value: 0, depth: 0, x: 0.5, y: 0},
{value: 0, depth: 1, x: 0.25, y: 1},
{value: 1, depth: 1, x: 0.75, y: 1}
]);
},
"can handle a single node": function(d3) {
var cluster = d3.layout.cluster();
assert.deepEqual(cluster.nodes({value: 0}).map(layout), [
"can handle a single node": function(cluster) {
var c = cluster();
assert.deepEqual(c.nodes({value: 0}).map(layout), [
{value: 0, depth: 0, x: 0.5, y: 0}
]);
}
......
......@@ -6,111 +6,108 @@ var suite = vows.describe("d3.layout.force");
suite.addBatch({
"force": {
topic: load("layout/force").sandbox({
document: {},
window: {},
setTimeout: setTimeout,
clearTimeout: clearTimeout
}),
topic: load("layout/force")
.expression("d3.layout.force")
.sandbox({document: {}, window: {}, setTimeout: setTimeout, clearTimeout: clearTimeout}),
"default instance": {
topic: function(d3) {
return d3.layout.force();
topic: function(force) {
return force();
},
"friction": {
"defaults to .9": function(force) {
assert.equal(force.friction(), .9);
"defaults to .9": function(f) {
assert.equal(f.friction(), .9);
},
"can be a number": function(force) {
force.friction(.5);
assert.equal(force.friction(), .5);
"can be a number": function(f) {
f.friction(.5);
assert.equal(f.friction(), .5);
},
"coerces to a number": function(force) {
force.friction(".5");
assert.strictEqual(force.friction(), .5);
"coerces to a number": function(f) {
f.friction(".5");
assert.strictEqual(f.friction(), .5);
}
},
"gravity": {
"defaults to .1": function(force) {
assert.equal(force.gravity(), .1);
"defaults to .1": function(f) {
assert.equal(f.gravity(), .1);
},
"can be a number": function(force) {
force.gravity(.5);
assert.equal(force.gravity(), .5);
"can be a number": function(f) {
f.gravity(.5);
assert.equal(f.gravity(), .5);
},
"coerces to a number": function(force) {
force.gravity(".5");
assert.strictEqual(force.gravity(), .5);
"coerces to a number": function(f) {
f.gravity(".5");
assert.strictEqual(f.gravity(), .5);
}
},
"theta": {
"defaults to .8": function(force) {
assert.equal(force.theta(), .8);
"defaults to .8": function(f) {
assert.equal(f.theta(), .8);
},
"can be a number": function(force) {
force.theta(.5);
assert.equal(force.theta(), .5);
"can be a number": function(f) {
f.theta(.5);
assert.equal(f.theta(), .5);
},
"coerces to a number": function(force) {
force.theta(".5");
assert.strictEqual(force.theta(), .5);
"coerces to a number": function(f) {
f.theta(".5");
assert.strictEqual(f.theta(), .5);
}
},
"charge": {
"defaults to -30": function(force) {
assert.equal(force.charge(), -30);
"defaults to -30": function(f) {
assert.equal(f.charge(), -30);
},
"can be a number": function(force) {
force.charge(-40);
assert.equal(force.charge(), -40);
"can be a number": function(f) {
f.charge(-40);
assert.equal(f.charge(), -40);
},
"can be a function": function(force) { // TODO expose the computed value?
force.charge(foo);
assert.equal(force.charge(), foo);
"can be a function": function(f) { // TODO expose the computed value?
f.charge(foo);
assert.equal(f.charge(), foo);
},
"coerces to a number": function(force) {
force.charge("-40");
assert.strictEqual(force.charge(), -40);
"coerces to a number": function(f) {
f.charge("-40");
assert.strictEqual(f.charge(), -40);
}
},
"linkDistance": {
"defaults to 20": function(force) {
assert.equal(force.linkDistance(), 20);
"defaults to 20": function(f) {
assert.equal(f.linkDistance(), 20);
},
"can be a number": function(force) {
force.linkDistance(40);
assert.equal(force.linkDistance(), 40);
"can be a number": function(f) {
f.linkDistance(40);
assert.equal(f.linkDistance(), 40);
},
"can be a function": function(force) { // TODO expose the computed value?
force.linkDistance(foo);
assert.equal(force.linkDistance(), foo);
"can be a function": function(f) { // TODO expose the computed value?
f.linkDistance(foo);
assert.equal(f.linkDistance(), foo);
},
"coerces to a number": function(force) {
force.linkDistance("40");
assert.strictEqual(force.linkDistance(), 40);
"coerces to a number": function(f) {
f.linkDistance("40");
assert.strictEqual(f.linkDistance(), 40);
}
},
"linkStrength": {
"defaults to 1": function(force) {
assert.equal(force.linkStrength(), 1);
"defaults to 1": function(f) {
assert.equal(f.linkStrength(), 1);
},
"can be a number": function(force) {
force.linkStrength(.5);
assert.equal(force.linkStrength(), .5);
"can be a number": function(f) {
f.linkStrength(.5);
assert.equal(f.linkStrength(), .5);
},
"can be a function": function(force) { // TODO expose the computed value?
force.linkStrength(foo);
assert.equal(force.linkStrength(), foo);
"can be a function": function(f) { // TODO expose the computed value?
f.linkStrength(foo);
assert.equal(f.linkStrength(), foo);
},
"coerces to a number": function(force) {
force.linkStrength(".5");
assert.strictEqual(force.linkStrength(), .5);
"coerces to a number": function(f) {
f.linkStrength(".5");
assert.strictEqual(f.linkStrength(), .5);
}
}
}
......
......@@ -6,19 +6,19 @@ var suite = vows.describe("d3.layout.hierarchy");
suite.addBatch({
"hierarchy": {
topic: load("layout/treemap"), // hierarchy is abstract, so test a subclass
"doesn't overwrite the value of a node that has an empty children array": function(d3) {
var hierarchy = d3.layout.treemap(),
nodes = hierarchy.sticky(true).nodes({value: 1, children: []});
topic: load("layout/treemap").expression("d3.layout.treemap"), // hierarchy is abstract, so test a subclass
"doesn't overwrite the value of a node that has an empty children array": function(hierarchy) {
var h = hierarchy(),
nodes = h.sticky(true).nodes({value: 1, children: []});
assert.equal(nodes[0].value, 1);
hierarchy.nodes(nodes[0]);
h.nodes(nodes[0]);
assert.equal(nodes[0].value, 1);
},
"a valueless node that has an empty children array gets a value of 0": function(d3) {
var hierarchy = d3.layout.treemap(),
nodes = hierarchy.sticky(true).nodes({children: []});
"a valueless node that has an empty children array gets a value of 0": function(hierarchy) {
var h = hierarchy(),
nodes = h.sticky(true).nodes({children: []});
assert.equal(nodes[0].value, 0);
hierarchy.nodes(nodes[0]);
h.nodes(nodes[0]);
assert.equal(nodes[0].value, 0);
}
}
......
......@@ -6,17 +6,17 @@ var suite = vows.describe("d3.layout.histogram");
suite.addBatch({
"histogram": {
topic: load("layout/histogram"),
"defaults to frequencies": function(d3) {
var h = d3.layout.histogram();
topic: load("layout/histogram").expression("d3.layout.histogram"),
"defaults to frequencies": function(histogram) {
var h = histogram();
assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [[0, 0, 0], [], [1], [2, 2]]);
},
"each bin contains the matching source elements": function(d3) {
var h = d3.layout.histogram();
"each bin contains the matching source elements": function(histogram) {
var h = histogram();
assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [[0, 0, 0], [], [1], [2, 2]]);
},
"each bin also has defined x, y and dx properties": function(d3) {
var h = d3.layout.histogram();
"each bin also has defined x, y and dx properties": function(histogram) {
var h = histogram();
assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [
{x: 0, y: 3, dx: 0.5},
{x: 0.5, y: 0, dx: 0.5},
......@@ -24,8 +24,8 @@ suite.addBatch({
{x: 1.5, y: 2, dx: 0.5}
]);
},
"can output frequencies": function(d3) {
var h = d3.layout.histogram().frequency(true);
"can output frequencies": function(histogram) {
var h = histogram().frequency(true);
assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [
{x: 0, y: 3, dx: 0.5},
{x: 0.5, y: 0, dx: 0.5},
......@@ -33,8 +33,8 @@ suite.addBatch({
{x: 1.5, y: 2, dx: 0.5}
]);
},
"can output probabilities": function(d3) {
var h = d3.layout.histogram().frequency(false);
"can output probabilities": function(histogram) {
var h = histogram().frequency(false);
assert.deepEqual(h([0,0,0,1,2,2]).map(metadata), [
{x: 0, y: 3/6, dx: 0.5},
{x: 0.5, y: 0, dx: 0.5},
......@@ -42,8 +42,8 @@ suite.addBatch({
{x: 1.5, y: 2/6, dx: 0.5}
]);
},
"can specify number of bins": function(d3) {
var h = d3.layout.histogram().bins(2);
"can specify number of bins": function(histogram) {
var h = histogram().bins(2);
assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [
[0, 0, 0],
[1, 2, 2]
......@@ -53,8 +53,8 @@ suite.addBatch({
{x: 1, y: 3, dx: 1}
]);
},
"can specify bin thresholds": function(d3) {
var h = d3.layout.histogram().bins([0,1,2,3]);
"can specify bin thresholds": function(histogram) {
var h = histogram().bins([0,1,2,3]);
assert.deepEqual(h([0,0,0,1,2,2]).map(elements), [
[0, 0, 0],
[1],
......@@ -66,10 +66,10 @@ suite.addBatch({
{x: 2, y: 2, dx: 1}
]);
},
"returns the empty array with fewer than two bins": function(d3) {
var h = d3.layout.histogram().bins([1]);
"returns the empty array with fewer than two bins": function(histogram) {
var h = histogram().bins([1]);
assert.deepEqual(h([0]), []);
var h = d3.layout.histogram().bins([]);
var h = histogram().bins([]);
assert.deepEqual(h([0]), []);
}
}
......
......@@ -6,25 +6,25 @@ var suite = vows.describe("d3.layout.pack");
suite.addBatch({
"pack": {
topic: load("layout/pack"),
"can handle an empty children array": function(d3) {
var pack = d3.layout.pack();
assert.deepEqual(pack.nodes({children: [{children: []}, {value: 1}]}).map(layout), [
topic: load("layout/pack").expression("d3.layout.pack"),
"can handle an empty children array": function(pack) {
var p = pack();
assert.deepEqual(p.nodes({children: [{children: []}, {value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 0.5},
{value: 0, depth: 1, x: 0.0, y: 0.5, r: 0.0},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 0.5}
]);
},
"can handle zero-valued nodes": function(d3) {
var pack = d3.layout.pack();
assert.deepEqual(pack.nodes({children: [{value: 0}, {value: 1}]}).map(layout), [
"can handle zero-valued nodes": function(pack) {
var p = pack();
assert.deepEqual(p.nodes({children: [{value: 0}, {value: 1}]}).map(layout), [
{value: 1, depth: 0, x: 0.5, y: 0.5, r: 0.5},
{value: 0, depth: 1, x: 0.0, y: 0.5, r: 0.0},
{value: 1, depth: 1, x: 0.5, y: 0.5, r: 0.5}
]);
},
"can handle small nodes": function(d3) {
assert.deepEqual(d3.layout.pack().sort(null).nodes({children: [
"can handle small nodes": function(pack) {
assert.deepEqual(pack().sort(null).nodes({children: [
{value: .01},
{value: 2},
{value: 2},
......@@ -36,7 +36,7 @@ suite.addBatch({
{y: 0.34256315498862167, x: 0.2832340573116551, value: 2, r: 0.23209360948886723, depth: 1},
{y: 0.7254154893606051, x: 0.38524055061025186, value: 1, r: 0.16411496513964044, depth: 1}
]);
assert.deepEqual(d3.layout.pack().sort(null).nodes({children: [
assert.deepEqual(pack().sort(null).nodes({children: [
{value: 2},
{value: 2},
{value: 1},
......@@ -49,9 +49,9 @@ suite.addBatch({
{y: 0.3878967195987758, x: 0.3386645534068854, value: 0.01, r: 0.01652869779539461, depth: 1}
]);
},
"can handle residual floating point error": function(d3) {
var pack = d3.layout.pack();
var result = pack.nodes({children: [
"can handle residual floating point error": function(pack) {
var p = pack();
var result = p.nodes({children: [
{value: 0.005348322447389364},
{value: 0.8065882022492588},
{value: 0}
......@@ -62,9 +62,9 @@ suite.addBatch({
assert.isFalse(result.map(function(d) { return d.y; }).some(isNaN));
assert.isFalse(result.map(function(d) { return d.r; }).some(isNaN));
},
"avoids coincident circles": function(d3) {
var pack = d3.layout.pack();
var result = pack({children: [
"avoids coincident circles": function(pack) {
var p = pack();
var result = p({children: [
{children: [{value: 17010}, {value: 5842}, {value: 0}, {value: 0}]},
{children: [
{children: [{value: 721}, {value: 4294}, {value: 9800}, {value: 1314}, {value: 2220}]},
......
......@@ -6,9 +6,9 @@ var suite = vows.describe("d3.layout.partition");
suite.addBatch({
"partition": {
topic: load("layout/partition"),
"ignores zero values": function(d3) {
var p = d3.layout.partition().size([3, 3]);
topic: load("layout/partition").expression("d3.layout.partition"),
"ignores zero values": function(partition) {
var p = partition().size([3, 3]);
assert.deepEqual(p.nodes({children: [{value: 1}, {value: 0}, {value: 2}, {children: [{value: 0}, {value: 0}]}]}).map(metadata), [
{x: 0, y: 0, dx: 3, dy: 1},
{x: 2, y: 1, dx: 1, dy: 1},
......@@ -19,8 +19,8 @@ suite.addBatch({
{x: 3, y: 2, dx: 0, dy: 1}
]);
},
"can handle an empty children array": function(d3) {
var p = d3.layout.partition();
"can handle an empty children array": function(partition) {
var p = partition();
assert.deepEqual(p.nodes({children: []}).map(metadata), [
{x: 0, y: 0, dx: 1, dy: 1}
]);
......
......@@ -6,13 +6,13 @@ var suite = vows.describe("d3.layout.pie");
suite.addBatch({
"pie": {
topic: load("layout/pie"),
"arcs are in same order as original data": function(d3) {
var pie = d3.layout.pie();
assert.deepEqual(pie([5, 30, 15]).map(function(d) { return d.data; }), [
topic: load("layout/pie").expression("d3.layout.pie"),
"arcs are in same order as original data": function(pie) {
var p = pie();
assert.deepEqual(p([5, 30, 15]).map(function(d) { return d.data; }), [
5, 30, 15
]);
assert.deepEqual(pie([
assert.deepEqual(p([
84, 90, 48, 61, 58, 8, 6, 31, 45, 18
]).map(function(d) { return d.data; }), [
84, 90, 48, 61, 58, 8, 6, 31, 45, 18
......
......@@ -6,13 +6,13 @@ var suite = vows.describe("d3.layout.tree");
suite.addBatch({
"tree": {
topic: load("layout/tree"),
"can handle an empty children array": function(d3) {
var tree = d3.layout.tree();
assert.deepEqual(tree.nodes({children: []}).map(layout), [
topic: load("layout/tree").expression("d3.layout.tree"),
"can handle an empty children array": function(tree) {
var t = tree();
assert.deepEqual(t.nodes({children: []}).map(layout), [
{depth: 0, x: 0.5, y: 0}
]);
assert.deepEqual(tree.nodes({children: [
assert.deepEqual(t.nodes({children: [
{children: []},
{children: [{}]},
{children: [{}]}
......@@ -25,9 +25,9 @@ suite.addBatch({
{depth: 2, x: .875, y: 1}
]);
},
"can handle a single node": function(d3) {
var tree = d3.layout.tree();
assert.deepEqual(tree.nodes({value: 0}).map(layout), [
"can handle a single node": function(tree) {
var t = tree();
assert.deepEqual(t.nodes({value: 0}).map(layout), [
{depth: 0, x: 0.5, y: 0}
]);
}
......
......@@ -6,9 +6,9 @@ var suite = vows.describe("d3.layout.treemap");
suite.addBatch({
"treemap": {
topic: load("layout/treemap"),
"outputs a squarified treemap": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null);
topic: load("layout/treemap").expression("d3.layout.treemap"),
"outputs a squarified treemap": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 0, y: 833, dx: 1000, dy: 167},
......@@ -18,8 +18,8 @@ suite.addBatch({
{x: 0, y: 0, dx: 600, dy: 555}
]);
},
"sorts by value by default": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]);
"sorts by value by default": function(treemap) {
var t = treemap().size([1000, 1000]);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 0, y: 0, dx: 333, dy: 500},
......@@ -29,8 +29,8 @@ suite.addBatch({
{x: 333, y: 500, dx: 667, dy: 500}
]);
},
"ignores zero values": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null);
"ignores zero values": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 0}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 0, y: 833, dx: 1000, dy: 167},
......@@ -41,8 +41,8 @@ suite.addBatch({
{x: 0, y: 0, dx: 600, dy: 555}
]);
},
"ignores NaN values": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null);
"ignores NaN values": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: NaN}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 0, y: 833, dx: 1000, dy: 167},
......@@ -53,8 +53,8 @@ suite.addBatch({
{x: 0, y: 0, dx: 600, dy: 555}
]);
},
"does not overflow empty size": function(d3) {
var t = d3.layout.treemap().size([0, 0]).sort(null);
"does not overflow empty size": function(treemap) {
var t = treemap().size([0, 0]).sort(null);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 0, dy: 0},
{x: 0, y: 0, dx: 0, dy: 0},
......@@ -64,8 +64,8 @@ suite.addBatch({
{x: 0, y: 0, dx: 0, dy: 0}
]);
},
"can specify padding as a number": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null).padding(1);
"can specify padding as a number": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null).padding(1);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 1, y: 833, dx: 998, dy: 166},
......@@ -75,8 +75,8 @@ suite.addBatch({
{x: 2, y: 2, dx: 597, dy: 553}
]);
},
"can specify padding as an array": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null).padding([1,2,3,4]);
"can specify padding as an array": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null).padding([1,2,3,4]);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 4, y: 831, dx: 994, dy: 166},
......@@ -86,8 +86,8 @@ suite.addBatch({
{x: 8, y: 2, dx: 590, dy: 551}
]);
},
"can specify padding as null": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null).padding(null);
"can specify padding as null": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null).padding(null);
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 0, y: 833, dx: 1000, dy: 167},
......@@ -97,8 +97,8 @@ suite.addBatch({
{x: 0, y: 0, dx: 600, dy: 555}
]);
},
"can specify padding as a function that returns a number": function(d3) {
var t = d3.layout.treemap().size([1000, 1000]).sort(null).padding(function(d) { return d.depth; });
"can specify padding as a function that returns a number": function(treemap) {
var t = treemap().size([1000, 1000]).sort(null).padding(function(d) { return d.depth; });
assert.deepEqual(t.nodes({children: [{value: 1}, {value: 2}, {children: [{value: 1}, {value: 2}]}]}).map(layout), [
{x: 0, y: 0, dx: 1000, dy: 1000},
{x: 0, y: 833, dx: 1000, dy: 167},
......@@ -108,8 +108,8 @@ suite.addBatch({
{x: 1, y: 1, dx: 598, dy: 554