source: pyenvjasmine/pyenvjasmine/envjasmine/lib/envjasmine.js@ 0:0175515fceea

Last change on this file since 0:0175515fceea was 0:0175515fceea, checked in by Borja Lopez <borja@…>, 9 years ago

Imported sources from http://repos.betabug.ch/pyenvjasmine

File size: 9.0 KB
Line 
1/*
2 EnvJasmine: Jasmine test runner for EnvJS.
3
4 EnvJasmine allows you to run headless JavaScript tests.
5
6 Based on info from:
7 http://agile.dzone.com/news/javascript-bdd-jasmine-without
8 http://www.mozilla.org/rhino/
9 http://www.envjs.com/
10 http://pivotal.github.com/jasmine/
11 https://github.com/velesin/jasmine-jquery
12*/
13
14importPackage(java.lang);
15importPackage(java.io);
16importPackage(org.mozilla.javascript);
17
18// Create the EnvJasmine namespace
19if (!this.EnvJasmine) {
20 this.EnvJasmine = {};
21}
22
23EnvJasmine.cx = Context.getCurrentContext();
24EnvJasmine.cx.setOptimizationLevel(-1);
25EnvJasmine.topLevelScope = this;
26
27EnvJasmine.about = function () {
28 print("usage: envjasmine.js [options] [spec_file...]");
29 print("");
30 print("options:");
31 print(" --configFile=<File> Set a config file to run before specs are executed");
32 print(" --disableColor Disable console colors (colors are not available in Windows)");
33 print(" --environment=<WIN|UNIX> Set the environment to UNIX or Windows");
34 print(" --help This list");
35 print(" --incrementalOutput Disable the ./F output and print results for each spec file");
36 print(" --rootDir=<Dir> Set the EnvJasmine root directory (REQUIRED)");
37 print(" --suppressConsoleMsgs Suppress window.console messages");
38 print(" --testDir=<Dir> Set the directory with the specs/ directory (REQUIRED)");
39 print(" --customSpecsDir=<Dir> Sets the name of the specs directory, default is 'specs'");
40};
41
42EnvJasmine.normalizePath = function(path) {
43 var endsInSlash = (path.slice(-1) == "/");
44
45 if (path.slice(0, 1) == ".") {
46 path = EnvJasmine.rootDir + "/" + path;
47 }
48
49 return File(path).getCanonicalPath() + (endsInSlash ? "/" : "");
50};
51
52EnvJasmine.loadFactory = function(scope) {
53 return function (path) {
54 var fileIn,
55 normalizedPath = EnvJasmine.normalizePath(path);
56
57 try {
58 fileIn = new FileReader(normalizedPath);
59 EnvJasmine.cx.evaluateReader(scope, fileIn, normalizedPath, 0, null);
60 } catch (e) {
61 print('Could not read file: ' + normalizedPath );
62 } finally {
63 fileIn.close();
64 }
65 };
66};
67EnvJasmine.loadGlobal = EnvJasmine.loadFactory(EnvJasmine.topLevelScope);
68
69EnvJasmine.setRootDir = function (rootDir) {
70 // These are standard directories in the EnvJasmine project.
71 EnvJasmine.rootDir = EnvJasmine.normalizePath(rootDir);
72 EnvJasmine.libDir = EnvJasmine.normalizePath(EnvJasmine.rootDir + "/lib/");
73 EnvJasmine.includeDir = EnvJasmine.normalizePath(EnvJasmine.rootDir + "/include/");
74
75 // This is the standard spec suffix
76 EnvJasmine.specSuffix = new RegExp(/.spec.js$/);
77
78 // Load the default dirs and files, these can be overridden with command line options
79 EnvJasmine.configFile = EnvJasmine.normalizePath(EnvJasmine.includeDir + "dependencies.js");
80};
81
82EnvJasmine.setTestDir = function (testDir, override) {
83 if (typeof EnvJasmine.testDir === "undefined" || !EnvJasmine.testDir || override) {
84 EnvJasmine.testDir = EnvJasmine.normalizePath(testDir);
85 EnvJasmine.mocksDir = EnvJasmine.normalizePath(EnvJasmine.testDir + "/mocks/");
86 EnvJasmine.specsDir = EnvJasmine.normalizePath(EnvJasmine.testDir + "/specs/");
87 }
88};
89
90// Process command line options
91
92(function(argumentList) {
93 var arg, nameValue, spec = "", specLoc;
94
95 EnvJasmine.specs = [];
96 EnvJasmine.passedCount = 0;
97 EnvJasmine.failedCount = 0;
98 EnvJasmine.totalCount = 0;
99
100 for (var i = 0; i < argumentList.length; i++) {
101 arg = argumentList[i];
102
103 if (arg.slice(0, 2) == "--") {
104 nameValue = arg.slice(2).split('=');
105
106 switch(nameValue[0]) {
107 case "testDir":
108 EnvJasmine.setTestDir(nameValue[1], true);
109 break;
110 case "rootDir":
111 EnvJasmine.setRootDir(nameValue[1]);
112 EnvJasmine.setTestDir(nameValue[1]); // Set the root as the default testDir.
113 break;
114 case "environment":
115 EnvJasmine.environment = nameValue[1];
116 break;
117 case "configFile":
118 EnvJasmine.configFile = EnvJasmine.normalizePath(nameValue[1]);
119 break;
120 case "disableColor":
121 EnvJasmine.disableColorOverride = true;
122 break;
123 case "incrementalOutput":
124 EnvJasmine.incrementalOutput = true;
125 break;
126 case "suppressConsoleMsgs":
127 EnvJasmine.suppressConsoleMsgs = true;
128 break;
129 case "customSpecsDir":
130 EnvJasmine.customSpecsDir = nameValue[1];
131 break;
132 case "help":
133 EnvJasmine.about();
134 System.exit(0);
135 default:
136 print("Unknown option: " + arg);
137 break;
138 }
139 } else {
140 if (arg.slice(-3) !== ".js") {
141 spec += arg + " ";
142 } else {
143 spec += arg;
144 if (arg[0] === "/" || (arg[1] === ":" && arg[2] === "\\")) {
145 specLoc = spec;
146 } else {
147 specLoc = EnvJasmine.testDir + "/" + spec
148 }
149 print(specLoc);
150 EnvJasmine.specs.push(EnvJasmine.normalizePath(specLoc));
151 spec = "";
152 }
153 }
154 }
155}(arguments));
156
157if (typeof EnvJasmine.customSpecsDir !== "undefined") {
158 EnvJasmine.specsDir = EnvJasmine.normalizePath(EnvJasmine.testDir + EnvJasmine.customSpecsDir);
159}
160
161if (typeof EnvJasmine.rootDir == "undefined" || typeof EnvJasmine.environment == "undefined") {
162 EnvJasmine.about();
163 System.exit(1);
164}
165
166EnvJasmine.SEPARATOR = (function (env) {
167 if (env == "UNIX") {
168 return "/";
169 } else if (env == "WIN") {
170 return "\\";
171 } else {
172 EnvJasmine.about();
173 System.exit(1);
174 }
175}(EnvJasmine.environment));
176
177EnvJasmine.disableColor = (function (env) {
178 return EnvJasmine.disableColorOverride || (env == "WIN");
179}(EnvJasmine.environment));
180
181(function() {
182 if (EnvJasmine.disableColor) {
183 EnvJasmine.green = function(msg) { return msg; };
184 EnvJasmine.red = function(msg) { return msg; };
185 EnvJasmine.plain = function(msg) { return msg; };
186 } else {
187 var green = "\033[32m",
188 red = "\033[31m",
189 end = "\033[0m";
190
191 EnvJasmine.green = function(msg) { return green + msg + end; };
192 EnvJasmine.red = function(msg) { return red + msg + end; };
193 EnvJasmine.plain = function(msg) { return msg; };
194 }
195}());
196EnvJasmine.results = [];
197
198EnvJasmine.loadConfig = function () {
199 EnvJasmine.loadGlobal(EnvJasmine.configFile);
200};
201
202(function() {
203 var i, fileIn, len;
204
205 EnvJasmine.loadConfig();
206
207 if (typeof EnvJasmine.reporterClass === "undefined") {
208 // Use the standard reporter
209 EnvJasmine.reporterClass = RhinoReporter;
210 }
211
212
213 jasmine.getEnv().addReporter(new EnvJasmine.reporterClass());
214
215 if (EnvJasmine.suppressConsoleMsgs === true) {
216 // suppress console messages
217 window.console = $.extend({}, window.console, {
218 info: jasmine.createSpy(),
219 log: jasmine.createSpy(),
220 debug: jasmine.createSpy(),
221 warning: jasmine.createSpy(),
222 error: jasmine.createSpy()
223 });
224 }
225
226 EnvJasmine.loadGlobal(EnvJasmine.libDir + "spanDir/spanDir.js");
227 if (EnvJasmine.specs.length == 0) {
228 spanDir(EnvJasmine.specsDir, function(spec) {
229 if (EnvJasmine.specSuffix.test(spec)) {
230 EnvJasmine.specs.push(spec);
231 }
232 });
233 }
234
235 for (i = 0, len = EnvJasmine.specs.length >>> 0; i < len; i += 1) {
236 try {
237 EnvJasmine.currentScope = {};
238 EnvJasmine.load = EnvJasmine.loadFactory(EnvJasmine.currentScope);
239 EnvJasmine.specFile = EnvJasmine.specs[i];
240 fileIn = new FileReader(EnvJasmine.specFile);
241 EnvJasmine.cx.evaluateReader(EnvJasmine.currentScope, fileIn, EnvJasmine.specs[i], 0, null);
242 EnvJasmine.cx.evaluateString(EnvJasmine.currentScope, 'window.location.assign(["file://", EnvJasmine.libDir, "envjasmine.html"].join(EnvJasmine.SEPARATOR));', 'Executing '+EnvJasmine.specs[i], 0, null);
243 }
244 finally {
245 fileIn.close();
246 }
247 }
248
249 if (EnvJasmine.results.length > 0) {
250 print("\n");
251 print(EnvJasmine.red(EnvJasmine.results.join("\n\n")));
252 }
253
254 print();
255 print(EnvJasmine[EnvJasmine.passedCount ? 'green' : 'plain']("Passed: " + EnvJasmine.passedCount));
256 print(EnvJasmine[EnvJasmine.failedCount ? 'red' : 'plain']("Failed: " + EnvJasmine.failedCount));
257 print(EnvJasmine.plain("Total : " + EnvJasmine.totalCount));
258
259 if (EnvJasmine.failedCount > 0) {
260 System.exit(1);
261 }
262}());
Note: See TracBrowser for help on using the repository browser.