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