minor code style
[redakcja.git] / apps / wiki / static--wiki--editor--node_modules--grunt-contrib-less--node_modules--less--test / browser / common.js
1 /* record log messages for testing */
2 // var logAllIds = function() {
3 //   var allTags = document.head.getElementsByTagName('style');
4 //   var ids = [];
5 //   for (var tg = 0; tg < allTags.length; tg++) {
6 //     var tag = allTags[tg];
7 //     if (tag.id) {
8 //       console.log(tag.id);
9 //     }
10 //   }
11 // };
12
13 var logMessages = [],
14   realConsoleLog = console.log;
15 console.log = function(msg) {
16   logMessages.push(msg);
17   realConsoleLog.call(console, msg);
18 };
19
20 var testLessEqualsInDocument = function() {
21   testLessInDocument(testSheet);
22 };
23
24 var testLessErrorsInDocument = function(isConsole) {
25     testLessInDocument(isConsole ? testErrorSheetConsole : testErrorSheet);
26 };
27
28 var testLessInDocument = function(testFunc) {
29   var links = document.getElementsByTagName('link'),
30     typePattern = /^text\/(x-)?less$/;
31
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)))) {
35       testFunc(links[i]);
36     }
37   }
38 };
39
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,
44       lessOutputObj,
45       lessOutput,
46       expectedOutputHref = document.getElementById(expectedOutputId).href,
47       expectedOutput = loadFile(expectedOutputHref);
48
49     // Browser spec generates less on the fly, so we need to loose control
50     waitsFor(function() {
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);
55
56     runs(function() {
57       lessOutput = lessOutputObj.innerText;
58     });
59
60     waitsFor(function() {
61       return expectedOutput.loaded;
62     }, "failed to load expected outout", 10000);
63
64     runs(function() {
65       // use sheet to do testing
66             expect(expectedOutput.text).toEqual(lessOutput);
67     });
68   });
69 };
70
71 //TODO: do it cleaner - the same way as in css
72
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)
79 }
80
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),
88       actualErrorElement,
89       actualErrorMsg;
90
91     // Less.js sets 10ms timer in order to add error message on top of page.
92     waitsFor(function() {
93       actualErrorElement = document.getElementById(id);
94       return actualErrorElement !== null;
95     }, "error message was not generated", 70);
96
97     runs(function() {
98       actualErrorMsg = actualErrorElement.innerText
99         .replace(/\n\d+/g, function(lineNo) {
100         return lineNo + " ";
101       })
102         .replace(/\n\s*in /g, " in ")
103         .replace("\n\n", "\n");
104     });
105
106     waitsFor(function() {
107       return errorFile.loaded;
108     }, "failed to load expected outout", 10000);
109
110     runs(function() {
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";
119       }
120         });
121     });
122 };
123
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];
132
133         describe("the error", function() {
134             expect(actualErrorElement).toBe(null);
135
136         });
137
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");*/
142
143         waitsFor(function() {
144             return errorFile.loaded;
145         }, "failed to load expected outout", 10000);
146
147         runs(function() {
148             var errorTxt = errorFile.text
149                 .replace("{path}", "")
150                 .replace("{pathrel}", "")
151                 .replace("{pathhref}", "http://localhost:8081/browser/less/")
152                 .replace("{404status}", " (404)")
153                 .trim();
154             expect(errorTxt).toEqual(actualErrorMsg);
155     });
156   });
157 };
158
159 var loadFile = function(href) {
160   var request = new XMLHttpRequest(),
161     response = {
162       loaded: false,
163       text: ""
164     };
165   request.open('GET', href, true);
166   request.onload = function(e) {
167     response.text = request.response.replace(/\r/g, "");
168     response.loaded = true;
169   };
170   request.send();
171   return response;
172 };
173
174 (function() {
175   var jasmineEnv = jasmine.getEnv();
176   jasmineEnv.updateInterval = 1000;
177
178   var htmlReporter = new jasmine.HtmlReporter();
179
180   jasmineEnv.addReporter(htmlReporter);
181
182   jasmineEnv.specFilter = function(spec) {
183     return htmlReporter.specFilter(spec);
184   };
185
186   var currentWindowOnload = window.onload;
187
188   window.onload = function() {
189     if (currentWindowOnload) {
190       currentWindowOnload();
191     }
192     execJasmine();
193   };
194
195   function execJasmine() {
196     setTimeout(function() {
197       jasmineEnv.execute();
198     }, 3000);
199   }
200
201 })();