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

Refactor scale tests for minimal load.

parent a6fa0c4f
......@@ -945,6 +945,7 @@ d3 = function() {
return !a - !b || comparator(a.__data__, b.__data__);
};
}
function d3_noop() {}
d3_selectionPrototype.on = function(type, listener, capture) {
var n = arguments.length;
if (n < 3) {
......@@ -1993,7 +1994,6 @@ d3 = function() {
};
}
d3.geo = {};
function d3_noop() {}
d3.geo.stream = function(object, listener) {
if (d3_geo_streamObjectType.hasOwnProperty(object.type)) {
d3_geo_streamObjectType[object.type](object, listener);
......
This source diff could not be displayed because it is too large. You can view the blob instead.
import "../arrays/range";
import "../core/rebind";
import "../interpolate/interpolate";
import "../interpolate/round";
import "../interpolate/uninterpolate";
import "../format/format";
import "bilinear";
......
......@@ -6,29 +6,29 @@ var suite = vows.describe("d3.random");
suite.addBatch({
"random": {
topic: load("math/random"),
topic: load("math/random").expression("d3.random"),
"normal": {
"topic": function(d3) {
return d3.random.normal();
"topic": function(random) {
return random.normal();
},
"returns a number": function(random) {
assert.typeOf(random(), "number");
"returns a number": function(r) {
assert.typeOf(r(), "number");
}
},
"logNormal": {
"topic": function(d3) {
return d3.random.logNormal();
"topic": function(random) {
return random.logNormal();
},
"returns a number": function(random) {
assert.typeOf(random(), "number");
"returns a number": function(r) {
assert.typeOf(r(), "number");
}
},
"irwinHall": {
"topic": function(d3) {
return d3.random.irwinHall(10);
"topic": function(random) {
return random.irwinHall(10);
},
"returns a number": function(random) {
assert.typeOf(random(), "number");
"returns a number": function(r) {
assert.typeOf(r(), "number");
}
}
}
......
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert");
......@@ -6,7 +7,7 @@ var suite = vows.describe("d3.scale.category");
suite.addBatch({
"category": {
topic: load("scale/category", "color/rgb", "arrays/range"),
topic: load("scale/category").expression("d3.scale"),
"category10": category("category10", 10),
"category20": category("category20", 20),
"category20b": category("category20b", 20),
......@@ -16,8 +17,8 @@ suite.addBatch({
function category(category, n) {
return {
"is an ordinal scale": function(d3) {
var x = d3.scale[category](), colors = x.range();
"is an ordinal scale": function(scale) {
var x = scale[category](), colors = x.range();
assert.lengthOf(x.domain(), 0);
assert.lengthOf(x.range(), n);
assert.equal(x(1), colors[0]);
......@@ -31,19 +32,19 @@ function category(category, n) {
assert.equal(y(1), colors[0]);
assert.equal(y(2), colors[1]);
},
"each instance is isolated": function(d3) {
var a = d3.scale[category](), b = d3.scale[category](), colors = a.range();
"each instance is isolated": function(scale) {
var a = scale[category](), b = scale[category](), colors = a.range();
assert.equal(a(1), colors[0]);
assert.equal(b(2), colors[0]);
assert.equal(b(1), colors[1]);
assert.equal(a(1), colors[0]);
},
"contains the expected number of values in the range": function(d3) {
var x = d3.scale[category]();
"contains the expected number of values in the range": function(scale) {
var x = scale[category]();
assert.lengthOf(x.range(), n);
},
"each range value is distinct": function(d3) {
var map = {}, count = 0, x = d3.scale[category]();
"each range value is distinct": function(scale) {
var map = {}, count = 0, x = scale[category]();
x.range().forEach(function(v) {
if (!(v in map)) {
map[v] = ++count;
......@@ -51,8 +52,8 @@ function category(category, n) {
});
assert.equal(count, x.range().length);
},
"each range value is a hexadecimal color": function(d3) {
var x = d3.scale[category]();
"each range value is a hexadecimal color": function(scale) {
var x = scale[category]();
x.range().forEach(function(v) {
assert.match(v, /#[0-9a-f]{6}/);
v = d3.rgb(v);
......@@ -61,8 +62,8 @@ function category(category, n) {
assert.isFalse(isNaN(v.b));
});
},
"no range values are very dark or very light": function(d3) {
var x = d3.scale[category]();
"no range values are very dark or very light": function(scale) {
var x = scale[category]();
x.range().forEach(function(v) {
var c = d3.hsl(v);
assert.isTrue(c.l >= .34, "expected " + v + " to be lighter (l = " + c.l + ")");
......
......@@ -6,131 +6,130 @@ var suite = vows.describe("d3.scale.identity");
suite.addBatch({
"identity": {
topic: load("scale/identity").sandbox({
document: null,
window: null
}),
topic: load("scale/identity")
.expression("d3.scale.identity")
.sandbox({document: null, window: null}),
"domain and range": {
"are identical": function(d3) {
var x = d3.scale.identity();
"are identical": function(identity) {
var x = identity();
assert.strictEqual(x.domain, x.range);
assert.strictEqual(x.domain(), x.range());
var x = d3.scale.identity().domain([-10, 0, 100]);
var x = identity().domain([-10, 0, 100]);
assert.deepEqual(x.range(), [-10, 0, 100]);
var x = d3.scale.identity().range([-10, 0, 100]);
var x = identity().range([-10, 0, 100]);
assert.deepEqual(x.domain(), [-10, 0, 100]);
},
"default to [0, 1]": function(d3) {
var x = d3.scale.identity();
"default to [0, 1]": function(identity) {
var x = identity();
assert.deepEqual(x.domain(), [0, 1]);
assert.deepEqual(x.range(), [0, 1]);
assert.strictEqual(x(.5), .5);
},
"coerce values to numbers": function(d3) {
var x = d3.scale.identity().domain([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
"coerce values to numbers": function(identity) {
var x = identity().domain([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
assert.typeOf(x.domain()[0], "number");
assert.typeOf(x.domain()[1], "number");
assert.strictEqual(x.domain()[0], +new Date(1990, 0, 1));
assert.strictEqual(x.domain()[1], +new Date(1991, 0, 1));
assert.typeOf(x(new Date(1989, 09, 20)), "number");
assert.strictEqual(x(new Date(1989, 09, 20)), +new Date(1989, 09, 20));
var x = d3.scale.identity().domain(["0", "1"]);
var x = identity().domain(["0", "1"]);
assert.typeOf(x.domain()[0], "number");
assert.typeOf(x.domain()[1], "number");
assert.strictEqual(x(.5), .5);
var x = d3.scale.identity().domain([new Number(0), new Number(1)]);
var x = identity().domain([new Number(0), new Number(1)]);
assert.typeOf(x.domain()[0], "number");
assert.typeOf(x.domain()[1], "number");
assert.strictEqual(x(.5), .5);
var x = d3.scale.identity().range([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
var x = identity().range([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
assert.typeOf(x.range()[0], "number");
assert.typeOf(x.range()[1], "number");
assert.strictEqual(x.range()[0], +new Date(1990, 0, 1));
assert.strictEqual(x.range()[1], +new Date(1991, 0, 1));
assert.typeOf(x(new Date(1989, 09, 20)), "number");
assert.strictEqual(x(new Date(1989, 09, 20)), +new Date(1989, 09, 20));
var x = d3.scale.identity().range(["0", "1"]);
var x = identity().range(["0", "1"]);
assert.typeOf(x.range()[0], "number");
assert.typeOf(x.range()[1], "number");
assert.strictEqual(x(.5), .5);
var x = d3.scale.identity().range([new Number(0), new Number(1)]);
var x = identity().range([new Number(0), new Number(1)]);
assert.typeOf(x.range()[0], "number");
assert.typeOf(x.range()[1], "number");
assert.strictEqual(x(.5), .5);
},
"can specify a polyidentity domain and range": function(d3) {
var x = d3.scale.identity().domain([-10, 0, 100]);
"can specify a polyidentity domain and range": function(identity) {
var x = identity().domain([-10, 0, 100]);
assert.deepEqual(x.domain(), [-10, 0, 100]);
assert.strictEqual(x(-5), -5);
assert.strictEqual(x(50), 50);
assert.strictEqual(x(75), 75);
var x = d3.scale.identity().range([-10, 0, 100]);
var x = identity().range([-10, 0, 100]);
assert.deepEqual(x.range(), [-10, 0, 100]);
assert.strictEqual(x(-5), -5);
assert.strictEqual(x(50), 50);
assert.strictEqual(x(75), 75);
},
"do not affect the identity function": function(d3) {
var x = d3.scale.identity().domain([Infinity, NaN]);
"do not affect the identity function": function(identity) {
var x = identity().domain([Infinity, NaN]);
assert.strictEqual(x(42), 42);
assert.strictEqual(x.invert(-42), -42);
}
},
"is the identity function": function(d3) {
var x = d3.scale.identity().domain([1, 2]);
"is the identity function": function(identity) {
var x = identity().domain([1, 2]);
assert.strictEqual(x(.5), .5);
assert.strictEqual(x(1), 1);
assert.strictEqual(x(1.5), 1.5);
assert.strictEqual(x(2), 2);
assert.strictEqual(x(2.5), 2.5);
},
"coerces input to a number": function(d3) {
var x = d3.scale.identity().domain([1, 2]);
"coerces input to a number": function(identity) {
var x = identity().domain([1, 2]);
assert.strictEqual(x("2"), 2);
},
"invert": {
"is the identity function": function(d3) {
var x = d3.scale.identity().domain([1, 2]);
"is the identity function": function(identity) {
var x = identity().domain([1, 2]);
assert.strictEqual(x.invert(.5), .5);
assert.strictEqual(x.invert(1), 1);
assert.strictEqual(x.invert(1.5), 1.5);
assert.strictEqual(x.invert(2), 2);
assert.strictEqual(x.invert(2.5), 2.5);
},
"coerces range value to numbers": function(d3) {
var x = d3.scale.identity().range(["0", "2"]);
"coerces range value to numbers": function(identity) {
var x = identity().range(["0", "2"]);
assert.strictEqual(x.invert("1"), 1);
var x = d3.scale.identity().range([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
var x = identity().range([new Date(1990, 0, 1), new Date(1991, 0, 1)]);
assert.strictEqual(x.invert(new Date(1990, 6, 2, 13)), +new Date(1990, 6, 2, 13));
var x = d3.scale.identity().range(["#000", "#fff"]);
var x = identity().range(["#000", "#fff"]);
assert.isNaN(x.invert("#999"));
},
"coerces input to a number": function(d3) {
var x = d3.scale.identity().domain([1, 2]);
"coerces input to a number": function(identity) {
var x = identity().domain([1, 2]);
assert.strictEqual(x.invert("2"), 2);
}
},
"ticks": {
"generates ticks of varying degree": function(d3) {
var x = d3.scale.identity();
"generates ticks of varying degree": function(identity) {
var x = identity();
assert.deepEqual(x.ticks(1).map(x.tickFormat(1)), [0, 1]);
assert.deepEqual(x.ticks(2).map(x.tickFormat(2)), [0, .5, 1]);
assert.deepEqual(x.ticks(5).map(x.tickFormat(5)), [0, .2, .4, .6, .8, 1]);
assert.deepEqual(x.ticks(10).map(x.tickFormat(10)), [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1]);
var x = d3.scale.identity().domain([1, 0]);
var x = identity().domain([1, 0]);
assert.deepEqual(x.ticks(1).map(x.tickFormat(1)), [0, 1]);
assert.deepEqual(x.ticks(2).map(x.tickFormat(2)), [0, .5, 1]);
assert.deepEqual(x.ticks(5).map(x.tickFormat(5)), [0, .2, .4, .6, .8, 1]);
assert.deepEqual(x.ticks(10).map(x.tickFormat(10)), [0, .1, .2, .3, .4, .5, .6, .7, .8, .9, 1]);
},
"formats ticks with the appropriate precision": function(d3) {
var x = d3.scale.identity().domain([.123456789, 1.23456789]);
"formats ticks with the appropriate precision": function(identity) {
var x = identity().domain([.123456789, 1.23456789]);
assert.strictEqual(x.tickFormat(1)(x.ticks(1)[0]), "1");
assert.strictEqual(x.tickFormat(2)(x.ticks(2)[0]), "0.5");
assert.strictEqual(x.tickFormat(4)(x.ticks(4)[0]), "0.2");
......@@ -144,15 +143,15 @@ suite.addBatch({
},
"copy": {
"changes to the domain or range are isolated": function(d3) {
var x = d3.scale.identity(), y = x.copy();
"changes to the domain or range are isolated": function(identity) {
var x = identity(), y = x.copy();
x.domain([1, 2]);
assert.deepEqual(y.domain(), [0, 1]);
y.domain([2, 3]);
assert.deepEqual(x.domain(), [1, 2]);
assert.deepEqual(y.domain(), [2, 3]);
var x = d3.scale.identity(), y = x.copy();
var x = identity(), y = x.copy();
x.range([1, 2]);
assert.deepEqual(y.range(), [0, 1]);
y.range([2, 3]);
......
......@@ -6,10 +6,8 @@ var suite = vows.describe("d3.scale.linear");
suite.addBatch({
"linear": {
topic: load("scale/linear").sandbox({
document: null,
window: null
}),
topic: load("scale/linear", "interpolate/hsl") // beware instanceof d3_Color
.sandbox({document: null, window: null}),
"domain": {
"defaults to [0, 1]": function(d3) {
......
......@@ -6,10 +6,8 @@ var suite = vows.describe("d3.scale.log");
suite.addBatch({
"log": {
topic: load("scale/log").sandbox({
document: null,
window: null
}),
topic: load("scale/log", "interpolate/hsl") // beware instanceof d3_Color
.sandbox({document: null, window: null}),
"domain": {
"defaults to [1, 10]": function(d3) {
......
......@@ -6,14 +6,14 @@ var suite = vows.describe("d3.scale.ordinal");
suite.addBatch({
"ordinal": {
topic: load("scale/ordinal"),
topic: load("scale/ordinal").expression("d3.scale.ordinal"),
"domain": {
"defaults to the empty array": function(d3) {
assert.isEmpty(d3.scale.ordinal().domain());
"defaults to the empty array": function(ordinal) {
assert.isEmpty(ordinal().domain());
},
"new input values are added to the domain": function(d3) {
var x = d3.scale.ordinal().range(["foo", "bar"]);
"new input values are added to the domain": function(ordinal) {
var x = ordinal().range(["foo", "bar"]);
assert.equal(x(0), "foo");
assert.deepEqual(x.domain(), ["0"]);
assert.equal(x(1), "bar");
......@@ -21,8 +21,8 @@ suite.addBatch({
assert.equal(x(0), "foo");
assert.deepEqual(x.domain(), ["0", "1"]);
},
"setting the domain forgets previous values": function(d3) {
var x = d3.scale.ordinal().range(["foo", "bar"]);
"setting the domain forgets previous values": function(ordinal) {
var x = ordinal().range(["foo", "bar"]);
assert.equal(x(1), "foo");
assert.equal(x(0), "bar");
assert.deepEqual(x.domain(), [1, 0]);
......@@ -31,14 +31,14 @@ suite.addBatch({
assert.equal(x(1), "bar");
assert.deepEqual(x.domain(), ["0", "1"]);
},
"uniqueness is based on string coercion": function(d3) {
var x = d3.scale.ordinal().domain(["foo"]).range([42, 43, 44]);
"uniqueness is based on string coercion": function(ordinal) {
var x = ordinal().domain(["foo"]).range([42, 43, 44]);
assert.equal(x(new String("foo")), 42);
assert.equal(x({toString: function() { return "foo"; }}), 42);
assert.equal(x({toString: function() { return "bar"; }}), 43);
},
"orders domain values by the order in which they are seen": function(d3) {
var x = d3.scale.ordinal();
"orders domain values by the order in which they are seen": function(ordinal) {
var x = ordinal();
x("foo");
x("bar");
x("baz");
......@@ -53,14 +53,14 @@ suite.addBatch({
x("bar");
assert.deepEqual(x.domain(), ["foo", "bar"]);
},
"does not coerce domain values to strings": function(d3) {
var x = d3.scale.ordinal().domain([0, 1]);
"does not coerce domain values to strings": function(ordinal) {
var x = ordinal().domain([0, 1]);
assert.deepEqual(x.domain(), [0, 1]);
assert.typeOf(x.domain()[0], "number");
assert.typeOf(x.domain()[1], "number");
},
"does not barf on object built-ins": function(d3) {
var x = d3.scale.ordinal().domain(["__proto__", "hasOwnProperty"]).range([42, 43]);
"does not barf on object built-ins": function(ordinal) {
var x = ordinal().domain(["__proto__", "hasOwnProperty"]).range([42, 43]);
assert.equal(x("__proto__"), 42);
assert.equal(x("hasOwnProperty"), 43);
assert.deepEqual(x.domain(), ["__proto__", "hasOwnProperty"]);
......@@ -68,21 +68,21 @@ suite.addBatch({
},
"range": {
"defaults to the empty array": function(d3) {
var x = d3.scale.ordinal();
"defaults to the empty array": function(ordinal) {
var x = ordinal();
assert.isEmpty(x.range());
assert.isUndefined(x(0));
},
"setting the range remembers previous values": function(d3) {
var x = d3.scale.ordinal();
"setting the range remembers previous values": function(ordinal) {
var x = ordinal();
assert.isUndefined(x(0));
assert.isUndefined(x(1));
x.range(["foo", "bar"]);
assert.equal(x(0), "foo");
assert.equal(x(1), "bar");
},
"recycles values when exhausted": function(d3) {
var x = d3.scale.ordinal().range(["a", "b", "c"]);
"recycles values when exhausted": function(ordinal) {
var x = ordinal().range(["a", "b", "c"]);
assert.equal(x(0), "a");
assert.equal(x(1), "b");
assert.equal(x(2), "c");
......@@ -95,8 +95,8 @@ suite.addBatch({
}
},
"maps distinct values to discrete values": function(d3) {
var x = d3.scale.ordinal().range(["a", "b", "c"]);
"maps distinct values to discrete values": function(ordinal) {
var x = ordinal().range(["a", "b", "c"]);
assert.equal(x(0), "a");
assert.equal(x("0"), "a");
assert.equal(x([0]), "a");
......@@ -106,117 +106,117 @@ suite.addBatch({
},
"rangePoints": {
"computes discrete points in a continuous range": function(d3) {
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([0, 120]);
"computes discrete points in a continuous range": function(ordinal) {
var x = ordinal().domain(["a", "b", "c"]).rangePoints([0, 120]);
assert.deepEqual(x.range(), [0, 60, 120]);
assert.equal(x.rangeBand(), 0);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([0, 120], 1);
var x = ordinal().domain(["a", "b", "c"]).rangePoints([0, 120], 1);
assert.deepEqual(x.range(), [20, 60, 100]);
assert.equal(x.rangeBand(), 0);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([0, 120], 2);
var x = ordinal().domain(["a", "b", "c"]).rangePoints([0, 120], 2);
assert.deepEqual(x.range(), [30, 60, 90]);
assert.equal(x.rangeBand(), 0);
},
"correctly handles singleton domains": function(d3) {
var x = d3.scale.ordinal().domain(["a"]).rangePoints([0, 120]);
"correctly handles singleton domains": function(ordinal) {
var x = ordinal().domain(["a"]).rangePoints([0, 120]);
assert.deepEqual(x.range(), [60]);
assert.equal(x.rangeBand(), 0);
},
"can be set to a descending range": function(d3) {
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([120, 0]);
"can be set to a descending range": function(ordinal) {
var x = ordinal().domain(["a", "b", "c"]).rangePoints([120, 0]);
assert.deepEqual(x.range(), [120, 60,0]);
assert.equal(x.rangeBand(), 0);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([120, 0], 1);
var x = ordinal().domain(["a", "b", "c"]).rangePoints([120, 0], 1);
assert.deepEqual(x.range(), [100, 60, 20]);
assert.equal(x.rangeBand(), 0);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangePoints([120, 0], 2);
var x = ordinal().domain(["a", "b", "c"]).rangePoints([120, 0], 2);
assert.deepEqual(x.range(), [90, 60, 30]);
assert.equal(x.rangeBand(), 0);
}
},
"rangeBands": {
"computes discrete bands in a continuous range": function(d3) {
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([0, 120]);
"computes discrete bands in a continuous range": function(ordinal) {
var x = ordinal().domain(["a", "b", "c"]).rangeBands([0, 120]);
assert.deepEqual(x.range(), [0, 40, 80]);
assert.equal(x.rangeBand(), 40);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([0, 120], .2);
var x = ordinal().domain(["a", "b", "c"]).rangeBands([0, 120], .2);
assert.deepEqual(x.range(), [7.5, 45, 82.5]);
assert.equal(x.rangeBand(), 30);
},
"setting domain recomputes range bands": function(d3) {
var x = d3.scale.ordinal().rangeRoundBands([0, 100]).domain(["a", "b", "c"]);
"setting domain recomputes range bands": function(ordinal) {
var x = ordinal().rangeRoundBands([0, 100]).domain(["a", "b", "c"]);
assert.deepEqual(x.range(), [1, 34, 67]);
assert.equal(x.rangeBand(), 33);
x.domain(["a", "b", "c", "d"]);
assert.deepEqual(x.range(), [0, 25, 50, 75]);
assert.equal(x.rangeBand(), 25);
},
"can be set to a descending range": function(d3) {
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([120, 0]);
"can be set to a descending range": function(ordinal) {
var x = ordinal().domain(["a", "b", "c"]).rangeBands([120, 0]);
assert.deepEqual(x.range(), [80, 40, 0]);
assert.equal(x.rangeBand(), 40);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([120, 0], .2);
var x = ordinal().domain(["a", "b", "c"]).rangeBands([120, 0], .2);
assert.deepEqual(x.range(), [82.5, 45, 7.5]);
assert.equal(x.rangeBand(), 30);
},
"can specify a different outer padding": function(d3) {
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([120, 0], .2, .1);
"can specify a different outer padding": function(ordinal) {
var x = ordinal().domain(["a", "b", "c"]).rangeBands([120, 0], .2, .1);
assert.deepEqual(x.range(), [84, 44, 4]);
assert.equal(x.rangeBand(), 32);
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeBands([120, 0], .2, 1);
var x = ordinal().domain(["a", "b", "c"]).rangeBands([120, 0], .2, 1);
assert.deepEqual(x.range(), [75, 50, 25]);
assert.equal(x.rangeBand(), 20);
}
},
"rangeRoundBands": {
"computes discrete rounded bands in a continuous range": function(d3) {
var x = d3.scale.ordinal().domain(["a", "b", "c"]).rangeRoundBands([0, 100]);
"computes discrete rounded bands in a continuous range": function(ordinal) {
var x = ordinal().domain(["a", "b", "c"]).rangeRoundBands([0, 100]);