Skip to content

Commit 39f130b

Browse files
committed
Support error test with "expectedThrow" property
1 parent 05a5951 commit 39f130b

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

test.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,38 @@ var total = TESTS.length, failures = 0,
3333
for (i = 0; i < total; i++) {
3434
test = TESTS[i];
3535
tokens = parseCss.tokenize(test.css);
36-
parser = parseCss[typeof test.parser === 'string' ? test.parser : 'parseAStylesheet'];
37-
result = (typeof parser === 'function') ? parser(tokens) : tokens;
38-
dump = JSON.stringify(result, null, ' ');
39-
expected_dump = JSON.stringify(test.expected, null, ' ');
40-
if (dump == expected_dump) {
41-
log(`Test ${i} of ${total}: PASS`);
42-
} else {
43-
log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
44-
log(ansidiff.lines(expected_dump, dump));
45-
failures++;
36+
try {
37+
parser = parseCss[typeof test.parser === 'string' ? test.parser : 'parseAStylesheet'];
38+
result = (typeof parser === 'function') ? parser(tokens) : tokens;
39+
if (test.expectedThrow) {
40+
log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
41+
log(`Expected error not thrown: ` + ansidiff.words(test.expectedThrow.name, ''));
42+
failures++;
43+
continue;
44+
}
45+
dump = JSON.stringify(result, null, ' ');
46+
expected_dump = JSON.stringify(test.expected, null, ' ');
47+
if (dump == expected_dump) {
48+
log(`Test ${i} of ${total}: PASS`);
49+
} else {
50+
log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
51+
log(ansidiff.lines(expected_dump, dump));
52+
failures++;
53+
}
54+
} catch (ex) {
55+
if (test.expectedThrow) {
56+
if (ex.name === test.expectedThrow.name) {
57+
log(`Test ${i} of ${total}: PASS`);
58+
} else {
59+
log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
60+
log(`Expected error not thrown: ` + ansidiff.words(test.expectedThrow.name, ex.name));
61+
failures++;
62+
}
63+
} else {
64+
log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
65+
log(ansidiff.words(`Unexpected error: ${ex}`, ``));
66+
failures++;
67+
}
4668
}
4769
}
4870

0 commit comments

Comments
 (0)