|
27 | 27 |
|
28 | 28 | log = log || console.log;
|
29 | 29 |
|
30 |
| -var total = TESTS.length, failures = 0, |
31 |
| - i, test, tokens, parser, result, dump, expected_dump; |
| 30 | +var total = TESTS.length, failures = 0; |
32 | 31 |
|
33 |
| -for (i = 0; i < total; i++) { |
34 |
| - test = TESTS[i]; |
35 |
| - tokens = parseCss.tokenize(test.css); |
| 32 | +for (let i = 0; i < total; i++) { |
| 33 | + const test = TESTS[i]; |
| 34 | + const tokens = parseCss.tokenize(test.css); |
| 35 | + let result, error; |
36 | 36 | try {
|
37 |
| - parser = parseCss[typeof test.parser === 'string' ? test.parser : 'parseAStylesheet']; |
| 37 | + const parser = parseCss[typeof test.parser === 'string' ? test.parser : 'parseAStylesheet']; |
38 | 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) { |
| 39 | + if (test.expectedThrow) { throw null; } |
| 40 | + } catch (ex) { |
| 41 | + error = ex; |
| 42 | + } |
| 43 | + |
| 44 | + if (test.expectedThrow) { |
| 45 | + if (error && error.name === test.expectedThrow.name) { |
48 | 46 | log(`Test ${i} of ${total}: PASS`);
|
49 | 47 | } else {
|
50 | 48 | log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
|
51 |
| - log(ansidiff.lines(expected_dump, dump)); |
| 49 | + log(`Expected error not thrown: ` + ansidiff.words(test.expectedThrow.name, error && error.name || '')); |
52 | 50 | failures++;
|
53 | 51 | }
|
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 |
| - } |
| 52 | + } else if (error) { |
| 53 | + log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`); |
| 54 | + log(ansidiff.words(`Unexpected error: ${error}`, ``)); |
| 55 | + failures++; |
| 56 | + } else if (test.expected) { |
| 57 | + const dump = JSON.stringify(result, null, ' '); |
| 58 | + const expected_dump = JSON.stringify(test.expected, null, ' '); |
| 59 | + if (dump == expected_dump) { |
| 60 | + log(`Test ${i} of ${total}: PASS`); |
63 | 61 | } else {
|
64 | 62 | log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
|
65 |
| - log(ansidiff.words(`Unexpected error: ${ex}`, ``)); |
| 63 | + log(ansidiff.lines(expected_dump, dump || '')); |
66 | 64 | failures++;
|
67 | 65 | }
|
| 66 | + } else { |
| 67 | + // no specified test, fallback to pass |
| 68 | + log(`Test ${i} of ${total}: PASS`); |
68 | 69 | }
|
69 | 70 | }
|
70 | 71 |
|
|
0 commit comments