-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.js
125 lines (112 loc) · 3.31 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
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
const test = require('ava');
const humanInterval = require('.');
const macro = (t, inputs, expected) => {
inputs = Array.isArray(inputs) ? inputs : [inputs];
inputs.forEach(input => t.is(humanInterval(input), expected));
};
const units = {};
units.second = 1000;
units.minute = units.second * 60;
units.hour = units.minute * 60;
units.day = units.hour * 24;
units.week = units.day * 7;
units.month = units.day * 30;
units.year = units.day * 365;
const timeUnits = [
'second',
'minute',
'hour',
'day',
'week',
'month',
'year'
];
const englishNumbers = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten'
];
// Invalid values:
test('Returns undefined when given undefined', macro, undefined, undefined);
test('Returns null when given null', macro, null, null);
test('Returns empty string when given empty string', macro, '', '');
test('Returns NaN when given unknown string', macro, 'foobar', Number.NaN);
// Single values
test('Returns the number when given a number', macro, 5000, 5000);
test('Understands numbers', macro, '2 seconds', 2 * units.second);
test('Understands decimals', macro, '2.5 seconds', 2.5 * units.second);
test('Understands long decimals', macro, '2.5555 seconds', 2.5555 * units.second);
// Understands time base units without numbers
// i.e. "second === 1000"
timeUnits.forEach(unit => {
test(
'Understands "' + unit + '" without number',
macro,
unit,
Number(units[unit])
);
});
// Understands time base units in singular
// i.e. "1 second === 1000"
timeUnits.forEach(unit => {
test(
'Understands "1 ' + unit + '"',
macro,
'1 ' + unit,
Number(units[unit])
);
});
// Understands time base units in plural
// i.e. "2 seconds === 2000"
timeUnits.forEach(unit => {
test(
'Understands ' + unit + 's in plural',
macro,
'2 ' + unit + 's',
2 * Number(units[unit])
);
});
// Understands time base units in decimals values
// i.e. "1.5 seconds === 1500"
timeUnits.forEach(unit => {
test(
'Understands ' + unit + 's in decimals',
macro,
'1.5 ' + unit + 's',
1.5 * Number(units[unit])
);
});
// English numbers
// i.e. "one second === 1000"
englishNumbers.forEach((number, i) => {
test(
'Understands english number ' + number,
macro,
number + ' seconds',
(i + 1) * units.second
);
});
// Understands negative numbers
// i.e. "-2 seconds === -2000"
timeUnits.forEach(unit => {
test(
'Understands negative ' + unit + 's',
macro,
'-2 ' + unit + 's',
-2 * Number(units[unit])
);
});
// Mixed
test('Understands mixed units', macro, '3 minutes and 30 seconds', (3 * units.minute) + (30 * units.second));
test('Understands mixed time expressions with plurals', macro, ['three minutes and 30 seconds', 'three minutes 30 seconds'], (3 * units.minute) + (30 * units.second));
test('Understands mixed time expressions with singulars', macro, ['one minute and 1 second', 'one minute 1 second'], units.minute + units.second);
test('Understands 2 digit english numbers', macro, 'thirty three seconds', 33 * units.second);
test('Understands mix units + multi digit english numbers', macro, 'hundred and three seconds and twelve minutes', (103 * units.second) + (12 * units.minute));
test('Understands hyphenated numbers', macro, 'three hundred and twenty-five seconds', (325 * units.second));