|
1 require("../env"); |
|
2 require("../../d3"); |
|
3 |
|
4 var vows = require("vows"), |
|
5 assert = require("assert"); |
|
6 |
|
7 var suite = vows.describe("d3.scale.category"); |
|
8 |
|
9 suite.addBatch({ |
|
10 "category10": category(d3.scale.category10, 10), |
|
11 "category20": category(d3.scale.category20, 20), |
|
12 "category20b": category(d3.scale.category20b, 20), |
|
13 "category20c": category(d3.scale.category20c, 20) |
|
14 }); |
|
15 |
|
16 function category(category, n) { |
|
17 return { |
|
18 "is an ordinal scale": function() { |
|
19 var x = category(), colors = x.range(); |
|
20 assert.length(x.domain(), 0); |
|
21 assert.length(x.range(), n); |
|
22 assert.equal(x(1), colors[0]); |
|
23 assert.equal(x(2), colors[1]); |
|
24 assert.equal(x(1), colors[0]); |
|
25 var y = x.copy(); |
|
26 assert.deepEqual(y.domain(), x.domain()); |
|
27 assert.deepEqual(y.range(), x.range()); |
|
28 x.domain(d3.range(n)); |
|
29 for (var i = 0; i < n; ++i) assert.equal(x(i + n), x(i)); |
|
30 assert.equal(y(1), colors[0]); |
|
31 assert.equal(y(2), colors[1]); |
|
32 }, |
|
33 "each instance is isolated": function() { |
|
34 var a = category(), b = category(), colors = a.range(); |
|
35 assert.equal(a(1), colors[0]); |
|
36 assert.equal(b(2), colors[0]); |
|
37 assert.equal(b(1), colors[1]); |
|
38 assert.equal(a(1), colors[0]); |
|
39 }, |
|
40 "contains the expected number of values in the range": function() { |
|
41 var x = category(); |
|
42 assert.length(x.range(), n); |
|
43 }, |
|
44 "each range value is distinct": function() { |
|
45 var map = {}, count = 0, x = category(); |
|
46 x.range().forEach(function(v) { |
|
47 if (!(v in map)) { |
|
48 map[v] = ++count; |
|
49 } |
|
50 }); |
|
51 assert.equal(count, x.range().length); |
|
52 }, |
|
53 "each range value is a hexadecimal color": function() { |
|
54 var x = category(); |
|
55 x.range().forEach(function(v) { |
|
56 assert.match(v, /#[0-9a-f]{6}/); |
|
57 v = d3.rgb(v); |
|
58 assert.isFalse(isNaN(v.r)); |
|
59 assert.isFalse(isNaN(v.g)); |
|
60 assert.isFalse(isNaN(v.b)); |
|
61 }); |
|
62 }, |
|
63 "no range values are very dark or very light": function() { |
|
64 var x = category(); |
|
65 x.range().forEach(function(v) { |
|
66 var c = d3.hsl(v); |
|
67 assert.isTrue(c.l >= .34, "expected " + v + " to be lighter (l = " + c.l + ")"); |
|
68 assert.isTrue(c.l <= .89, "expected " + v + " to be darker (l = " + c.l + ")"); |
|
69 }); |
|
70 } |
|
71 }; |
|
72 } |
|
73 |
|
74 suite.export(module); |