-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtest.js
181 lines (134 loc) · 4.58 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const csso = require('./');
const test = require('tape');
const Stream = require('stream');
const Vinyl = require('vinyl');
const fs = require('fs');
const basestyle = 'h1 { color: yellow; } \n h1 { font-size: 2em; }';
const optimalmin = 'h1{color:#ff0;font-size:2em}';
const nonoptimal = 'h1{color:#ff0}h1{font-size:2em}';
const sourceMapCss = readSourceMapTestFile('css');
const sourceMapCssMin = readSourceMapTestFile('min.css');
const sourceMap = readSourceMapTestFile('map');
const sourceMapInitial = readSourceMapTestFile('initial.map');
const sourceMapContinue = readSourceMapTestFile('continue.map');
function readSourceMapTestFile(ext) {
return fs.readFileSync(__dirname + '/test/source-map.' + ext, 'utf8').trimRight();
}
function fixture (contents) {
return new Vinyl({
contents: contents,
cwd: __dirname,
base: __dirname,
path: __dirname + '/test.css'
});
}
function sourceMapFixture (contents, sourceMap) {
const file = new Vinyl({
contents: new Buffer(contents),
cwd: __dirname,
base: __dirname,
path: __dirname + '/test.css',
});
file.sourceMap = sourceMap;
return file;
}
test('should minify css with csso, performing structural optimisation', function (t) {
t.plan(1);
const stream = csso();
stream.on('data', function (file) {
t.equal(String(file.contents), optimalmin);
});
stream.write(fixture(new Buffer(basestyle)));
});
test('should minify css with csso, performing structural optimisation when options is `false` (backward compatibility)', function (t) {
t.plan(1);
const stream = csso(false);
stream.on('data', function (file) {
t.equal(String(file.contents), optimalmin);
});
stream.write(fixture(new Buffer(basestyle)));
});
test('should minify css with csso, with no structural optimisation', function (t) {
t.plan(1);
const stream = csso({ restructure: false });
stream.on('data', function (file) {
t.equal(String(file.contents), nonoptimal);
});
stream.write(fixture(new Buffer(basestyle)));
});
test('should minify css with csso, with no structural optimisation when options is `true` (backward compatibility)', function (t) {
t.plan(1);
const stream = csso(true);
stream.on('data', function (file) {
t.equal(String(file.contents), nonoptimal);
});
stream.write(fixture(new Buffer(basestyle)));
});
test('should let null files pass through', function (t) {
t.plan(1);
const stream = csso();
stream.on('data', function (data) {
t.equal(data.contents, null, 'should not transform null in any way');
});
const file = fixture(null);
stream.write(file);
});
test('should throw an error in stream mode', function (t) {
t.plan(1);
const stream = csso();
const file = fixture(new Stream.Readable());
const write = function () {
stream.write(file);
file.contents.write(basestyle);
file.contents.end();
};
t.throws(write, 'should not support streaming contents');
});
// source maps
test('should generate source map when sourceMap is true', function (t) {
t.plan(2);
const stream = csso({ sourceMap: true });
stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
t.deepEqual(file.sourceMap, JSON.parse(sourceMapInitial));
});
stream.write(sourceMapFixture(
sourceMapCss
));
});
test('should not override filename by options', function (t) {
t.plan(2);
const stream = csso({ sourceMap: true, filename: 'foobar' });
stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
t.deepEqual(file.sourceMap, JSON.parse(sourceMapInitial));
});
stream.write(sourceMapFixture(
sourceMapCss
));
});
test('should generate source map when file has source map', function (t) {
t.plan(2);
const stream = csso();
stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
t.deepEqual(file.sourceMap, JSON.parse(sourceMapContinue));
});
stream.write(sourceMapFixture(
sourceMapCss,
sourceMap
));
});
test('should not generate source map when sourceMap setting is false', function (t) {
t.plan(2);
const stream = csso({ sourceMap: false });
stream.on('data', function (file) {
t.equal(String(file.contents), sourceMapCssMin);
t.equal(file.sourceMap, null);
});
stream.write(sourceMapFixture(
sourceMapCss,
sourceMap
));
});