File: symbol-test.js

package info (click to toggle)
node-d3-shape 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,388 kB
  • sloc: makefile: 11; sh: 2
file content (142 lines) | stat: -rw-r--r-- 6,410 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
var tape = require("tape"),
    shape = require("../"),
    polygonContext = require("./polygonContext");

require("./inDelta");
require("./pathEqual");

tape("symbol() returns a default symbol shape", function(test) {
  var s = shape.symbol();
  test.equal(s.type()(), shape.symbolCircle);
  test.equal(s.size()(), 64);
  test.equal(s.context(), null);
  test.pathEqual(s(), "M4.513517,0A4.513517,4.513517,0,1,1,-4.513517,0A4.513517,4.513517,0,1,1,4.513517,0");
  test.end();
});

tape("symbol().size(f)(…) propagates the context and arguments to the specified function", function(test) {
  var expected = {that: {}, args: [42]}, actual;
  shape.symbol().size(function() { actual = {that: this, args: [].slice.call(arguments)}; return 64; }).apply(expected.that, expected.args);
  test.deepEqual(actual, expected);
  test.end();
});

tape("symbol().type(f)(…) propagates the context and arguments to the specified function", function(test) {
  var expected = {that: {}, args: [42]}, actual;
  shape.symbol().type(function() { actual = {that: this, args: [].slice.call(arguments)}; return shape.symbolCircle; }).apply(expected.that, expected.args);
  test.deepEqual(actual, expected);
  test.end();
});

tape("symbol.size(size) observes the specified size function", function(test) {
  var size = function(d, i) { return d.z * 2 + i; },
      s = shape.symbol().size(size);
  test.equal(s.size(), size);
  test.pathEqual(s({z: 0}, 0), "M0,0");
  test.pathEqual(s({z: Math.PI / 2}, 0), "M1,0A1,1,0,1,1,-1,0A1,1,0,1,1,1,0");
  test.pathEqual(s({z: 2 * Math.PI}, 0), "M2,0A2,2,0,1,1,-2,0A2,2,0,1,1,2,0");
  test.pathEqual(s({z: Math.PI}, 1), "M1.522600,0A1.522600,1.522600,0,1,1,-1.522600,0A1.522600,1.522600,0,1,1,1.522600,0");
  test.pathEqual(s({z: 4 * Math.PI}, 2), "M2.938813,0A2.938813,2.938813,0,1,1,-2.938813,0A2.938813,2.938813,0,1,1,2.938813,0");
  test.end();
});

tape("symbol.size(size) observes the specified size constant", function(test) {
  var s = shape.symbol();
  test.equal(s.size(42).size()(), 42);
  test.pathEqual(s.size(0)(), "M0,0");
  test.pathEqual(s.size(Math.PI)(), "M1,0A1,1,0,1,1,-1,0A1,1,0,1,1,1,0");
  test.pathEqual(s.size(4 * Math.PI)(), "M2,0A2,2,0,1,1,-2,0A2,2,0,1,1,2,0");
  test.end();
});

tape("symbol.type(symbolCircle) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolCircle).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0");
  test.pathEqual(s(20), "M2.523133,0A2.523133,2.523133,0,1,1,-2.523133,0A2.523133,2.523133,0,1,1,2.523133,0");
  test.end();
});

tape("symbol.type(symbolCross) generates a polygon with the specified size", function(test) {
  var p = polygonContext(), s = shape.symbol().type(shape.symbolCross).context(p);
  s.size(1)(); test.inDelta(p.area(), 1);
  s.size(240)(); test.inDelta(p.area(), 240);
  test.end();
});

tape("symbol.type(symbolCross) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolCross).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0Z");
  test.pathEqual(s(20), "M-3,-1L-1,-1L-1,-3L1,-3L1,-1L3,-1L3,1L1,1L1,3L-1,3L-1,1L-3,1Z");
  test.end();
});

tape("symbol.type(symbolDiamond) generates a polygon with the specified size", function(test) {
  var p = polygonContext(), s = shape.symbol().type(shape.symbolDiamond).context(p);
  s.size(1)(); test.inDelta(p.area(), 1);
  s.size(240)(); test.inDelta(p.area(), 240);
  test.end();
});

tape("symbol.type(symbolDiamond) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolDiamond).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0L0,0L0,0L0,0Z");
  test.pathEqual(s(10), "M0,-2.942831L1.699044,0L0,2.942831L-1.699044,0Z");
  test.end();
});

tape("symbol.type(symbolStar) generates a polygon with the specified size", function(test) {
  var p = polygonContext(), s = shape.symbol().type(shape.symbolStar).context(p);
  s.size(1)(); test.inDelta(p.area(), 1);
  s.size(240)(); test.inDelta(p.area(), 240);
  test.end();
});

tape("symbol.type(symbolStar) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolStar).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0Z");
  test.pathEqual(s(10), "M0,-2.984649L0.670095,-0.922307L2.838570,-0.922307L1.084237,0.352290L1.754333,2.414632L0,1.140035L-1.754333,2.414632L-1.084237,0.352290L-2.838570,-0.922307L-0.670095,-0.922307Z");
  test.end();
});

tape("symbol.type(symbolSquare) generates a polygon with the specified size", function(test) {
  var p = polygonContext(), s = shape.symbol().type(shape.symbolSquare).context(p);
  s.size(1)(); test.inDelta(p.area(), 1);
  s.size(240)(); test.inDelta(p.area(), 240);
  test.end();
});

tape("symbol.type(symbolSquare) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolSquare).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0h0v0h0Z");
  test.pathEqual(s(4), "M-1,-1h2v2h-2Z");
  test.pathEqual(s(16), "M-2,-2h4v4h-4Z");
  test.end();
});

tape("symbol.type(symbolTriangle) generates a polygon with the specified size", function(test) {
  var p = polygonContext(), s = shape.symbol().type(shape.symbolTriangle).context(p);
  s.size(1)(); test.inDelta(p.area(), 1);
  s.size(240)(); test.inDelta(p.area(), 240);
  test.end();
});

tape("symbol.type(symbolTriangle) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolTriangle).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0L0,0L0,0Z");
  test.pathEqual(s(10), "M0,-2.774528L2.402811,1.387264L-2.402811,1.387264Z");
  test.end();
});

tape("symbol.type(symbolWye) generates a polygon with the specified size", function(test) {
  var p = polygonContext(), s = shape.symbol().type(shape.symbolWye).context(p);
  s.size(1)(); test.inDelta(p.area(), 1);
  s.size(240)(); test.inDelta(p.area(), 240);
  test.end();
});

tape("symbol.type(symbolWye) generates the expected path", function(test) {
  var s = shape.symbol().type(shape.symbolWye).size(function(d) { return d; });
  test.pathEqual(s(0), "M0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0Z");
  test.pathEqual(s(10), "M0.853360,0.492688L0.853360,2.199408L-0.853360,2.199408L-0.853360,0.492688L-2.331423,-0.360672L-1.478063,-1.838735L0,-0.985375L1.478063,-1.838735L2.331423,-0.360672Z");
  test.end();
});