Commit 3442685b authored by Mike Bostock's avatar Mike Bostock
Browse files

Refactor core tests using smash.

parent 04fe8f8e
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.functor");
suite.addBatch({
"functor": {
topic: function() {
return d3.functor;
},
"when passed a function, returns the function": function(functor) {
topic: load("core/functor"),
"when passed a function, returns the function": function(d3) {
function foo() {}
assert.strictEqual(functor(foo), foo);
assert.strictEqual(d3.functor(foo), foo);
},
"when passed a non-function, returns a wrapper function": function(functor) {
"when passed a non-function, returns a wrapper function": function(d3) {
var a = {};
assert.isNull(functor(null)());
assert.isUndefined(functor(undefined)());
assert.strictEqual(functor(a)(), a);
assert.strictEqual(functor(1)(), 1);
assert.deepEqual(functor([1])(), [1]);
assert.isNull(d3.functor(null)());
assert.isUndefined(d3.functor(undefined)());
assert.strictEqual(d3.functor(a)(), a);
assert.strictEqual(d3.functor(1)(), 1);
assert.deepEqual(d3.functor([1])(), [1]);
}
}
});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("ns");
suite.addBatch({
"prefix": {
topic: function() {
return d3.ns.prefix;
},
"svg is http://www.w3.org/2000/svg": function(prefix) {
assert.equal(prefix.svg, "http://www.w3.org/2000/svg");
},
"xhtml is http://www.w3.org/1999/xhtml": function(prefix) {
assert.equal(prefix.xhtml, "http://www.w3.org/1999/xhtml");
},
"xlink is http://www.w3.org/1999/xlink": function(prefix) {
assert.equal(prefix.xlink, "http://www.w3.org/1999/xlink");
},
"xml is http://www.w3.org/XML/1998/namespace": function(prefix) {
assert.equal(prefix.xml, "http://www.w3.org/XML/1998/namespace");
},
"xmlns is http://www.w3.org/2000/xmlns/": function(prefix) {
assert.equal(prefix.xmlns, "http://www.w3.org/2000/xmlns/");
}
}
});
suite.addBatch({
"qualify": {
topic: function() {
return d3.ns.qualify;
},
"local name returns name": function() {
assert.equal(d3.ns.qualify("local"), "local");
},
"known qualified name returns space and local": function() {
var name = d3.ns.qualify("svg:path");
assert.equal(name.space, "http://www.w3.org/2000/svg");
assert.equal(name.local, "path");
},
"unknown qualified name returns name": function() {
assert.equal(d3.ns.qualify("foo:bar"), "bar");
},
"known local name returns space and local": function() {
var name = d3.ns.qualify("svg");
assert.equal(name.space, "http://www.w3.org/2000/svg");
assert.equal(name.local, "svg");
},
"names that collide with built-ins are ignored": function(qualify) {
assert.equal(qualify("hasOwnProperty:test"), "test");
"ns": {
topic: load("core/ns"),
"prefix": {
"svg is http://www.w3.org/2000/svg": function(d3) {
assert.equal(d3.ns.prefix.svg, "http://www.w3.org/2000/svg");
},
"xhtml is http://www.w3.org/1999/xhtml": function(d3) {
assert.equal(d3.ns.prefix.xhtml, "http://www.w3.org/1999/xhtml");
},
"xlink is http://www.w3.org/1999/xlink": function(d3) {
assert.equal(d3.ns.prefix.xlink, "http://www.w3.org/1999/xlink");
},
"xml is http://www.w3.org/XML/1998/namespace": function(d3) {
assert.equal(d3.ns.prefix.xml, "http://www.w3.org/XML/1998/namespace");
},
"xmlns is http://www.w3.org/2000/xmlns/": function(d3) {
assert.equal(d3.ns.prefix.xmlns, "http://www.w3.org/2000/xmlns/");
}
},
"qualify": {
"local name returns name": function(d3) {
assert.equal(d3.ns.qualify("local"), "local");
},
"known qualified name returns space and local": function(d3) {
var name = d3.ns.qualify("svg:path");
assert.equal(name.space, "http://www.w3.org/2000/svg");
assert.equal(name.local, "path");
},
"unknown qualified name returns name": function(d3) {
assert.equal(d3.ns.qualify("foo:bar"), "bar");
},
"known local name returns space and local": function(d3) {
var name = d3.ns.qualify("svg");
assert.equal(name.space, "http://www.w3.org/2000/svg");
assert.equal(name.local, "svg");
},
"names that collide with built-ins are ignored": function(d3) {
assert.equal(d3.ns.qualify("hasOwnProperty:test"), "test");
}
}
}
});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.rebind");
suite.addBatch({
"rebind": {
topic: function() {
return d3.rebind;
},
"source function always has source as context": function(rebind) {
topic: load("core/rebind"),
"source function always has source as context": function(d3) {
var target = {}, source = {method: function() { that = this; }}, that;
rebind(target, source, "method");
d3.rebind(target, source, "method");
assert.strictEqual((target.method(), that), source);
assert.strictEqual((target.method.call({}), that), source);
},
"source function receives target function's arguments": function(rebind) {
"source function receives target function's arguments": function(d3) {
var target = {}, source = {method: function() { those = Array.prototype.slice.call(arguments); }}, those;
rebind(target, source, "method");
d3.rebind(target, source, "method");
assert.deepEqual((target.method(), those), []);
assert.deepEqual((target.method(1), those), [1]);
assert.deepEqual((target.method(null), those), [null]);
assert.deepEqual((target.method(source, source, 1), those), [source, source, 1]);
},
"target function returns target if source function returns source": function(rebind) {
"target function returns target if source function returns source": function(d3) {
var target = {}, source = {method: function(value) { return value ? source : 42; }};
rebind(target, source, "method");
d3.rebind(target, source, "method");
assert.strictEqual(target.method(true), target);
},
"otherwise, target function returns source function return value": function(rebind) {
"otherwise, target function returns source function return value": function(d3) {
var target = {}, source = {method: function(value) { return value ? source : 42; }};
rebind(target, source, "method");
d3.rebind(target, source, "method");
assert.strictEqual(target.method(false), 42);
},
"can bind multiple methods": function(rebind) {
"can bind multiple methods": function(d3) {
var target = {}, source = {
foo: function() { return 1; },
bar: function() { return 2; }
};
rebind(target, source, "foo", "bar");
d3.rebind(target, source, "foo", "bar");
assert.strictEqual(target.foo(), 1);
assert.strictEqual(target.bar(), 2);
},
"returns the target object": function(rebind) {
"returns the target object": function(d3) {
var target = {}, source = {foo: function() {}};
assert.strictEqual(rebind(target, source, "foo"), target);
assert.strictEqual(d3.rebind(target, source, "foo"), target);
}
}
});
......
require("../env");
var vows = require("vows"),
load = require("../load"),
assert = require("../env-assert");
var suite = vows.describe("d3.version");
suite.addBatch({
"semantic versioning": {
topic: d3.version,
"has the form major.minor.patch": function(version) {
assert.match(version, /^[0-9]+\.[0-9]+\.[0-9]+/);
"version": {
topic: load(),
"has the form major.minor.patch": function(d3) {
assert.match(d3.version, /^[0-9]+\.[0-9]+\.[0-9]+/);
}
}
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment