common.js
3.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
/*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 logMessages = [],
realConsoleLog = console.log;
console.log = function(msg) {
logMessages.push(msg);
realConsoleLog.call(console, msg);
};
var testLessEqualsInDocument = function() {
testLessInDocument(testSheet);
};
var testLessErrorsInDocument = function() {
testLessInDocument(testErrorSheet);
};
var testLessInDocument = function(testFunc) {
var links = document.getElementsByTagName('link'),
typePattern = /^text\/(x-)?less$/;
for (var i = 0; i < links.length; i++) {
if (links[i].rel === 'stylesheet/less' || (links[i].rel.match(/stylesheet/) &&
(links[i].type.match(typePattern)))) {
testFunc(links[i]);
}
}
};
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,
expectedOutputHref = document.getElementById(expectedOutputId).href,
expectedOutput = loadFile(expectedOutputHref);
waitsFor(function() {
return expectedOutput.loaded;
}, "failed to load expected outout", 10000);
runs(function() {
// use sheet to do testing
expect(lessOutput).toEqual(expectedOutput.text);
});
});
};
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:"),
errorHref = lessHref.replace(/.less$/, ".txt"),
errorFile = loadFile(errorHref),
actualErrorElement = document.getElementById(id),
actualErrorMsg;
describe("the error", function() {
expect(actualErrorElement).not.toBe(null);
});
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;
}, "failed to load expected outout", 10000);
runs(function() {
var errorTxt = errorFile.text
.replace("{path}", "")
.replace("{pathrel}", "")
.replace("{pathhref}", "http://localhost:8081/less/errors/")
.replace("{404status}", " (404)");
expect(actualErrorMsg).toEqual(errorTxt);
if (errorTxt == actualErrorMsg) {
actualErrorElement.style.display = "none";
}
});
});
};
var loadFile = function(href) {
var request = new XMLHttpRequest(),
response = { loaded: false, text: ""};
request.open('GET', href, true);
request.onload = function(e) {
response.text = request.response.replace(/\r/g, "");
response.loaded = true;
}
request.send();
return response;
};
(function() {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function(spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function() {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
setTimeout(function() {
jasmineEnv.execute();
}, 3000);
}
})();