|
| 1 | +// Setup chai assert - for the browser |
| 2 | +var assert = assert || chai.assert; |
| 3 | + |
| 4 | +// |
| 5 | +// I use this hackerish "require" syntax that loads the scripts Synchrounous |
| 6 | +// BECAUSE this will make the test-suite script CONSISTENT in both node and browser. |
| 7 | +// |
| 8 | +/** |
| 9 | +* Minimal node.js compatible require() for the browser. |
| 10 | +* |
| 11 | +* For documentation, see Github: |
| 12 | +* https://github.com/trausti/TKRequire.js |
| 13 | +* |
| 14 | +* Heavily based on StackOverflow answers by Lucio M. Tato and Ilya Kharlamov in thread: |
| 15 | +* http://stackoverflow.com/questions/6971583/node-style-require-for-in-browser-javascript |
| 16 | +* |
| 17 | +* MIT license. |
| 18 | +*/ |
| 19 | +function require(url) { |
| 20 | + if (url.toLowerCase().substr(-3)!=='.js') { |
| 21 | + url+='.js'; // To allow loading without js suffix. |
| 22 | + } |
| 23 | + if (!require.cache) { |
| 24 | + require.cache=[]; // Init cache. |
| 25 | + } |
| 26 | + |
| 27 | + if (!require.relativePath) { |
| 28 | + require.relativePath = ''; |
| 29 | + //console.log("TKRequire: initializing relativePath"); |
| 30 | + } |
| 31 | + var originalPath = require.relativePath; |
| 32 | + if ("http" === url.substr(0, 4)) { |
| 33 | + // If full href is given, extract relative path, if any. |
| 34 | + var baseDir = window.location.href.substring(0, window.location.href.lastIndexOf('/')); |
| 35 | + var scriptDir = url.substring(0, url.lastIndexOf('/')); |
| 36 | + if (0 == url.indexOf(baseDir)) { |
| 37 | + require.relativePath = scriptDir.substring(baseDir.length + 1) + '/'; |
| 38 | + //console.log("TKRequire: extractiong relative path" + require.relativePath); |
| 39 | + } |
| 40 | + } else if ("./" === url.substr(0, 2)) { |
| 41 | + require.relativePath = require.relativePath + url.substring(2, url.lastIndexOf('/') + 1); |
| 42 | + //console.log("TKRequire: Extending Path : " + require.relativePath); |
| 43 | + } |
| 44 | + var scriptName = url.substring(url.lastIndexOf('/') + 1); |
| 45 | + //console.log("TKRequire: scriptName :" + scriptName); |
| 46 | + |
| 47 | + var exports = require.cache[url]; // Get from cache. |
| 48 | + if (!exports) { // Not cached. |
| 49 | + try { |
| 50 | + exports = {}; |
| 51 | + var X = new XMLHttpRequest(); |
| 52 | + var fullOrRelativePath = ""; |
| 53 | + if ("http" === url.substr(0, 4)) { |
| 54 | + fullOrRelativePath = url; |
| 55 | + } else { |
| 56 | + fullOrRelativePath = "./" + require.relativePath + scriptName; |
| 57 | + }; |
| 58 | + //console.log("TKRequire: including: " + fullOrRelativePath); |
| 59 | + X.open("GET", fullOrRelativePath, false); // Synchrounous load. |
| 60 | + X.send(); |
| 61 | + if (X.status && X.status !== 200) { |
| 62 | + throw new Error(X.statusText); |
| 63 | + } |
| 64 | + var source = X.responseText; |
| 65 | + // Fix (if saved form for Chrome Dev Tools) |
| 66 | + if (source.substr(0, 10)==="(function(") { |
| 67 | + var moduleStart = source.indexOf('{'); |
| 68 | + var moduleEnd = source.lastIndexOf('})'); |
| 69 | + var CDTcomment = source.indexOf('//@ '); |
| 70 | + if (CDTcomment >- 1 && CDTcomment < moduleStart + 6) { |
| 71 | + moduleStart = source.indexOf('\n', CDTcomment); |
| 72 | + } |
| 73 | + source = source.slice(moduleStart + 1, moduleEnd - 1); |
| 74 | + } |
| 75 | + // Fix, add comment to show source on Chrome Dev Tools |
| 76 | + source = "//@ sourceURL=" + window.location.origin + url + "\n" + source; |
| 77 | + //------ |
| 78 | + var module = { id: url, uri: url, exports: exports }; // According to node.js modules |
| 79 | + // Create a Fn with module code, and 3 params: require, exports & module |
| 80 | + var anonFn = new Function("require", "exports", "module", source); |
| 81 | + anonFn(require, exports, module); // Call the Fn, Execute the module |
| 82 | + require.cache[url] = exports = module.exports; // Cache obj exported by module. |
| 83 | + } catch (err) { |
| 84 | + throw new Error("Error loading module " + url + ": " + err); |
| 85 | + } |
| 86 | + } |
| 87 | + // Restore the relative path. |
| 88 | + require.relativePath = originalPath; |
| 89 | + |
| 90 | + return exports; // Require returns object exported by module |
| 91 | +} |
| 92 | +require.relativePath = './test/' |
| 93 | + |
| 94 | +// Load all the test suites |
| 95 | +require("./run-suite.js"); |
0 commit comments