forked from tabatkins/parse-css
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.js
75 lines (70 loc) · 2.06 KB
/
tests.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
var TESTS = [
{
css: 'foo { bar: baz; }',
expected: {"type": "stylesheet", "value": [
{
"type": "selector",
"selector": ["IDENT(foo)", "WS"],
"value": [
{
"type": "declaration",
"name": "bar",
"value": ["WS", "IDENT(baz)"]}]}]
}
}, {
css: 'foo { bar: rgb(255, 0, 127); }',
expected: {"type": "stylesheet", "value": [
{
"type": "selector",
"selector": ["IDENT(foo)", "WS"],
"value": [
{
"type": "declaration",
"name": "bar",
"value": ["WS", {"type": "func", "name": "rgb", "value": [
["INT(255)"], ["WS", "INT(0)"], ["WS", "INT(127)"]]}]}]}]
}
}, {
css: '#foo {}',
expected: {"type": "stylesheet", "value": [
{
"type": "selector",
"selector": ["HASH(foo)", "WS"],
"value": []}]
}
}, {
css: '@media{ }',
expected: {"type": "stylesheet", "value": [
{
"type": "at", "name": "media",
"prelude": [],
"value": []}]}
}
];
var ansidiff = require('ansidiff'),
tokenize = require('./parse-css').tokenize,
parseAStylesheet = require('./parse-css').parseAStylesheet;
var total = TESTS.length, failures = 0,
i, test, tokens, sheet, dump, expected_dump;
for (i = 0; i < total; i++) {
test = TESTS[i];
sheet = parseAStylesheet(test.css);
dump = sheet.toString(' ');
expected_dump = JSON.stringify(test.expected, null, ' ');
if (dump == expected_dump) {
console.log('Test %d of %d: PASS', i, total);
} else {
console.log('Test %d of %d: FAIL\nCSS: %s\nTokens: %s',
i, total, test.css, tokens.join(' '));
console.log(ansidiff.lines(expected_dump, dump));
failures++;
}
}
// Abuse the differ to get colored output
if (failures == 0) {
console.log(ansidiff.words('%d tests, ', '%d tests, all passed :)'),
total);
} else {
console.log(ansidiff.words('%d tests, %d failures :(', '%d tests, '),
total, failures);
}