Skip to content

Commit ff083fd

Browse files
committed
Better tests for svg.area and svg.line.radial.
1 parent b5a857f commit ff083fd

2 files changed

Lines changed: 214 additions & 78 deletions

File tree

test/svg/area-test.js

Lines changed: 172 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,85 +11,181 @@ suite.addBatch({
1111
topic: function() {
1212
return d3.svg.area;
1313
},
14-
"default accessors assume tuple input": function(area) {
14+
15+
"x is an alias for setting x0 and x1": function(area) {
16+
var a = area().x(f);
17+
function f() {}
18+
assert.equal(a.x(), f);
19+
assert.equal(a.x0(), f);
20+
assert.equal(a.x1(), f);
21+
},
22+
"x is an alias for getting x1": function(area) {
23+
var a = area().x1(f);
24+
function f() {}
25+
assert.equal(a.x(), f);
26+
},
27+
28+
"y is an alias for setting y0 and y1": function(area) {
29+
var a = area().y(f);
30+
function f() {}
31+
assert.equal(a.y(), f);
32+
assert.equal(a.y0(), f);
33+
assert.equal(a.y1(), f);
34+
},
35+
"y is an alias for getting x1": function(area) {
36+
var a = area().y1(f);
37+
function f() {}
38+
assert.equal(a.y(), f);
39+
},
40+
41+
"x0 defaults to a function accessor": function(area) {
42+
var a = area();
43+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
44+
assert.typeOf(a.x0(), "function");
45+
},
46+
"x0 can be defined as a constant": function(area) {
47+
var a = area().x0(0);
48+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L0,0L0,0Z");
49+
assert.equal(a.x0(), 0);
50+
},
51+
"x0 can be defined as a function": function(area) {
52+
var a = area().x0(f), t = {}, dd = [], ii = [], tt = [];
53+
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; }
54+
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,2L4,3L0,0L0,0Z");
55+
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
56+
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
57+
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
58+
},
59+
60+
"x1 defaults to a function accessor": function(area) {
1561
var a = area();
16-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
17-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0L1,1L1,0L0,0Z");
18-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0L1,1L2,0L2,0L1,0L0,0Z");
19-
},
20-
"can specify x-accessor as a function": function(area) {
21-
var i = 0, a = area().x(function() { return ++i; });
22-
assert.pathEqual(a([[0, 0]]), "M1,0L1,0Z");
23-
assert.pathEqual(a([[0, 0], [1, 1]]), "M2,0L3,1L3,0L2,0Z");
24-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M4,0L5,1L6,0L6,0L5,0L4,0Z");
25-
},
26-
"can specify y-accessor as a function": function(area) {
27-
var i = 0, a = area().y(function() { return ++i; });
28-
assert.pathEqual(a([[0, 0]]), "M0,1L0,1Z");
29-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,2L1,3L1,3L0,2Z");
30-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,4L1,5L2,6L2,6L1,5L0,4Z");
31-
},
32-
"can specify y0-accessor as a constant": function(area) {
33-
var a = area().y0(-1);
34-
assert.pathEqual(a([[0, 0]]), "M0,0L0,-1Z");
35-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0L1,1L1,-1L0,-1Z");
36-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0L1,1L2,0L2,-1L1,-1L0,-1Z");
37-
},
38-
"can specify y0-accessor as a function": function(area) {
39-
var a = area().x(function(d) { return d.x; }).y0(function(d) { return -d.y; }).y1(function(d) { return d.y; });
40-
assert.pathEqual(a([{x:0,y:0}]), "M0,0L0,0Z");
41-
assert.pathEqual(a([{x:0,y:0},{x:1,y:1}]), "M0,0L1,1L1,-1L0,0Z");
42-
assert.pathEqual(a([{x:0,y:0},{x:1,y:1},{x:2,y:0}]), "M0,0L1,1L2,0L2,0L1,-1L0,0Z");
43-
},
44-
"can specify y1-accessor as a function": function(area) {
45-
var a = area().x(function(d) { return d.x; }).y1(function(d) { return d.y; });
46-
assert.pathEqual(a([{x:0,y:0}]), "M0,0L0,0Z");
47-
assert.pathEqual(a([{x:0,y:0},{x:1,y:1}]), "M0,0L1,1L1,0L0,0Z");
48-
assert.pathEqual(a([{x:0,y:0},{x:1,y:1},{x:2,y:0}]), "M0,0L1,1L2,0L2,0L1,0L0,0Z");
49-
},
50-
"supports step-before interpolation": function(area) {
51-
var a = area().interpolate("step-before");
52-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
53-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0V1H1L1,0V0H0Z");
54-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0V1H1V0H2L2,0V0H1V0H0Z");
55-
},
56-
"supports step-after interpolation": function(area) {
57-
var a = area().interpolate("step-after");
58-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
59-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0H1V1L1,0H0V0Z");
60-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0H1V1H2V0L2,0H1V0H0V0Z");
61-
},
62-
"supports basis interpolation": function(area) {
63-
var a = area().interpolate("basis");
64-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
65-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0L1,1L1,0L0,0Z");
66-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0C0,0,0,0,0.16666666666666666,0.16666666666666666C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,1.8333333333333333,0.16666666666666666C2,0,2,0,1.9999999999999998,0L2,0C2,0,2,0,1.8333333333333333,0C1.6666666666666665,0,1.3333333333333333,0,1,0C0.6666666666666666,0,0.3333333333333333,0,0.16666666666666666,0C0,0,0,0,0,0Z");
67-
},
68-
"supports basis-closed interpolation": function(area) {
69-
var a = area().interpolate("basis-closed");
70-
assert.pathEqual(a([[0, 0]]), "M0,0C0,0,0,0,0,0L0,0C0,0,0,0,0,0Z");
71-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0.3333333333333333,0.3333333333333333C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,0.6666666666666666,0.6666666666666666C0.6666666666666666,0.6666666666666666,0.3333333333333333,0.3333333333333333,0.3333333333333333,0.3333333333333333L0.6666666666666666,0C0.6666666666666666,0,0.3333333333333333,0,0.3333333333333333,0C0.3333333333333333,0,0.6666666666666666,0,0.6666666666666666,0Z");
72-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M1.5,0.16666666666666666C1.3333333333333333,0,0.6666666666666666,0,0.5,0.16666666666666666C0.3333333333333333,0.3333333333333333,0.6666666666666666,0.6666666666666666,1,0.6666666666666666C1.3333333333333333,0.6666666666666666,1.6666666666666665,0.3333333333333333,1.5,0.16666666666666666L0.5,0C0.6666666666666666,0,1.3333333333333333,0,1.5,0C1.6666666666666665,0,1.3333333333333333,0,1,0C0.6666666666666666,0,0.3333333333333333,0,0.5,0Z");
73-
},
74-
"supports cardinal interpolation": function(area) {
75-
var a = area().interpolate("cardinal");
76-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
77-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0L1,1L1,0L0,0Z");
78-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0Q0.7999999999999999,1,1,1Q1.2,1,2,0L2,0Q1.2,0,1,0Q0.7999999999999999,0,0,0Z");
79-
},
80-
"supports cardinal-closed interpolation": function(area) {
81-
var a = area().interpolate("cardinal-closed");
82-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
83-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0L1,1L1,0L0,0Z");
84-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0C-0.15000000000000002,0.15000000000000002,0.7,1,1,1S2.15,0.15000000000000002,2,0S0.15000000000000002,-0.15000000000000002,0,0L2,0C2.15,0,1.3,0,1,0S-0.15000000000000002,0,0,0S1.85,0,2,0Z");
85-
},
86-
"supports monotone interpolation": function(area) {
87-
var a = area().interpolate("monotone");
88-
assert.pathEqual(a([[0, 0]]), "M0,0L0,0Z");
89-
assert.pathEqual(a([[0, 0], [1, 1]]), "M0,0L1,1L1,0L0,0Z");
90-
assert.pathEqual(a([[0, 0], [1, 1], [2, 0]]), "M0,0C0.08333333333333333,0.08333333333333333,0.6666666666666667,1,1,1S1.9166666666666667,0.08333333333333333,2,0L2,0C1.8333333333333333,0,1.3333333333333333,0,1,0S0.16666666666666666,0,0,0Z");
62+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
63+
assert.typeOf(a.x1(), "function");
64+
},
65+
"x1 can be defined as a constant": function(area) {
66+
var a = area().x1(0);
67+
assert.pathEqual(a([[1, 2], [4, 3]]), "M0,2L0,3L4,0L1,0Z");
68+
assert.equal(a.x1(), 0);
69+
},
70+
"x1 can be defined as a function": function(area) {
71+
var a = area().x1(f), t = {}, dd = [], ii = [], tt = [];
72+
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; }
73+
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M0,2L0,3L4,0L1,0Z");
74+
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
75+
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
76+
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
77+
},
78+
79+
"y0 defaults to zero": function(area) {
80+
var a = area();
81+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
82+
assert.equal(a.y0(), 0);
83+
},
84+
"y0 can be defined as a constant": function(area) {
85+
var a = area().y0(1);
86+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,1L1,1Z");
87+
assert.equal(a.y0(), 1);
88+
},
89+
"y0 can be defined as a function": function(area) {
90+
var a = area().y0(f), t = {}, dd = [], ii = [], tt = [];
91+
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 1; }
92+
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,2L4,3L4,1L1,1Z");
93+
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
94+
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
95+
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
96+
},
97+
98+
"y1 defaults to a function accessor": function(area) {
99+
var a = area();
100+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,2L4,3L4,0L1,0Z");
101+
assert.typeOf(a.y1(), "function");
102+
},
103+
"y1 can be defined as a constant": function(area) {
104+
var a = area().y1(1);
105+
assert.pathEqual(a([[1, 2], [4, 3]]), "M1,1L4,1L4,0L1,0Z");
106+
assert.equal(a.y1(), 1);
107+
},
108+
"y1 can be defined as a function": function(area) {
109+
var a = area().y1(f), t = {}, dd = [], ii = [], tt = [];
110+
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 1; }
111+
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,1L4,1L4,0L1,0Z");
112+
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
113+
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
114+
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
115+
},
116+
117+
"if x0 === x1, x is only evaluated once per point": function(area) {
118+
var a = area().x(f), t = {}, dd = [], ii = [], tt = [];
119+
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 0; }
120+
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M0,2L0,3L0,0L0,0Z");
121+
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
122+
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
123+
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
124+
},
125+
"if y0 === y1, y is only evaluated once per point": function(area) {
126+
var a = area().y(f), t = {}, dd = [], ii = [], tt = [];
127+
function f(d, i) { dd.push(d); ii.push(i); tt.push(this); return 1; }
128+
assert.pathEqual(a.call(t, [[1, 2], [4, 3]]), "M1,1L4,1L4,1L1,1Z");
129+
assert.deepEqual(dd, [[1, 2], [4, 3]], "expected data, got {actual}");
130+
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
131+
assert.deepEqual(tt, [t, t], "expected this, got {actual}");
132+
},
133+
134+
"interpolate defaults to linear": function(area) {
135+
assert.equal(area().interpolate(), "linear");
136+
},
137+
"interpolate can be defined as a constant": function(area) {
138+
var l = area().interpolate("step-before");
139+
assert.pathEqual(l([[0, 0], [1, 1]]), "M0,0V1H1L1,0V0H0Z");
140+
assert.equal(l.interpolate(), "step-before");
141+
},
142+
143+
"tension defaults to .7": function(area) {
144+
assert.equal(area().tension(), .7);
145+
},
146+
"tension can be specified as a constant": function(area) {
147+
var l = area().tension(.5);
148+
assert.equal(l.tension(), .5);
149+
},
150+
151+
"returns null if input points array is empty": function(area) {
152+
assert.isNull(area()([]));
153+
},
154+
155+
"interpolate(linear)": {
156+
"supports linear interpolation": testInterpolation("linear")
157+
},
158+
159+
"interpolate(step)": {
160+
"supports step-before interpolation": testInterpolation("step-before"),
161+
"supports step-after interpolation": testInterpolation("step-after")
162+
},
163+
164+
"interpolate(basis)": {
165+
"supports basis interpolation": testInterpolation("basis"),
166+
"supports basis-open interpolation": testInterpolation("basis-open")
167+
},
168+
169+
"interpolate(cardinal)": {
170+
"supports cardinal interpolation": testInterpolation("cardinal"),
171+
"supports cardinal-open interpolation": testInterpolation("cardinal-open")
172+
},
173+
174+
"interpolate(monotone)": {
175+
"supports monotone interpolation": testInterpolation("monotone")
91176
}
92177
}
93178
});
94179

