-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
98 lines (78 loc) · 2.95 KB
/
test.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import fs from 'fs';
import path from 'path';
import postcss from 'postcss';
import test from 'ava';
import plugin from './';
const TEST_DIR = path.join(__dirname, 'test');
const normalize = str => str.replace(/\r\n?/g, '\n').replace(/\n$/,'');
const readFile = basepath => filepath => {
return new Promise((resolve, reject) => {
fs.readFile(path.join(basepath, filepath), 'utf8', (err, data) => {
err ? reject(err) : resolve(data);
});
});
};
const run = (t, input, expected, opts = { readFile: readFile(__dirname) }) => {
return postcss([ plugin(opts) ]).process(input)
.then( result => {
t.same(result.css, expected);
t.same(result.warnings().length, 0);
});
};
const runWithError = (t, input, output, opts = { readFile: readFile(__dirname) }) => {
return t.throws(postcss([ plugin(opts) ]).process(input));
};
//
// Test cases on TEST_DIR
//
// TODO: source order
// TODO: recursion with cycle check
fs.readdirSync(TEST_DIR).forEach(testCase => {
const testCaseDir = (p = '') => path.join(TEST_DIR, testCase, p);
if (fs.existsSync(path.join(testCaseDir(), 'source.css'))) {
test(testCase, t => {
const input = normalize(fs.readFileSync(testCaseDir('source.css'), 'utf-8'));
const expected = normalize(fs.readFileSync(testCaseDir('expected.css'), 'utf-8'));
return run(t, input, expected, {readFile: readFile(testCaseDir())});
});
}
});
//
// Manual test cases
//
test("does not do anything if it doesn\'t have to", t => {
return run(t, '', '');
});
test('reading from other file only works when readFile option is defined', t => {
const input = '.test{includes: bar from "./foo.css"}';
const output = '.test{color: blue}';
return runWithError(t, input, output, { readFile: undefined });
});
test('throws an error when referenced file does not exist', t => {
const input = '.test{includes: bar from "./not.exists"}';
const output = '.test{color: blue}';
return runWithError(t, input, output);
});
test('throws an error when recursion depth exceeds allowed limit', t => {
const input = '.test{includes: test}';
return runWithError(t, input);
});
test('throws an error when selector appears multiple times', t => {
const input = '.mixin{}.mixin{color:red;}.test{includes: mixin}';
return runWithError(t, input);
});
/*
test('throws an error when similar selectors are defined alongside this selector', t => {
const hover = '.mixin{color:red;}.mixin:hover{color:blue;}.test{includes: mixin}';
return runWithError(t, hover);
const outerCascade = 'div .mixin{color:red;}.test{includes: mixin}';
return runWithError(t, outerCascade);
const innerCascade = '.mixin a{color:red;}.test{includes: mixin}';
return runWithError(t, innerCascade);
});
test('works with differently named selector', t => {
const input = '.mixin{color:red;}.mixinOther{color:blue;}.test{includes: mixin}';
const output = '.mixin{color:red;}.mixinOther{color:blue;}.test{color:red;}';
return run(t, input, output);
});
*/