Skip to content

Commit 05a5951

Browse files
committed
Separate test runner from test cases
- Separate the test runner script to a new test.js file so that the developers of other projects can import and run the test cases on their own.
1 parent 30b4cc8 commit 05a5951

File tree

3 files changed

+68
-47
lines changed

3 files changed

+68
-47
lines changed

test.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!doctype html>
22
<title>Tests</title>
3+
<script src="tests.js"></script>
34
<script src="parse-css.js"></script>
45
<script src="diff.js"></script>
56
<style>
@@ -13,4 +14,4 @@
1314
}
1415
</script>
1516
<pre id='log'></pre>
16-
<script src="tests.js"></script>
17+
<script src="test.js"></script>

test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict";
2+
(function (global, factory) {
3+
if (typeof exports === 'object' && typeof module === 'object') {
4+
// CommonJS/Node.js
5+
factory(
6+
require('./tests').TESTS,
7+
require('./parse-css'),
8+
require('ansidiff'),
9+
);
10+
} else if (typeof define === 'function' && define.amd) {
11+
// AMD
12+
require(
13+
['./tests', './parse-css', 'ansidiff'],
14+
factory,
15+
);
16+
} else {
17+
// browser global
18+
global = typeof globalThis !== 'undefined' ? globalThis : global || self;
19+
factory(
20+
global.TESTS,
21+
global,
22+
{lines: global.diffString, words: global.diffString},
23+
global.log,
24+
);
25+
}
26+
}(this, function (TESTS, parseCss, ansidiff, log) {
27+
28+
log = log || console.log;
29+
30+
var total = TESTS.length, failures = 0,
31+
i, test, tokens, parser, result, dump, expected_dump;
32+
33+
for (i = 0; i < total; i++) {
34+
test = TESTS[i];
35+
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++;
46+
}
47+
}
48+
49+
// Abuse the differ to get colored output
50+
if (failures == 0) {
51+
log(ansidiff.words(`${total} tests, `, `${total} tests, all passed :)`));
52+
} else {
53+
log(ansidiff.words(`${total} tests, ${failures} failures :(`, `${total} tests, `));
54+
}
55+
56+
}));

tests.js

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
"use strict";
22
(function (global, factory) {
3-
if (typeof define === 'function' && define.amd) {
4-
require(
5-
['./parse-css', 'ansidiff'],
6-
factory,
7-
);
8-
} else if (typeof exports !== 'undefined') {
9-
factory(
10-
require('./parse-css'),
11-
require('ansidiff'),
12-
);
3+
if (typeof exports === 'object' && typeof module === 'object') {
4+
// CommonJS/Node.js
5+
exports.TESTS = factory();
6+
} else if (typeof define === 'function' && define.amd) {
7+
// AMD
8+
define(factory);
139
} else {
10+
// browser global
1411
global = typeof globalThis !== 'undefined' ? globalThis : global || self;
15-
factory(
16-
global,
17-
{lines: global.diffString, words: global.diffString},
18-
global.log,
19-
);
12+
global.TESTS = factory();
2013
}
21-
}(this, function (parseCss, ansidiff, log) {
14+
}(this, function () {
2215

23-
var TESTS = [
16+
return [
2417
// preprocess()
2518
{
2619
parser: "",
@@ -1652,33 +1645,4 @@ var TESTS = [
16521645
}
16531646
];
16541647

1655-
1656-
var log = log || console.log;
1657-
1658-
var total = TESTS.length, failures = 0,
1659-
i, test, tokens, parser, result, dump, expected_dump;
1660-
1661-
for (i = 0; i < total; i++) {
1662-
test = TESTS[i];
1663-
tokens = parseCss.tokenize(test.css);
1664-
parser = parseCss[typeof test.parser === 'string' ? test.parser : 'parseAStylesheet'];
1665-
result = (typeof parser === 'function') ? parser(tokens) : tokens;
1666-
dump = JSON.stringify(result, null, ' ');
1667-
expected_dump = JSON.stringify(test.expected, null, ' ');
1668-
if (dump == expected_dump) {
1669-
log(`Test ${i} of ${total}: PASS`);
1670-
} else {
1671-
log(`Test ${i} of ${total}: FAIL\nCSS: ${test.css}\nTokens: ${tokens.join(' ')}`);
1672-
log(ansidiff.lines(expected_dump, dump));
1673-
failures++;
1674-
}
1675-
}
1676-
1677-
// Abuse the differ to get colored output
1678-
if (failures == 0) {
1679-
log(ansidiff.words(`${total} tests, `, `${total} tests, all passed :)`));
1680-
} else {
1681-
log(ansidiff.words(`${total} tests, ${failures} failures :(`, `${total} tests, `));
1682-
}
1683-
16841648
}));

0 commit comments

Comments
 (0)