1 /*jshint latedef: nofunc */
2 var path = require('path'),
6 var less = require('../lib/less');
7 var stylize = require('../lib/less/lessc_helper').stylize;
9 var globals = Object.keys(global);
11 var oneTestOnly = process.argv[2];
13 var isVerbose = process.env.npm_config_loglevel === 'verbose';
19 less.tree.functions.add = function (a, b) {
20 return new(less.tree.Dimension)(a.value + b.value);
22 less.tree.functions.increment = function (a) {
23 return new(less.tree.Dimension)(a.value + 1);
25 less.tree.functions._color = function (str) {
26 if (str.value === "evil red") { return new(less.tree.Color)("600"); }
29 console.log("\n" + stylize("LESS", 'underline') + "\n");
31 runTestSet({strictMath: true, relativeUrls: true, silent: true});
32 runTestSet({strictMath: true, strictUnits: true}, "errors/",
33 testErrors, null, getErrorPathReplacementFunction("errors"));
34 runTestSet({strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/",
35 testErrors, null, getErrorPathReplacementFunction("no-js-errors"));
36 runTestSet({strictMath: true, dumpLineNumbers: 'comments'}, "debug/", null,
37 function(name) { return name + '-comments'; });
38 runTestSet({strictMath: true, dumpLineNumbers: 'mediaquery'}, "debug/", null,
39 function(name) { return name + '-mediaquery'; });
40 runTestSet({strictMath: true, dumpLineNumbers: 'all'}, "debug/", null,
41 function(name) { return name + '-all'; });
42 runTestSet({strictMath: true, relativeUrls: false, rootpath: "folder (1)/"}, "static-urls/");
43 runTestSet({strictMath: true, compress: true}, "compression/");
44 runTestSet({}, "legacy/");
45 runTestSet({strictMath: true, strictUnits: true, sourceMap: true }, "sourcemaps/",
46 testSourcemap, null, null, function(filename) { return path.join('test/sourcemaps', filename) + '.json'; });
50 function getErrorPathReplacementFunction(dir) {
51 return function(input) {
53 "{path}", path.join(process.cwd(), "/test/less/" + dir + "/"))
54 .replace("{pathrel}", path.join("test", "less", dir + "/"))
55 .replace("{pathhref}", "")
56 .replace("{404status}", "")
57 .replace(/\r\n/g, '\n');
61 function testSourcemap(name, err, compiledLess, doReplacements, sourcemap) {
62 fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
63 sys.print("- " + name + ": ");
64 if (sourcemap === expectedSourcemap) {
67 fail("ERROR: " + (err && err.message));
70 console.error(err.stack);
73 difference("FAIL", expectedSourcemap, sourcemap);
78 function testErrors(name, err, compiledLess, doReplacements) {
79 fs.readFile(path.join('test/less/', name) + '.txt', 'utf8', function (e, expectedErr) {
80 sys.print("- " + name + ": ");
81 expectedErr = doReplacements(expectedErr, 'test/less/errors/');
84 fail("No Error", 'red');
86 fail("No Error, No Output");
89 var errMessage = less.formatError(err);
90 if (errMessage === expectedErr) {
93 difference("FAIL", expectedErr, errMessage);
99 function globalReplacements(input, directory) {
100 var p = path.join(process.cwd(), directory),
101 pathimport = path.join(process.cwd(), directory + "import/"),
102 pathesc = p.replace(/[.:/\\]/g, function(a) { return '\\' + (a=='\\' ? '\/' : a); }),
103 pathimportesc = pathimport.replace(/[.:/\\]/g, function(a) { return '\\' + (a=='\\' ? '\/' : a); });
105 return input.replace(/\{path\}/g, p)
106 .replace(/\{pathesc\}/g, pathesc)
107 .replace(/\{pathimport\}/g, pathimport)
108 .replace(/\{pathimportesc\}/g, pathimportesc)
109 .replace(/\r\n/g, '\n');
112 function checkGlobalLeaks() {
113 return Object.keys(global).filter(function(v) {
114 return globals.indexOf(v) < 0;
118 function runTestSet(options, foldername, verifyFunction, nameModifier, doReplacements, getFilename) {
119 foldername = foldername || "";
121 if(!doReplacements) {
122 doReplacements = globalReplacements;
125 fs.readdirSync(path.join('test/less/', foldername)).forEach(function (file) {
126 if (! /\.less/.test(file)) { return; }
128 var name = foldername + path.basename(file, '.less');
130 if (oneTestOnly && name !== oneTestOnly) {
136 if (options.sourceMap) {
138 options.writeSourceMap = function(output) {
139 sourceMapOutput = output;
141 options.sourceMapOutputFilename = name + ".css";
142 options.sourceMapBasepath = path.join(process.cwd(), "test/less");
143 options.sourceMapRootpath = "testweb/";
146 toCSS(options, path.join('test/less/', foldername + file), function (err, less) {
148 if (verifyFunction) {
149 return verifyFunction(name, err, less, doReplacements, sourceMapOutput);
152 if(nameModifier) { css_name = nameModifier(name); }
153 fs.readFile(path.join('test/css', css_name) + '.css', 'utf8', function (e, css) {
154 sys.print("- " + css_name + ": ");
156 css = css && doReplacements(css, 'test/less/' + foldername);
157 if (less === css) { ok('OK'); }
159 fail("ERROR: " + (err && err.message));
162 console.error(err.stack);
165 difference("FAIL", css, less);
172 function diff(left, right) {
173 require('diff').diffLines(left, right).forEach(function(item) {
174 if(item.added || item.removed) {
175 var text = item.value.replace("\n", String.fromCharCode(182) + "\n");
176 sys.print(stylize(text, item.added ? 'green' : 'red'));
178 sys.print(item.value);
185 console.error(stylize(msg, 'red'));
190 function difference(msg, left, right) {
191 console.warn(stylize(msg, 'yellow'));
199 console.log(stylize(msg, 'green'));
205 var leaked = checkGlobalLeaks();
206 if (failedTests + passedTests === totalTests) {
208 if (failedTests > 0) {
209 console.error(failedTests + stylize(" Failed", "red") + ", " + passedTests + " passed");
211 console.log(stylize("All Passed ", "green") + passedTests + " run");
213 if (leaked.length > 0) {
215 console.warn(stylize("Global leak detected: ", "red") + leaked.join(', '));
218 if (leaked.length || failedTests) {
220 process.on('exit', function() { process.reallyExit(1) });
225 function toCSS(options, path, callback) {
227 options = options || {};
228 fs.readFile(path, 'utf8', function (e, str) {
229 if (e) { return callback(e); }
231 options.paths = [require('path').dirname(path)];
232 options.filename = require('path').resolve(process.cwd(), path);
233 options.optimization = options.optimization || 0;
235 new(less.Parser)(options).parse(str, function (err, tree) {
240 css = tree.toCSS(options);
250 function testNoOptions() {
253 sys.print("- Integration - creating parser without options: ");
256 fail(stylize("FAIL\n", "red"));
259 ok(stylize("OK\n", "green"));