Merge pull request #1 from SomMeri/jasmine

Refactoring browser unit tests into grunt-contrib-jasmine
This commit is contained in:
Jon Schlinkert
2013-08-08 22:03:12 -07:00
39 changed files with 362 additions and 424 deletions

3
.gitignore vendored
View File

@@ -16,3 +16,6 @@ test/browser/less.js
test/browser/test-runner-*.htm
test/sourcemaps/*.map
test/sourcemaps/*.css
# grunt
.grunt

View File

@@ -28,12 +28,6 @@ module.exports = function(grunt) {
test: {
command: 'node test/less-test.js'
},
browser: {
command: 'node test/browser-test-prepare.js'
},
phantom: {
command: 'phantomjs test/browser/phantom-runner.js'
},
benchmark: {
command: 'node benchmark/less-benchmark.js'
}
@@ -115,6 +109,105 @@ module.exports = function(grunt) {
}
},
connect: {
server: {
options: {
port: 8081
}
}
},
jasmine: {
options: {
keepRunner: true,
host: 'http://localhost:8081/',
vendor: 'test/browser/common.js',
template: 'test/browser/test-runner-template.tmpl'
},
main: {
//src is used to build list of less files to compile
src: ['test/less/*.less', '!test/less/javascript.less', '!test/less/urls.less'],
options: {
helpers: 'test/browser/runner-main-options.js',
specs: 'test/browser/runner-main-spec.js',
outfile: 'test/browser/test-runner-main.html'
}
},
legacy: {
src: ['test/less/legacy/*.less'],
options: {
helpers: 'test/browser/runner-legacy-options.js',
specs: 'test/browser/runner-legacy-spec.js',
outfile: 'test/browser/test-runner-legacy.html'
}
},
errors: {
src: ['test/less/errors/*.less', '!test/less/errors/javascript-error.less'],
options: {
timeout: 20000,
helpers: 'test/browser/runner-errors-options.js',
specs: 'test/browser/runner-errors-spec.js',
outfile: 'test/browser/test-runner-errors.html'
}
},
noJsErrors: {
src: ['test/less/no-js-errors/*.less'],
options: {
helpers: 'test/browser/runner-no-js-errors-options.js',
specs: 'test/browser/runner-no-js-errors-spec.js',
outfile: 'test/browser/test-runner-no-js-errors.html'
}
},
browser: {
src: ['test/browser/less/*.less'],
options: {
helpers: 'test/browser/runner-browser-options.js',
specs: 'test/browser/runner-browser-spec.js',
outfile: 'test/browser/test-runner-browser.html'
}
},
relativeUrls: {
src: ['test/browser/less/relative-urls/*.less'],
options: {
helpers: 'test/browser/runner-relative-urls-options.js',
specs: 'test/browser/runner-relative-urls-spec.js',
outfile: 'test/browser/test-runner-relative-urls.html'
}
},
rootpath: {
src: ['test/browser/less/rootpath/*.less'],
options: {
helpers: 'test/browser/runner-rootpath-options.js',
specs: 'test/browser/runner-rootpath-spec.js',
outfile: 'test/browser/test-runner-rootpath.html'
}
},
rootpathRelative: {
src: ['test/browser/less/rootpath-relative/*.less'],
options: {
helpers: 'test/browser/runner-rootpath-relative-options.js',
specs: 'test/browser/runner-rootpath-relative-spec.js',
outfile: 'test/browser/test-runner-rootpath-relative.html'
}
},
production: {
src: ['test/browser/less/production/*.less'],
options: {
helpers: 'test/browser/runner-production-options.js',
specs: 'test/browser/runner-production-spec.js',
outfile: 'test/browser/test-runner-production.html'
}
},
modifyVars: {
src: ['test/browser/less/modify-vars/*.less'],
options: {
helpers: 'test/browser/runner-modify-vars-options.js',
specs: 'test/browser/runner-modify-vars-spec.js',
outfile: 'test/browser/test-runner-modify-vars.html'
}
}
},
// Before running tests, clean out the results
// of any previous tests. (this will need to be
// setup based on configuration of browser tests.
@@ -162,13 +255,18 @@ module.exports = function(grunt) {
'uglify:beta'
]);
// Run all tests
grunt.registerTask('browserTest', [
'connect:server',
'jasmine'
]);
// Run all tests
grunt.registerTask('test', [
'jshint:lib',
'clean',
'shell:test'
// 'shell:browser',
// 'shell:phantom'
'shell:test',
'browserTest'
]);
// Run benchmark

View File

@@ -19,7 +19,7 @@ lib: lib/less
# General
# =================================
prepend:
browser: build/require.js
browser: [build/require.js, build/browser-header.js]
rhino: build/require-rhino.js
append:
@@ -45,6 +45,8 @@ less:
to_css : <%= build.lib %>/to-css-visitor.js
extend : <%= build.lib %>/extend-visitor.js
browser : <%= build.lib %>/browser.js
source_map_output : <%= build.lib %>/source-map-output.js
to_css_visitor : <%= build.lib %>/to-css-visitor.js
# glob all files in ./lib/less/tree directory
treedir : <%= build.lib %>/tree/*.js
@@ -72,6 +74,8 @@ browser:
- <%= build.less.join %>
- <%= build.less.to_css %>
- <%= build.less.extend %>
- <%= build.less.source_map_output %>
- <%= build.less.to_css_visitor %>
# append browser-specific code
- <%= build.append.browser %>
@@ -99,6 +103,8 @@ rhino:
- <%= build.less.colors %>
- <%= build.less.treedir %> # glob all files
- <%= build.less.tree %>
- <%= build.less.source_map_output %>
- <%= build.less.to_css_visitor %>
# append rhino-specific code
- <%= build.append.rhino %>
@@ -143,4 +149,4 @@ tree:
- <%= build.lib %>/tree/unicode-descriptor.js
- <%= build.lib %>/tree/url.js
- <%= build.lib %>/tree/value.js
- <%= build.lib %>/tree/variable.js
- <%= build.lib %>/tree/variable.js

File diff suppressed because one or more lines are too long

View File

@@ -41,7 +41,16 @@
"devDependencies": {
"diff": "~1.0",
"jshint": "~2.1.4",
"http-server": "~0.5.5"
"http-server": "~0.5.5",
"grunt": "~0.4.1",
"matchdep": "~0.1.2",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-jshint": "~0.6.0",
"grunt-contrib-uglify": "~0.2.2",
"grunt-shell": "~0.3.1",
"grunt-contrib-connect": "~0.3.0",
"grunt-contrib-jasmine": "~0.5.1"
},
"keywords": [
"compile less",

View File

@@ -1,47 +0,0 @@
var path = require('path'),
fs = require('fs');
var readDirFilesSync = function(dir, regex, callback) {
fs.readdirSync(dir).forEach(function (file) {
if (! regex.test(file)) { return; }
callback(file);
});
};
var createTestRunnerPage = function(dir, exclude, testSuiteName, dir2) {
var output = '<html><head>\n';
readDirFilesSync(path.join("test", dir, 'less', dir2 || ""), /\.less$/, function (file) {
var name = path.basename(file, '.less'),
id = (dir ? dir + '-' : "") + 'less-' + (dir2 ? dir2 + "-" : "") + name;
if (exclude && name.match(exclude)) { return; }
output += '<link id="original-less:' + id + '" rel="stylesheet/less" type="text/css" href="/' + path.join(dir, 'less', dir2 || "", name) + '.less' +'">\n';
output += '<link id="expected-less:' + id + '" rel="stylesheet" type="text/css" href="/' + path.join(dir, 'css', dir2 || "", name) + '.css' + '">\n';
});
output += String(fs.readFileSync(path.join('test/browser', 'template.htm'))).replace("{runner-name}", testSuiteName);
fs.writeFileSync(path.join('test/browser', 'test-runner-'+testSuiteName+'.htm'), output);
};
var removeFiles = function(dir, regex) {
readDirFilesSync(dir, regex, function(file) {
fs.unlinkSync(path.join(dir, file), function() {
console.log("Failed to delete " + file);
});
});
};
removeFiles("test/browser", /test-runner-[a-zA-Z-]*\.htm$/);
createTestRunnerPage("", /javascript|urls/, "main");
createTestRunnerPage("", null, "legacy", "legacy");
createTestRunnerPage("", /javascript/, "errors", "errors");
createTestRunnerPage("", null, "no-js-errors", "no-js-errors");
createTestRunnerPage("browser", null, "browser");
createTestRunnerPage("browser", null, "relative-urls", "relative-urls");
createTestRunnerPage("browser", null, "rootpath", "rootpath");
createTestRunnerPage("browser", null, "rootpath-relative", "rootpath-relative");
createTestRunnerPage("browser", null, "production");
createTestRunnerPage("browser", null, "modify-vars", "modify-vars");

View File

@@ -1,7 +1,15 @@
/*if not async then phantomjs fails to run the webserver and the test concurrently*/
var less = { async: true, strictMath: true };
/* record log messages for testing */
var logAllIds = function() {
var allTags = document.head.getElementsByTagName('style');
var ids = [];
for (var tg = 0; tg< allTags.length; tg++) {
var tag = allTags[tg];
if (tag.id) {
console.log(tag.id);
}
}
};
var logMessages = [],
realConsoleLog = console.log;
console.log = function(msg) {
@@ -33,11 +41,23 @@ var testSheet = function(sheet) {
it(sheet.id + " should match the expected output", function() {
var lessOutputId = sheet.id.replace("original-", ""),
expectedOutputId = "expected-" + lessOutputId,
lessOutput = document.getElementById(lessOutputId).innerText,
lessOutputObj,
lessOutput,
expectedOutputHref = document.getElementById(expectedOutputId).href,
expectedOutput = loadFile(expectedOutputHref);
// Browser spec generates less on the fly, so we need to loose control
waitsFor(function() {
lessOutputObj = document.getElementById(lessOutputId);
// the type condition is necessary because of inline browser tests
return lessOutputObj!==null && lessOutputObj.type==="text/css";
}, "generation of " + lessOutputId + "", 700);
runs(function() {
lessOutput = lessOutputObj.innerText;
});
waitsFor(function() {
return expectedOutput.loaded;
}, "failed to load expected outout", 10000);
@@ -48,23 +68,37 @@ var testSheet = function(sheet) {
});
};
//TODO: do it cleaner - the same way as in css
function extractId(href) {
return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain
.replace(/^\//, '' ) // Remove root /
.replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension
.replace(/[^\.\w-]+/g, '-') // Replace illegal characters
.replace(/\./g, ':'); // Replace dots with colons(for valid id)
}
var testErrorSheet = function(sheet) {
it(sheet.id + " should match an error", function() {
var lessHref = sheet.href,
id = sheet.id.replace(/^original-less:/, "less-error-message:"),
id = "less-error-message:"+extractId(lessHref),
// id = sheet.id.replace(/^original-less:/, "less-error-message:"),
errorHref = lessHref.replace(/.less$/, ".txt"),
errorFile = loadFile(errorHref),
actualErrorElement = document.getElementById(id),
actualErrorElement,
actualErrorMsg;
describe("the error", function() {
expect(actualErrorElement).not.toBe(null);
});
actualErrorMsg = actualErrorElement.innerText
// Less.js sets 10ms timer in order to add error message on top of page.
waitsFor(function() {
actualErrorElement = document.getElementById(id);
return actualErrorElement!==null;
}, "error message was not generated", 70);
runs(function() {
actualErrorMsg = actualErrorElement.innerText
.replace(/\n\d+/g, function(lineNo) { return lineNo + " "; })
.replace(/\n\s*in /g, " in ")
.replace("\n\n", "\n");
});
waitsFor(function() {
return errorFile.loaded;
@@ -123,4 +157,4 @@ var loadFile = function(href) {
}, 3000);
}
})();
})();

View File

@@ -1,21 +1,21 @@
@import "http://localhost:8081/browser/less/imports/modify-this.css";
@import "http://localhost:8081/test/browser/less/imports/modify-this.css";
@import "http://localhost:8081/browser/less/imports/modify-again.css";
@import "http://localhost:8081/test/browser/less/imports/modify-again.css";
.modify {
my-url: url("http://localhost:8081/browser/less/imports/a.png");
my-url: url("http://localhost:8081/test/browser/less/imports/a.png");
}
.modify {
my-url: url("http://localhost:8081/browser/less/imports/b.png");
my-url: url("http://localhost:8081/test/browser/less/imports/b.png");
}
@font-face {
src: url("/fonts/garamond-pro.ttf");
src: local(Futura-Medium), url(http://localhost:8081/browser/less/relative-urls/fonts.svg#MyGeometricModern) format("svg");
src: local(Futura-Medium), url(http://localhost:8081/test/browser/less/relative-urls/fonts.svg#MyGeometricModern) format("svg");
}
#shorthands {
background: url("http://www.lesscss.org/spec.html") no-repeat 0 4px;
}
#misc {
background-image: url(http://localhost:8081/browser/less/relative-urls/images/image.jpg);
background-image: url(http://localhost:8081/test/browser/less/relative-urls/images/image.jpg);
}
#data-uri {
background: url(data:image/png;charset=utf-8;base64,
@@ -29,8 +29,8 @@
background: transparent url('data:image/svg+xml, <svg version="1.1"><g></g></svg>');
}
.comma-delimited {
background: url(http://localhost:8081/browser/less/relative-urls/bg.jpg) no-repeat, url(http://localhost:8081/browser/less/relative-urls/bg.png) repeat-x top left, url(http://localhost:8081/browser/less/relative-urls/bg);
background: url(http://localhost:8081/test/browser/less/relative-urls/bg.jpg) no-repeat, url(http://localhost:8081/test/browser/less/relative-urls/bg.png) repeat-x top left, url(http://localhost:8081/test/browser/less/relative-urls/bg);
}
.values {
url: url('http://localhost:8081/browser/less/relative-urls/Trebuchet');
url: url('http://localhost:8081/test/browser/less/relative-urls/Trebuchet');
}

View File

@@ -1,4 +1,5 @@
@import "https://www.github.com/cloudhead/imports/modify-this.css";
@import "https://www.github.com/cloudhead/imports/modify-again.css";
.modify {
my-url: url("https://www.github.com/cloudhead/imports/a.png");

View File

@@ -1,20 +1,21 @@
@import "http://localhost:8081/browser/less/modify-this.css";
@import "http://localhost:8081/browser/less/modify-again.css";
@import "http://localhost:8081/test/browser/less/modify-this.css";
@import "http://localhost:8081/test/browser/less/modify-again.css";
.modify {
my-url: url("http://localhost:8081/browser/less/a.png");
my-url: url("http://localhost:8081/test/browser/less/a.png");
}
.modify {
my-url: url("http://localhost:8081/browser/less/b.png");
my-url: url("http://localhost:8081/test/browser/less/b.png");
}
@font-face {
src: url("/fonts/garamond-pro.ttf");
src: local(Futura-Medium), url(http://localhost:8081/browser/less/fonts.svg#MyGeometricModern) format("svg");
src: local(Futura-Medium), url(http://localhost:8081/test/browser/less/fonts.svg#MyGeometricModern) format("svg");
}
#shorthands {
background: url("http://www.lesscss.org/spec.html") no-repeat 0 4px;
}
#misc {
background-image: url(http://localhost:8081/browser/less/images/image.jpg);
background-image: url(http://localhost:8081/test/browser/less/images/image.jpg);
}
#data-uri {
background: url(data:image/png;charset=utf-8;base64,
@@ -28,23 +29,23 @@
background: transparent url('data:image/svg+xml, <svg version="1.1"><g></g></svg>');
}
.comma-delimited {
background: url(http://localhost:8081/browser/less/bg.jpg) no-repeat, url(http://localhost:8081/browser/less/bg.png) repeat-x top left, url(http://localhost:8081/browser/less/bg);
background: url(http://localhost:8081/test/browser/less/bg.jpg) no-repeat, url(http://localhost:8081/test/browser/less/bg.png) repeat-x top left, url(http://localhost:8081/test/browser/less/bg);
}
.values {
url: url('http://localhost:8081/browser/less/Trebuchet');
url: url('http://localhost:8081/test/browser/less/Trebuchet');
}
#data-uri {
uri: url('http://localhost:8081/browser/less/../../data/image.jpg');
uri: url('http://localhost:8081/test/browser/less/../../data/image.jpg');
}
#data-uri-guess {
uri: url('http://localhost:8081/browser/less/../../data/image.jpg');
uri: url('http://localhost:8081/test/browser/less/../../data/image.jpg');
}
#data-uri-ascii {
uri-1: url('http://localhost:8081/browser/less/../../data/page.html');
uri-2: url('http://localhost:8081/browser/less/../../data/page.html');
uri-1: url('http://localhost:8081/test/browser/less/../../data/page.html');
uri-2: url('http://localhost:8081/test/browser/less/../../data/page.html');
}
#data-uri-toobig {
uri: url('http://localhost:8081/browser/less/../../data/data-uri-fail.png');
uri: url('http://localhost:8081/test/browser/less/../../data/data-uri-fail.png');
}
#svg-functions {
background-image: url('data:image/svg+xml,<?xml version="1.0" ?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="0 0 1 1" preserveAspectRatio="none"><linearGradient id="gradient" gradientUnits="userSpaceOnUse" x1="0%" y1="0%" x2="0%" y2="100%"><stop offset="0%" stop-color="#000000"/><stop offset="100%" stop-color="#ffffff"/></linearGradient><rect x="0" y="0" width="1" height="1" fill="url(#gradient)" /></svg>');

View File

@@ -1,5 +1,5 @@
@import ".././imports/urls.less";
@import "http://localhost:8081/browser/less/imports/urls2.less";
@import "http://localhost:8081/test/browser/less/imports/urls2.less";
@font-face {
src: url("/fonts/garamond-pro.ttf");
src: local(Futura-Medium),

View File

@@ -1,5 +1,5 @@
@import "../imports/urls.less";
@import "http://localhost:8081/browser/less/imports/urls2.less";
@import "http://localhost:8081/test/browser/less/imports/urls2.less";
@font-face {
src: url("/fonts/garamond-pro.ttf");
src: local(Futura-Medium),

View File

@@ -1,5 +1,5 @@
@import "../imports/urls.less";
@import "http://localhost:8081/browser/less/imports/urls2.less";
@import "http://localhost:8081/test/browser/less/imports/urls2.less";
@font-face {
src: url("/fonts/garamond-pro.ttf");
src: local(Futura-Medium),

View File

@@ -1,5 +1,5 @@
@import "imports/urls.less";
@import "http://localhost:8081/browser/less/imports/urls2.less";
@import "http://localhost:8081/test/browser/less/imports/urls2.less";
@font-face {
src: url("/fonts/garamond-pro.ttf");
src: local(Futura-Medium),

View File

@@ -1,24 +1,26 @@
describe("less.js browser behaviour", function() {
testLessEqualsInDocument();
var less = {};
it("has some log messages", function() {
expect(logMessages.length).toBeGreaterThan(0);
});
// There originally run inside describe method. However, since they have not
// been inside it, they run at jasmine compile time (not runtime). It all
// worked cause less.js was in async mode and custom phantom runner had
// different setup then grunt-contrib-jasmine. They have been created before
// less.js run, even as they have been defined in spec.
// test inline less in style tags by grabbing an assortment of less files and doing `@import`s
var testFiles = ['charsets', 'colors', 'comments', 'css-3', 'strings', 'media', 'mixins'],
testSheets = [];
// test inline less in style tags by grabbing an assortment of less files and doing `@import`s
var testFiles = ['charsets', 'colors', 'comments', 'css-3', 'strings', 'media', 'mixins'],
testSheets = [];
// setup style tags with less and link tags pointing to expected css output
for (var i = 0; i < testFiles.length; i++) {
var file = testFiles[i],
lessPath = '/less/' + file + '.less',
cssPath = '/css/' + file + '.css',
lessPath = '/test/less/' + file + '.less',
cssPath = '/test/css/' + file + '.css',
lessStyle = document.createElement('style'),
cssLink = document.createElement('link'),
lessText = '@import "' + lessPath + '";';
lessStyle.type = 'text/less';
lessStyle.id = file;
lessStyle.href = file;
if (lessStyle.styleSheet) {
lessStyle.styleSheet.cssText = lessText;
} else {
@@ -35,8 +37,4 @@ describe("less.js browser behaviour", function() {
testSheets[i] = lessStyle;
}
for (var i = 0; i < testFiles.length; i++) {
var sheet = testSheets[i];
testSheet(sheet);
}
});

View File

@@ -0,0 +1,12 @@
describe("less.js browser behaviour", function() {
testLessEqualsInDocument();
it("has some log messages", function() {
expect(logMessages.length).toBeGreaterThan(0);
});
for (var i = 0; i < testFiles.length; i++) {
var sheet = testSheets[i];
testSheet(sheet);
}
});

View File

@@ -0,0 +1,3 @@
var less = {};
less.strictUnits = true;

View File

@@ -0,0 +1,4 @@
describe("less.js error tests", function() {
testLessErrorsInDocument();
});

View File

@@ -1,5 +0,0 @@
less.strictUnits = true;
describe("less.js error tests", function() {
testLessErrorsInDocument();
});

View File

@@ -0,0 +1,4 @@
var less = {};
less.strictMath = false;
less.strictUnits = false;

View File

@@ -1,6 +1,3 @@
less.strictMath = false;
less.strictUnits = false;
describe("less.js legacy tests", function() {
testLessEqualsInDocument();
});
});

View File

@@ -1,3 +1,4 @@
var less = {};
less.functions = {
add: function (a, b) {
return new(less.tree.Dimension)(a.value + b.value);
@@ -10,6 +11,3 @@ less.functions = {
}
};
describe("less.js main tests", function() {
testLessEqualsInDocument();
});

View File

@@ -0,0 +1,3 @@
describe("less.js main tests", function() {
testLessEqualsInDocument();
});

View File

@@ -0,0 +1,2 @@
var less = {};

View File

@@ -0,0 +1,39 @@
var alreadyRun = false;
describe("less.js modify vars", function() {
beforeEach(function(){
// simulating "setUp" or "beforeAll" method
var lessOutputObj;
if (alreadyRun)
return ;
alreadyRun = true;
// wait until the sheet is compiled first time
waitsFor(function() {
lessOutputObj = document.getElementById("less:test-less-simple");
return lessOutputObj!==null;
}, "first generation of less:test-less-simple", 7000);
// modify variables
runs(function() {
lessOutputObj.type="not compiled yet";
less.modifyVars({var1: "green", var2: "purple"});
});
// wait until variables are modified
waitsFor(function() {
lessOutputObj = document.getElementById("less:test-less-simple");
return lessOutputObj!==null && lessOutputObj.type==="text/css";
}, "second generation of less:test-less-simple", 7000);
});
testLessEqualsInDocument();
it("Should log only 2 XHR requests", function() {
var xhrLogMessages = logMessages.filter(function(item) {
return /XHR: Getting '/.test(item);
})
expect(xhrLogMessages.length).toEqual(2);
});
});

View File

@@ -1,14 +0,0 @@
setTimeout(function(){
less.modifyVars({var1: "green", var2: "purple"});
}, 1000);
describe("less.js modify vars", function() {
testLessEqualsInDocument();
it("Should log only 2 XHR requests", function() {
var xhrLogMessages = logMessages.filter(function(item) {
return /XHR: Getting '/.test(item);
})
expect(xhrLogMessages.length).toEqual(2);
});
});

View File

@@ -0,0 +1,4 @@
var less = {};
less.strictUnits = true;
less.javascriptEnabled = false;

View File

@@ -0,0 +1,4 @@
describe("less.js javascript disabled error tests", function() {
testLessErrorsInDocument();
});

View File

@@ -1,6 +0,0 @@
less.strictUnits = true;
less.javascriptEnabled = false;
describe("less.js javascript disabled error tests", function() {
testLessErrorsInDocument();
});

View File

@@ -0,0 +1,3 @@
var less = {};
less.env = "production";

View File

@@ -1,7 +1,5 @@
less.env = "production";
describe("less.js production behaviour", function() {
it("doesn't log any messages", function() {
expect(logMessages.length).toEqual(0);
});
});
});

View File

@@ -0,0 +1,3 @@
var less = {};
less.relativeUrls = true;

View File

@@ -1,4 +1,3 @@
less.relativeUrls = true;
describe("less.js browser test - relative url's", function() {
testLessEqualsInDocument();
});
});

View File

@@ -0,0 +1,3 @@
var less = {};
less.rootpath = "https://www.github.com/";

View File

@@ -0,0 +1,4 @@
var less = {};
less.rootpath = "https://www.github.com/cloudhead/less.js/";
less.relativeUrls = true;

View File

@@ -1,5 +1,3 @@
less.rootpath = "https://www.github.com/cloudhead/less.js/";
less.relativeUrls = true;
describe("less.js browser test - rootpath and relative url's", function() {
testLessEqualsInDocument();
});
});

View File

@@ -1,4 +1,3 @@
less.rootpath = "https://www.github.com/";
describe("less.js browser test - rootpath url's", function() {
testLessEqualsInDocument();
});
});

View File

@@ -0,0 +1,41 @@
<!doctype html>
<%
var generateScriptsTasgs = function(allScripts) {
allScripts.forEach(function(script){ %> <script src="<%= script %>"></script>
<% });
};
%>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<!-- less and css links -->
<%
scripts.src.forEach(function(fullLessName) {
var pathParts = fullLessName.split('/'),
fullCssName = fullLessName.replace(/less/g, 'css'),
lessName = pathParts[pathParts.length-1],
name = lessName.split('.')[0];
%>
<link id="original-less:test-less-<%=name%>" title="test-less-<%=name%>" rel="stylesheet/less" type="text/css" href="<%=fullLessName%>">
<link id="expected-less:test-less-<%=name%>" rel="stylesheet" type="text/css" href="<%=fullCssName%>"><%
});
%>
<!-- grunt-contrib-jasmine css -->
<% css.forEach(function(style){ %>
<link rel="stylesheet" type="text/css" href="<%= style %>">
<% }) %>
<!-- inital grunt-contrib-jasmine scripts -->
<% generateScriptsTasgs([].concat(scripts.polyfills, scripts.jasmine)); %>
<!-- less.js testing -->
<% generateScriptsTasgs([].concat(scripts.vendor, scripts.helpers));
%> <script src="less.js"></script>
<% generateScriptsTasgs(scripts.specs); %>
<!-- final grunt-contrib-jasmine scripts -->
<% generateScriptsTasgs([].concat(scripts.reporters, scripts.start)); %>
</head>
<body>
</body>
</html>

View File

@@ -1,260 +0,0 @@
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 isVerbose = process.env.npm_config_loglevel === 'verbose';
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/",
testErrors, null, getErrorPathReplacementFunction("errors"));
runTestSet({strictMath: true, strictUnits: true, javascriptEnabled: false}, "no-js-errors/",
testErrors, null, getErrorPathReplacementFunction("no-js-errors"));
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/");
runTestSet({strictMath: true, strictUnits: true, sourceMap: true }, "sourcemaps/",
testSourcemap, null, null, function(filename) { return path.join('test/sourcemaps', filename) + '.json'; });
testNoOptions();
function getErrorPathReplacementFunction(dir) {
return function(input) {
return input.replace(
"{path}", path.join(process.cwd(), "/test/less/" + dir + "/"))
.replace("{pathrel}", path.join("test", "less", dir + "/"))
.replace("{pathhref}", "")
.replace("{404status}", "")
.replace(/\r\n/g, '\n');
};
}
function testSourcemap(name, err, compiledLess, doReplacements, sourcemap) {
fs.readFile(path.join('test/', name) + '.json', 'utf8', function (e, expectedSourcemap) {
sys.print("- " + name + ": ");
if (sourcemap === expectedSourcemap) {
ok('OK');
} else if (err) {
fail("ERROR: " + (err && err.message));
if (isVerbose) {
console.error();
console.error(err.stack);
}
} else {
difference("FAIL", expectedSourcemap, sourcemap);
}
sys.puts("");
});
}
function testErrors(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("");
});
}
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, getFilename) {
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++;
if (options.sourceMap) {
var sourceMapOutput;
options.writeSourceMap = function(output) {
sourceMapOutput = output;
};
options.sourceMapOutputFilename = name + ".css";
options.sourceMapBasepath = path.join(process.cwd(), "test/less");
options.sourceMapRootpath = "testweb/";
}
toCSS(options, path.join('test/less/', foldername + file), function (err, less) {
if (verifyFunction) {
return verifyFunction(name, err, less, doReplacements, sourceMapOutput);
}
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));
if (isVerbose) {
console.error();
console.error(err.stack);
}
} 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) {
var text = item.value.replace("\n", String.fromCharCode(182) + "\n");
sys.print(stylize(text, 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 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"));
}