3
var vows = require("vows"),
4
assert = require("assert");
6
var suite = vows.describe("d3.svg.area");
14
"x is an alias for setting x0 and x1": function(area) {
17
assert.equal(a.x(), f);
18
assert.equal(a.x0(), f);
19
assert.equal(a.x1(), f);
21
"x is an alias for getting x1": function(area) {
24
assert.equal(a.x(), f);
27
"y is an alias for setting y0 and y1": function(area) {
30
assert.equal(a.y(), f);
31
assert.equal(a.y0(), f);
32
assert.equal(a.y1(), f);
34
"y is an alias for getting x1": function(area) {
37
assert.equal(a.y(), f);
40
"x0 defaults to a function accessor": function(area) {
42
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
43
assert.typeOf(a.x0(), "function");
45
"x0 can be defined as a constant": function(area) {
47
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L0,0L0,0Z");
48
assert.equal(a.x0(), 0);
50
"x0 can be defined as a function": function(area) {
51
var a = area().x0(f), t = {}, dd = [], ii = [], tt = [];
52
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; }
53
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,2L4,3L0,0L0,0Z");
54
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
55
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
56
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
59
"x1 defaults to a function accessor": function(area) {
61
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
62
assert.typeOf(a.x1(), "function");
64
"x1 can be defined as a constant": function(area) {
66
assert.pathEqual(a([[1, 2], [4, 3]]), "M0,2L0,3L4,0L1,0Z");
67
assert.equal(a.x1(), 0);
69
"x1 can be defined as a function": function(area) {
70
var a = area().x1(f), t = {}, dd = [], ii = [], tt = [];
71
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; }
72
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M0,2L0,3L4,0L1,0Z");
73
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
74
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
75
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
78
"y0 defaults to zero": function(area) {
80
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
81
assert.equal(a.y0(), 0);
83
"y0 can be defined as a constant": function(area) {
85
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,1L1,1Z");
86
assert.equal(a.y0(), 1);
88
"y0 can be defined as a function": function(area) {
89
var a = area().y0(f), t = {}, dd = [], ii = [], tt = [];
90
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 1; }
91
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,2L4,3L4,1L1,1Z");
92
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
93
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
94
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
97
"y1 defaults to a function accessor": function(area) {
99
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
100
assert.typeOf(a.y1(), "function");
102
"y1 can be defined as a constant": function(area) {
103
var a = area().y1(1);
104
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,1L4,1L4,0L1,0Z");
105
assert.equal(a.y1(), 1);
107
"y1 can be defined as a function": function(area) {
108
var a = area().y1(f), t = {}, dd = [], ii = [], tt = [];
109
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 1; }
110
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,1L4,1L4,0L1,0Z");
111
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
112
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
113
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
116
"if x0 === x1, x is only evaluated once per point": function(area) {
117
var a = area().x(f), t = {}, dd = [], ii = [], tt = [];
118
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; }
119
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M0,2L0,3L0,0L0,0Z");
120
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
121
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
122
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
124
"if y0 === y1, y is only evaluated once per point": function(area) {
125
var a = area().y(f), t = {}, dd = [], ii = [], tt = [];
126
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 1; }
127
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,1L4,1L4,1L1,1Z");
128
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
129
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
130
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
133
"interpolate defaults to linear": function(area) {
134
assert.equal(area().interpolate(), "linear");
136
"interpolate can be defined as a constant": function(area) {
137
var a = area().interpolate("step-before");
138
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0V1H1L1,0H0V0Z");
139
assert.equal(a.interpolate(), "step-before");
141
"invalid interpolates fallback to linear": function(area) {
142
assert.equal(area().interpolate("__proto__").interpolate(), "linear");
145
"tension defaults to .7": function(area) {
146
assert.equal(area().tension(), .7);
148
"tension can be specified as a constant": function(area) {
149
var a = area().tension(.5);
150
assert.equal(a.tension(), .5);
153
"returns null if input points array is empty": function(area) {
154
assert.isNull(area()([]));
157
"interpolate(linear)": {
158
"supports linear interpolation": testInterpolation("linear")
161
"interpolate(step)": {
162
"supports step-before interpolation": testInterpolation("step-before", "step-after"),
163
"supports step-after interpolation": testInterpolation("step-after", "step-before")
166
"interpolate(basis)": {
167
"supports basis interpolation": testInterpolation("basis"),
168
"supports basis-open interpolation": testInterpolation("basis-open")
171
"interpolate(cardinal)": {
172
"supports cardinal interpolation": testInterpolation("cardinal"),
173
"supports cardinal-open interpolation": testInterpolation("cardinal-open")
176
"interpolate(monotone)": {
177
"supports monotone interpolation": testInterpolation("monotone")
182
// An area is just two lines, with one reversed.
183
function testInterpolation(i0, i1) {
184
if (arguments.length < 2) i1 = i0;
185
return function(area) {
186
var a = area().interpolate(i0),
187
d = [[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]],
188
l0 = d3.svg.line().interpolate(i1).x(a.x0()).y(a.y0()),
189
l1 = d3.svg.line().interpolate(i0).x(a.x1()).y(a.y1());
190
assert.pathEqual(a(d), l1(d) + "L" + l0(d.reverse()).substring(1) + "Z");
194
suite.export(module);