forked from tabatkins/parse-css
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
88 lines (85 loc) · 2.28 KB
/
test.html
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
<!doctype html>
<title>Tests</title>
<script src="tokenizer.js"></script>
<script src="parser.js"></script>
<script src="diff.js"></script>
<style>
ins { background: hsl(120, 50%, 90%); color: greeen; }
del { background: hsl(0, 50%, 90%); color: darkred; }
ins, del { text-decoration: none; }
</style>
<script>
function log(str) {
document.querySelector('#log').innerHTML += str+'\n';
}
</script>
<pre id='log'></pre>
<script>
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 total = TESTS.length, failures = 0,
i, test, tokens, sheet, dump, expected_dump;
for (i = 0; i < total; i++) {
test = TESTS[i];
tokens = tokenize(test.css);
sheet = parse(tokens);
dump = sheet.toString(' ');
expected_dump = JSON.stringify(test.expected, null, ' ');
if (dump == expected_dump) {
log('Test '+i+' of '+total+': PASS');
} else {
log('Test '+i+' of '+total+': FAIL\nCSS: '+test.css+'\nTokens: '+tokens.join(' '));
log(diffString(expected_dump, dump));
failures++;
}
}
// Abuse the differ to get colored output
if (failures == 0) {
log(diffString(total+' tests, ', total+' tests, all passed :)'));
} else {
log(diffString(total+' tests, '+failures+' failures :(', total+' tests, '));
}
</script>