-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.js
143 lines (106 loc) · 3.3 KB
/
tests.js
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
143
var Mapping = require("./Mapping")
, test = require("tape")
, d3 = require("d3")
var a = 10130
, b = 3900
, ordinal_data = ['hi', 'blue', 'yellow', 'green']
, data = make_some_data(a, b, ordinal_data)
// The numbers above are chosen at random ^^
var M = new Mapping(d3.scale.linear(), accessor)
, D = new Mapping(d3.scale.ordinal(), ordinal_accessor)
// okay, now we're going to make sure all the methods just work:
test("A method throws for continuous data", function(t) {
t.plan(5)
t.doesNotThrow(
function(){
M.domain([0,1])
}, "Setting the domain." )
t.doesNotThrow(function(){
M.create_axis()
}, "Creating an axis.")
t.doesNotThrow(function(){
M.min(-10)
}, "Setting the minimum value")
t.doesNotThrow(function(){
M.max(10)
}, "Setting the maximum value")
t.doesNotThrow(function(){
M.compute_domain(data)
}, "Computing the domain from the data")
})
test("A method throws for discrete data", function(t) {
t.plan(5)
t.doesNotThrow(
function(){
D.domain([0,1])
}, "Setting the domain." )
t.doesNotThrow(function(){
D.create_axis()
}, "Creating an axis.")
t.doesNotThrow(function(){
D.min(-10)
}, "Setting the minimum value")
t.doesNotThrow(function(){
D.max(10)
}, "Setting the maximum value")
t.doesNotThrow(function(){
D.compute_domain(data)
}, "Computing the domain from the data")
})
test("Domains are computed correctly for continuous data.", function(t){
t.plan(2)
M.compute_domain(data)
var continuous_domain = M.domain()
t.equal(continuous_domain[0], b)
t.equal(continuous_domain[1], a)
})
test("Domains are computed correctly for discrete data.", function(t){
t.plan(5)
D.compute_domain(data, true)
var discrete_domain = D.domain()
, n = discrete_domain.length
, m = ordinal_data.length
ordinal_data.sort()
discrete_domain.sort()
t.equal(n, m, "same number of elements in both pieces")
for (var i = 0; i < ordinal_data.length; ++i){
t.equal(ordinal_data[i], discrete_domain[i], ordinal_data[i] +", "+ discrete_domain[i])
}
})
test("Mapping.place does the right thing for M", function(t){
t.plan(2)
var points = data.slice(0, 1).concat(data.slice(-1))
M.compute_domain(data)
M.range([-10, 10])
t.equal(M.place(points[0]), 10, "Places the point at the end of the domain correctly")
t.equal(M.place(points[1]), -10, "Places the point at the beginning of the domain correctly")
})
test("Mapping.place does the right thing for D", function(t){
t.plan(5)
var points = ordinal_data.map(function(d){ return {color: d}})
D.domain(ordinal_data)
D.range(["a", "b", "c", "d"])
var places = points.map(D.place)
t.equal(d3.set(places).values().length, ordinal_data.length, "make sure the mapping is 1:1 and onto")
places.forEach(function(d, i){
t.equal(d, D.range()[i], "Make sure the placement of the " + i + "-th point is correct")
})
})
function make_some_data(a, b, ordinal_data) {
// given a range for datas, make some datas in that range
var datas = []
, val
, choice
for (var i = 0; i <= 100; ++i){
val = a + ((b-a)/100)*i
choice = i % ordinal_data.length
datas.push({val: val, color: ordinal_data[choice]})
}
return datas
}
function accessor(d) {
return d.val
}
function ordinal_accessor(d) {
return d.color
}