less-test.js
6.7 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var path = require('path'),
fs = require('fs'),
sys = require('util');
var less = require('../lib/less');
var stylize = require('../lib/less/lessc_helper').stylize;
var globals = Object.keys(global);
var oneTestOnly = process.argv[2];
var totalTests = 0,
failedTests = 0,
passedTests = 0;
less.tree.functions.add = function (a, b) {
return new(less.tree.Dimension)(a.value + b.value);
};
less.tree.functions.increment = function (a) {
return new(less.tree.Dimension)(a.value + 1);
};
less.tree.functions._color = function (str) {
if (str.value === "evil red") { return new(less.tree.Color)("600") }
};
sys.puts("\n" + stylize("LESS", 'underline') + "\n");
runTestSet({strictMath: true, relativeUrls: true, silent: true});
runTestSet({strictMath: true, strictUnits: true}, "errors/", function(name, err, compiledLess, doReplacements) {
fs.readFile(path.join('test/less/', name) + '.txt', 'utf8', function (e, expectedErr) {
sys.print("- " + name + ": ");
expectedErr = doReplacements(expectedErr, 'test/less/errors/');
if (!err) {
if (compiledLess) {
fail("No Error", 'red');
} else {
fail("No Error, No Output");
}
} else {
var errMessage = less.formatError(err);
if (errMessage === expectedErr) {
ok('OK');
} else {
difference("FAIL", expectedErr, errMessage);
}
}
sys.puts("");
});}, null, function(input, directory) {
return input.replace(
"{path}", path.join(process.cwd(), "/test/less/errors/"))
.replace("{pathrel}", path.join("test", "less", "errors/"))
.replace("{pathhref}", "")
.replace("{404status}", "")
.replace(/\r\n/g, '\n');
});
runTestSet({strictMath: true, dumpLineNumbers: 'comments'}, "debug/", null,
function(name) { return name + '-comments'; });
runTestSet({strictMath: true, dumpLineNumbers: 'mediaquery'}, "debug/", null,
function(name) { return name + '-mediaquery'; });
runTestSet({strictMath: true, dumpLineNumbers: 'all'}, "debug/", null,
function(name) { return name + '-all'; });
runTestSet({strictMath: true, relativeUrls: false, rootpath: "folder (1)/"}, "static-urls/");
runTestSet({strictMath: true, compress: true}, "compression/");
runTestSet({ }, "legacy/");
testNoOptions();
function globalReplacements(input, directory) {
var p = path.join(process.cwd(), directory),
pathimport = path.join(process.cwd(), directory + "import/"),
pathesc = p.replace(/[.:/\\]/g, function(a) { return '\\' + (a=='\\' ? '\/' : a); }),
pathimportesc = pathimport.replace(/[.:/\\]/g, function(a) { return '\\' + (a=='\\' ? '\/' : a); });
return input.replace(/\{path\}/g, p)
.replace(/\{pathesc\}/g, pathesc)
.replace(/\{pathimport\}/g, pathimport)
.replace(/\{pathimportesc\}/g, pathimportesc)
.replace(/\r\n/g, '\n');
}
function checkGlobalLeaks() {
return Object.keys(global).filter(function(v) {
return globals.indexOf(v) < 0;
});
}
function runTestSet(options, foldername, verifyFunction, nameModifier, doReplacements) {
foldername = foldername || "";
if(!doReplacements)
doReplacements = globalReplacements;
fs.readdirSync(path.join('test/less/', foldername)).forEach(function (file) {
if (! /\.less/.test(file)) { return }
var name = foldername + path.basename(file, '.less');
if (oneTestOnly && name !== oneTestOnly) { return; }
totalTests++;
toCSS(options, path.join('test/less/', foldername + file), function (err, less) {
if (verifyFunction) {
return verifyFunction(name, err, less, doReplacements);
}
var css_name = name;
if(nameModifier) css_name=nameModifier(name);
fs.readFile(path.join('test/css', css_name) + '.css', 'utf8', function (e, css) {
sys.print("- " + css_name + ": ")
css = css && doReplacements(css, 'test/less/' + foldername);
if (less === css) { ok('OK'); }
else if (err) {
fail("ERROR: " + (err && err.message));
} else {
difference("FAIL", css, less);
}
sys.puts("");
});
});
});
}
function diff(left, right) {
sys.puts("");
require('diff').diffLines(left, right).forEach(function(item) {
if(item.added || item.removed) {
sys.print(stylize(item.value, item.added ? 'green' : 'red'));
} else {
sys.print(item.value);
}
});
}
function fail(msg) {
sys.print(stylize(msg, 'red'));
failedTests++;
endTest();
}
function difference(msg, left, right) {
sys.print(stylize(msg, 'yellow'));
failedTests++;
diff(left, right);
endTest();
}
function ok(msg) {
sys.print(stylize(msg, 'green'));
passedTests++;
endTest();
}
function endTest() {
var leaked = checkGlobalLeaks();
if (failedTests + passedTests === totalTests) {
sys.puts("");
sys.puts("");
if (failedTests > 0) {
sys.print(failedTests);
sys.print(stylize(" Failed", "red"));
sys.print(", " + passedTests + " passed");
} else {
sys.print(stylize("All Passed ", "green"));
sys.print(passedTests + " run");
}
if (leaked.length > 0) {
sys.puts("");
sys.puts("");
sys.print(stylize("Global leak detected: ", "red") + leaked.join(', '));
sys.print("\n");
}
}
}
function toCSS(options, path, callback) {
var tree, css;
options = options || {};
fs.readFile(path, 'utf8', function (e, str) {
if (e) { return callback(e) }
options.paths = [require('path').dirname(path)];
options.filename = require('path').resolve(process.cwd(), path);
options.optimization = options.optimization || 0;
new(less.Parser)(options).parse(str, function (err, tree) {
if (err) {
callback(err);
} else {
try {
css = tree.toCSS(options);
callback(null, css);
} catch (e) {
callback(e);
}
}
});
});
}
function testNoOptions() {
totalTests++;
try {
sys.print("- Integration - creating parser without options: ");
new(less.Parser);
} catch(e) {
fail(stylize("FAIL\n", "red"));
return;
}
ok(stylize("OK\n", "green"));
}