Skip to content

Commit 8e1e37d

Browse files
committed
utilities/conversion.js: add unit tests
1 parent 3b7016c commit 8e1e37d

File tree

2 files changed

+314
-1
lines changed

2 files changed

+314
-1
lines changed

src/utilities/conversion.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ p5.prototype.str = function(n) {
122122
* print(boolean(1)); // true
123123
* print(boolean('true')); // true
124124
* print(boolean('abcd')); // false
125-
* print(boolean([0, 12, 'true'])); // [false, true, false]
125+
* print(boolean([0, 12, 'true'])); // [false, true, true]
126126
* </code></div>
127127
*/
128128
p5.prototype.boolean = function(n) {

test/unit/utilities/conversion.js

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
suite.only('Conversion', function() {
2+
var myp5;
3+
4+
setup(function(done) {
5+
new p5(function(p) {
6+
p.setup = function() {
7+
myp5 = p;
8+
done();
9+
};
10+
});
11+
});
12+
13+
teardown(function() {
14+
myp5.remove();
15+
});
16+
17+
var result;
18+
19+
suite('p5.prototype.float', function() {
20+
test('should be a function', function() {
21+
assert.ok(myp5.float);
22+
assert.typeOf(myp5.float, 'function');
23+
});
24+
25+
test('should convert a string to its floating point representation', function() {
26+
result = myp5.float('56.99998');
27+
assert.typeOf(result, 'Number');
28+
assert.strictEqual(result, 56.99998);
29+
});
30+
31+
test('should return NaN for invalid string', function() {
32+
result = myp5.float('cat');
33+
assert.isNaN(result);
34+
});
35+
36+
test('should return Infinity for Infinity', function() {
37+
result = myp5.float(Infinity);
38+
assert.strictEqual(result, Infinity);
39+
});
40+
41+
test('should return array of floating points and Nan', function() {
42+
result = myp5.float(['1', '2.0', '3.1', 'giraffe']);
43+
assert.typeOf(result, 'Array');
44+
assert.deepEqual(result, [1, 2.0, 3.1, NaN]);
45+
});
46+
});
47+
48+
suite('p5.prototype.int', function() {
49+
test('should be a function', function() {
50+
assert.ok(myp5.int);
51+
assert.typeOf(myp5.int, 'function');
52+
});
53+
54+
test('should convert false to its integer representation i.e. 0', function() {
55+
result = myp5.int(false);
56+
assert.typeOf(result, 'Number');
57+
assert.strictEqual(result, 0);
58+
});
59+
60+
test('should convert true to its integer representation i.e. 1', function() {
61+
result = myp5.int(true);
62+
assert.strictEqual(result, 1);
63+
});
64+
65+
test('should convert a string to its integer representation', function() {
66+
result = myp5.int('1001');
67+
assert.strictEqual(result, 1001);
68+
});
69+
70+
test('should return NaN for invalid string', function() {
71+
result = myp5.int('cat');
72+
assert.isNaN(result);
73+
});
74+
75+
test('should return Infinity for Infinity', function() {
76+
result = myp5.int(Infinity);
77+
assert.strictEqual(result, Infinity);
78+
});
79+
80+
test('should convert float to its integer representation', function() {
81+
result = myp5.int('-1001.9');
82+
assert.strictEqual(result, -1001);
83+
});
84+
85+
test('should return array of integers and NaN', function() {
86+
result = myp5.int(['1', '2.3', '-3.5', 'giraffe', false, 4.7]);
87+
assert.typeOf(result, 'Array');
88+
assert.deepEqual(result, [1, 2, -3, NaN, 0, 4]);
89+
});
90+
});
91+
92+
suite('p5.prototype.str', function() {
93+
test('should be a function', function() {
94+
assert.ok(myp5.str);
95+
assert.typeOf(myp5.str, 'function');
96+
});
97+
98+
test('should convert false to string', function() {
99+
result = myp5.str(false);
100+
assert.typeOf(result, 'String');
101+
assert.strictEqual(result, 'false');
102+
});
103+
104+
test('should convert true to string', function() {
105+
result = myp5.str(true);
106+
assert.strictEqual(result, 'true');
107+
});
108+
109+
test('should convert a number to string', function() {
110+
result = myp5.str(45);
111+
assert.strictEqual(result, '45');
112+
});
113+
114+
test('should return array of strings', function() {
115+
result = myp5.str([1, 2.3, true, -4.5]);
116+
assert.typeOf(result, 'Array');
117+
assert.deepEqual(result, ['1', '2.3', 'true', '-4.5']);
118+
});
119+
});
120+
121+
suite('p5.prototype.boolean', function() {
122+
test('should be a function', function() {
123+
assert.ok(myp5.boolean);
124+
assert.typeOf(myp5.boolean, 'function');
125+
});
126+
127+
test('should convert 1 to true', function() {
128+
result = myp5.boolean(1);
129+
assert.strictEqual(result, true);
130+
});
131+
132+
test('should convert a number to true', function() {
133+
result = myp5.boolean(154);
134+
assert.strictEqual(result, true);
135+
});
136+
137+
test('should return true for Infinity', function() {
138+
result = myp5.boolean(Infinity);
139+
assert.strictEqual(result, true);
140+
});
141+
142+
test('should convert 0 to false', function() {
143+
result = myp5.boolean(0);
144+
assert.strictEqual(result, false);
145+
});
146+
147+
test('should convert a string to false', function() {
148+
result = myp5.boolean('1');
149+
assert.strictEqual(result, false);
150+
});
151+
152+
test('should convert a string to false', function() {
153+
result = myp5.boolean('0');
154+
assert.strictEqual(result, false);
155+
});
156+
157+
test('should convert "true" to true', function() {
158+
result = myp5.boolean('true');
159+
assert.strictEqual(result, true);
160+
});
161+
162+
test('should return false for empty string', function() {
163+
result = myp5.boolean('');
164+
assert.strictEqual(result, false);
165+
});
166+
167+
test('should return array of boolean', function() {
168+
result = myp5.boolean([1, true, -4.5, Infinity, 'cat', '23']);
169+
assert.typeOf(result, 'Array');
170+
assert.deepEqual(result, [true, true, true, true, false, false]);
171+
});
172+
});
173+
174+
suite('p5.prototype.byte', function() {
175+
test('should be a function', function() {
176+
assert.ok(myp5.byte);
177+
assert.typeOf(myp5.byte, 'function');
178+
});
179+
180+
test('should return 127 for 127', function() {
181+
result = myp5.byte(127);
182+
assert.strictEqual(result, 127);
183+
});
184+
185+
test('should return -128 for 128', function() {
186+
result = myp5.byte(128);
187+
assert.strictEqual(result, -128);
188+
});
189+
190+
test('should return 23 for 23.4', function() {
191+
result = myp5.byte(23.4);
192+
assert.strictEqual(result, 23);
193+
});
194+
195+
test('should return 1 for true', function() {
196+
result = myp5.byte(true);
197+
assert.strictEqual(result, 1);
198+
});
199+
200+
test('should return 23 for "23.4"', function() {
201+
result = myp5.byte('23.4');
202+
assert.strictEqual(result, 23);
203+
});
204+
205+
test('should return NaN for invalid string', function() {
206+
result = myp5.byte('cat');
207+
assert.isNaN(result);
208+
});
209+
210+
test('should return array', function() {
211+
result = myp5.byte([0, 255, '100']);
212+
assert.typeOf(result, 'Array');
213+
assert.deepEqual(result, [0, -1, 100]);
214+
});
215+
});
216+
217+
suite('p5.prototype.char', function() {
218+
test('should be a function', function() {
219+
assert.ok(myp5.char);
220+
assert.typeOf(myp5.char, 'function');
221+
});
222+
223+
test('should return the char representation of the number', function() {
224+
result = myp5.char(65);
225+
assert.typeOf(result, 'String');
226+
assert.strictEqual(result, 'A');
227+
});
228+
229+
test('should return the char representation of the string', function() {
230+
result = myp5.char('65');
231+
assert.strictEqual(result, 'A');
232+
});
233+
234+
test('should return array', function() {
235+
result = myp5.char([65, 66, '67']);
236+
assert.typeOf(result, 'Array');
237+
assert.deepEqual(result, ['A', 'B', 'C']);
238+
});
239+
});
240+
241+
suite('p5.prototype.unchar', function() {
242+
test('should be a function', function() {
243+
assert.ok(myp5.unchar);
244+
assert.typeOf(myp5.unchar, 'function');
245+
});
246+
247+
test('should return the integer representation of char', function() {
248+
result = myp5.unchar('A');
249+
assert.typeOf(result, 'Number');
250+
assert.strictEqual(result, 65);
251+
});
252+
253+
test('should return array of numbers', function() {
254+
result = myp5.unchar(['A', 'B', 'C']);
255+
assert.typeOf(result, 'Array');
256+
assert.deepEqual(result, [65, 66, 67]);
257+
});
258+
});
259+
260+
suite('p5.prototype.hex', function() {
261+
test('should be a function', function() {
262+
assert.ok(myp5.hex);
263+
assert.typeOf(myp5.hex, 'function');
264+
});
265+
266+
test('should return the hex representation of the number', function() {
267+
result = myp5.hex(65);
268+
assert.typeOf(result, 'String');
269+
assert.strictEqual(result, '00000041');
270+
});
271+
272+
test('should return Infinity for Infinity', function() {
273+
result = myp5.hex(Infinity);
274+
assert.typeOf(result, 'Number');
275+
assert.strictEqual(result, Infinity);
276+
});
277+
278+
test('should return array', function() {
279+
result = myp5.hex([65, 66, 67]);
280+
assert.typeOf(result, 'Array');
281+
assert.deepEqual(result, ['00000041', '00000042', '00000043']);
282+
});
283+
});
284+
285+
suite('p5.prototype.unhex', function() {
286+
test('should be a function', function() {
287+
assert.ok(myp5.unhex);
288+
assert.typeOf(myp5.unchar, 'function');
289+
});
290+
291+
test('should return the integer representation of hex', function() {
292+
result = myp5.unhex('00000041');
293+
assert.typeOf(result, 'Number');
294+
assert.strictEqual(result, 65);
295+
});
296+
297+
test('should return the NaN for empty string', function() {
298+
result = myp5.unhex('');
299+
assert.isNaN(result);
300+
});
301+
302+
test('should return the NaN for invalid hex string', function() {
303+
result = myp5.unhex('cat');
304+
assert.isNaN(result);
305+
});
306+
307+
test('should return array of numbers', function() {
308+
result = myp5.unhex(['00000041', '00000042', '00000043']);
309+
assert.typeOf(result, 'Array');
310+
assert.deepEqual(result, [65, 66, 67]);
311+
});
312+
});
313+
});

0 commit comments

Comments
 (0)