1 /* record log messages for testing */
2 // var logAllIds = function() {
3 // var allTags = document.head.getElementsByTagName('style');
5 // for (var tg = 0; tg < allTags.length; tg++) {
6 // var tag = allTags[tg];
8 // console.log(tag.id);
14 realConsoleLog = console.log;
15 console.log = function(msg) {
16 logMessages.push(msg);
17 realConsoleLog.call(console, msg);
20 var testLessEqualsInDocument = function() {
21 testLessInDocument(testSheet);
24 var testLessErrorsInDocument = function(isConsole) {
25 testLessInDocument(isConsole ? testErrorSheetConsole : testErrorSheet);
28 var testLessInDocument = function(testFunc) {
29 var links = document.getElementsByTagName('link'),
30 typePattern = /^text\/(x-)?less$/;
32 for (var i = 0; i < links.length; i++) {
33 if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
34 (links[i].type.match(typePattern)))) {
40 var testSheet = function(sheet) {
41 it(sheet.id + " should match the expected output", function() {
42 var lessOutputId = sheet.id.replace("original-", ""),
43 expectedOutputId = "expected-" + lessOutputId,
46 expectedOutputHref = document.getElementById(expectedOutputId).href,
47 expectedOutput = loadFile(expectedOutputHref);
49 // Browser spec generates less on the fly, so we need to loose control
51 lessOutputObj = document.getElementById(lessOutputId);
52 // the type condition is necessary because of inline browser tests
53 return lessOutputObj !== null && lessOutputObj.type === "text/css";
54 }, "generation of " + lessOutputId + "", 700);
57 lessOutput = lessOutputObj.innerText;
61 return expectedOutput.loaded;
62 }, "failed to load expected outout", 10000);
65 // use sheet to do testing
66 expect(expectedOutput.text).toEqual(lessOutput);
71 //TODO: do it cleaner - the same way as in css
73 function extractId(href) {
74 return href.replace(/^[a-z-]+:\/+?[^\/]+/, '') // Remove protocol & domain
75 .replace(/^\//, '') // Remove root /
76 .replace(/\.[a-zA-Z]+$/, '') // Remove simple extension
77 .replace(/[^\.\w-]+/g, '-') // Replace illegal characters
78 .replace(/\./g, ':'); // Replace dots with colons(for valid id)
81 var testErrorSheet = function(sheet) {
82 it(sheet.id + " should match an error", function() {
83 var lessHref = sheet.href,
84 id = "less-error-message:" + extractId(lessHref),
85 // id = sheet.id.replace(/^original-less:/, "less-error-message:"),
86 errorHref = lessHref.replace(/.less$/, ".txt"),
87 errorFile = loadFile(errorHref),
91 // Less.js sets 10ms timer in order to add error message on top of page.
93 actualErrorElement = document.getElementById(id);
94 return actualErrorElement !== null;
95 }, "error message was not generated", 70);
98 actualErrorMsg = actualErrorElement.innerText
99 .replace(/\n\d+/g, function(lineNo) {
102 .replace(/\n\s*in /g, " in ")
103 .replace("\n\n", "\n");
106 waitsFor(function() {
107 return errorFile.loaded;
108 }, "failed to load expected outout", 10000);
111 var errorTxt = errorFile.text
112 .replace("{path}", "")
113 .replace("{pathrel}", "")
114 .replace("{pathhref}", "http://localhost:8081/test/less/errors/")
115 .replace("{404status}", " (404)");
116 expect(errorTxt).toEqual(actualErrorMsg);
117 if (errorTxt == actualErrorMsg) {
118 actualErrorElement.style.display = "none";
124 var testErrorSheetConsole = function(sheet) {
125 it(sheet.id + " should match an error", function() {
126 var lessHref = sheet.href,
127 id = sheet.id.replace(/^original-less:/, "less-error-message:"),
128 errorHref = lessHref.replace(/.less$/, ".txt"),
129 errorFile = loadFile(errorHref),
130 actualErrorElement = document.getElementById(id),
131 actualErrorMsg = logMessages[logMessages.length - 1];
133 describe("the error", function() {
134 expect(actualErrorElement).toBe(null);
138 /*actualErrorMsg = actualErrorElement.innerText
139 .replace(/\n\d+/g, function(lineNo) { return lineNo + " "; })
140 .replace(/\n\s*in /g, " in ")
141 .replace("\n\n", "\n");*/
143 waitsFor(function() {
144 return errorFile.loaded;
145 }, "failed to load expected outout", 10000);
148 var errorTxt = errorFile.text
149 .replace("{path}", "")
150 .replace("{pathrel}", "")
151 .replace("{pathhref}", "http://localhost:8081/browser/less/")
152 .replace("{404status}", " (404)")
154 expect(errorTxt).toEqual(actualErrorMsg);
159 var loadFile = function(href) {
160 var request = new XMLHttpRequest(),
165 request.open('GET', href, true);
166 request.onload = function(e) {
167 response.text = request.response.replace(/\r/g, "");
168 response.loaded = true;
175 var jasmineEnv = jasmine.getEnv();
176 jasmineEnv.updateInterval = 1000;
178 var htmlReporter = new jasmine.HtmlReporter();
180 jasmineEnv.addReporter(htmlReporter);
182 jasmineEnv.specFilter = function(spec) {
183 return htmlReporter.specFilter(spec);
186 var currentWindowOnload = window.onload;
188 window.onload = function() {
189 if (currentWindowOnload) {
190 currentWindowOnload();
195 function execJasmine() {
196 setTimeout(function() {
197 jasmineEnv.execute();