-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
99 lines (82 loc) · 2.01 KB
/
test.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
'use strict';
var test = require('tape');
var decToRoman = require('./decToRoman.js');
var numerals = [
[1, 'I'],
[5, 'V'],
[10, 'X'],
[50, 'L'],
[100, 'C'],
[500, 'D'],
[1000, 'M']
];
var additions = [
[2, 'II'],
[3, 'III'],
[6, 'VI'],
[7, 'VII'],
[8, 'VIII'],
[11, 'XI'],
[12, 'XII'],
[13, 'XIII'],
[95, 'XCV'],
[3999, 'MMMCMXCIX']
];
var subtractions = [
[4, 'IV'],
[9, 'IX'],
[14, 'XIV'],
[40, 'XL'],
[1900, 'MCM']
];
var shortest = [
[99, 'XCIX']
];
function process(cases, testMsg) {
test(testMsg, function(t) {
t.plan(cases.length);
cases.forEach(function(el) {
var num = el[0], roman = el[1];
t.equal(decToRoman(num), roman, num+' -> ' + roman);
});
});
}
test('exception handling', function(t) {
var tests = [
[[0.4], 'throw an error when float is given'],
[[null], 'throw an error when null is given'],
[[undefined], 'throw an error when null is given'],
[[0], 'throw an error when \'0\' is given'],
[[-12], 'throw an error when negative is given'],
[[4000], 'throw an error when number larger than 3999 is given'],
];
t.plan(tests.length);
tests.forEach(function(obj) {
try {
decToRoman.apply(this, obj[0]);
t.fail('it should ' + obj[1]);
} catch (e) {
t.pass('it does ' + obj[1]);
}
});
});
test('Checking if any supported number throws an error', function(t) {
var i = 4000,
fail = false;
while (--i) {
try {
decToRoman(i);
} catch (e) {
t.fail('it should convert ' + i + ' instead of throwing ' + (e.msg || e));
fail = true;
}
}
if (!fail) {
t.pass('all supported numbers can be transformed');
}
t.end();
});
process(numerals, 'Basic numerals');
process(additions, 'additions');
process(subtractions, 'subtractions');
process(shortest, 'shortest');