180+
// An area is just two lines, with one reversed.
181+
function testInterpolation(interpolate) {
182+
return function(area) {
183+
var a = area().interpolate(interpolate),
184+
d = [[0, 0], [1, 1], [2, 0], [3, 1], [4, 0]],
185+
l0 = d3.svg.line().interpolate(interpolate).x(a.x0()).y(a.y0()),
186+
l1 = d3.svg.line().interpolate(interpolate).x(a.x1()).y(a.y1());
187+
assert.pathEqual(a(d), l1(d) + "L" + l0(d.reverse()).substring(1) + "Z");
188+
};
189+
}
190+
95191
suite.export(module);

test/svg/line-radial-test.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ suite.addBatch({
6464
assert.equal(l.interpolate(), "cardinal");
6565
},
6666

67-
// We could test the other interpolation modes here, but that'd be redundant.
68-
6967
"tension defaults to .7": function(line) {
7068
assert.equal(line().tension(), .7);
7169
},
@@ -76,8 +74,50 @@ suite.addBatch({
7674

7775
"returns null if input points array is empty": function(line) {
7876
assert.isNull(line()([]));
77+
},
78+
79+
"interpolate(linear)": {
80+
"supports linear interpolation": testInterpolation("linear")
81+
},
82+
83+
"interpolate(step)": {
84+
"supports step-before interpolation": testInterpolation("step-before"),
85+
"supports step-after interpolation": testInterpolation("step-after")
86+
},
87+
88+
"interpolate(basis)": {
89+
"supports basis interpolation": testInterpolation("basis"),
90+
"supports basis-open interpolation": testInterpolation("basis-open"),
91+
"supports basis-closed interpolation": testInterpolation("basis-closed")
92+
},
93+
94+
"interpolate(bundle)": {
95+
"supports bundle interpolation": testInterpolation("bundle")
96+
},
97+
98+
"interpolate(cardinal)": {
99+
"supports cardinal interpolation": testInterpolation("cardinal"),
100+
"supports cardinal-open interpolation": testInterpolation("cardinal-open"),
101+
"supports cardinal-closed interpolation": testInterpolation("cardinal-closed")
102+
},
103+
104+
"interpolate(monotone)": {
105+
"supports monotone interpolation": testInterpolation("monotone")
79106
}
80107
}
81108
});
82109

110+
// A radial line is just a transformation of a Cartesian line.
111+
function testInterpolation(interpolate) {
112+
var data = [[10, 0], [20, 1], [20, 2], [10, 3]];
113+
114+
var cartesian = d3.svg.line()
115+
.x(function(d) { return d[0] * Math.cos(d[1] - Math.PI / 2); })
116+
.y(function(d) { return d[0] * Math.sin(d[1] - Math.PI / 2); });
117+
118+
return function(line) {
119+
assert.pathEqual(line().interpolate(interpolate)(data), cartesian.interpolate(interpolate)(data));
120+
};
121+
}
122+
83123
suite.export(module);

0 commit comments

Comments
 (0)