-
-
Notifications
You must be signed in to change notification settings - Fork 813
/
Copy pathtest.js
184 lines (161 loc) · 3.05 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
// MODULES //
var tape = require( 'tape' );
var RE_DECIMAL_NUMBER = require( './../lib' );
// TESTS //
tape( 'main export is a regular expression', function test( t ) {
t.ok( true, __filename );
t.strictEqual( RE_DECIMAL_NUMBER instanceof RegExp, true, 'main export is a regular expression' );
t.end();
});
tape( 'the regular expression matches decimal numbers', function test( t ) {
var values;
var i;
values = [
'1.234',
'beep 1.234',
'1.234 boop',
'foo 1.234.',
'foo 1.234.567.890',
'1.234!',
'0.234',
'.234',
'1.0',
'-1.0',
'+1.0',
'.0',
'0.0',
'1.234:',
'1.234%'
];
for ( i = 0; i < values.length; i++ ) {
t.strictEqual( RE_DECIMAL_NUMBER.test( values[i] ), true, 'returns true when provided '+values[i] );
}
t.end();
});
tape( 'the regular expression captures decimal numbers', function test( t ) {
var expected;
var values;
var i;
values = [
'1.234',
'beep 1.234',
'1.234 boop',
'foo 1.234.',
'foo 1.234.567.890',
'1.234!',
'0.234',
'.234',
'1.0',
'-1.0',
'+1.0',
'.0',
'0.0',
'1.234:',
'1.234%'
];
expected = [
[ '1.234', '1.234' ],
[ '1.234', '1.234' ],
[ '1.234', '1.234' ],
[ '1.234', '1.234' ],
[ '1.234', '1.234' ],
[ '1.234', '1.234' ],
[ '0.234', '0.234' ],
[ '.234', '.234' ],
[ '1.0', '1.0' ],
[ '-1.0', '-1.0' ],
[ '+1.0', '+1.0' ],
[ '.0', '.0' ],
[ '0.0', '0.0' ],
[ '1.234', '1.234' ],
[ '1.234', '1.234' ]
];
for ( i = 0; i < values.length; i++ ) {
t.deepEqual( RE_DECIMAL_NUMBER.exec( values[i] ).slice(), expected[ i ], 'captures a decimal number when provided '+values[i] );
}
t.end();
});
tape( 'the regular expression does not match strings which are not decimal numbers', function test( t ) {
var values;
var i;
values = [
'foo',
'beep',
'boop',
'%',
'',
'$',
'$$$$',
'%%%%',
'()',
'[]',
'{}',
'/////',
'a/b',
'p%',
'\\d+',
'0',
'1',
'2',
'2:3',
'beep 0',
'0 beep'
];
for ( i = 0; i < values.length; i++ ) {
t.strictEqual( RE_DECIMAL_NUMBER.test( values[i] ), false, 'returns false when provided '+values[i] );
}
t.end();
});
tape( 'the regular expression does not capture anything when executed on strings without decimal numbers', function test( t ) {
var values;
var i;
values = [
'foo',
'beep',
'boop',
'%',
'',
'$',
'$$$$',
'%%%%',
'()',
'[]',
'{}',
'/////',
'a/b',
'p%',
'\\d+',
'0',
'1',
'2',
'2:3',
'beep 0',
'0 beep'
];
for ( i = 0; i < values.length; i++ ) {
t.strictEqual( RE_DECIMAL_NUMBER.exec( values[i] ), null, 'returns null when provided '+values[i] );
}
t.end();
});
tape( 'the regular expression can be used to generate a regular expression to perform a global capture', function test( t ) {
var expected;
var out;
var str;
var re;
str = '1.234 5.67 -1.0, +1.4, 5.6.7.8.9';
re = new RegExp( RE_DECIMAL_NUMBER.source, 'g' );
out = str.match( re );
expected = [
'1.234',
'5.67',
'-1.0',
'+1.4',
'5.6',
'.7',
'.8',
'.9'
];
t.deepEqual( out.slice(), expected, 'returns all matches' );
t.end();
});