diff options
author | Johannes <johannes.ewald@roomieplanet.de> | 2012-07-10 22:32:54 +0400 |
---|---|---|
committer | Johannes <johannes.ewald@roomieplanet.de> | 2012-07-10 22:32:54 +0400 |
commit | 0bb70adf091ce5ccf8c81f28cc72a229ac15eba1 (patch) | |
tree | 61ef88ea9646dd734533e295d7002653c311508e /lib | |
parent | b443c61ab73679410f083e383be7aef3039d228f (diff) |
- fixed parsing error when trying to set a function as mockv0.3.2
- update to mocha 1.3.x
- fixed minor IE issues
Diffstat (limited to 'lib')
-rw-r--r-- | lib/__set__.js | 86 | ||||
-rw-r--r-- | lib/getImportGlobalsSrc.js | 58 |
2 files changed, 69 insertions, 75 deletions
diff --git a/lib/__set__.js b/lib/__set__.js index 6e02727..66e8c0a 100644 --- a/lib/__set__.js +++ b/lib/__set__.js @@ -1,46 +1,42 @@ -/**
- * This function will be stringified and then injected into every rewired module.
- * Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
- *
- * All variables within this function are namespaced in the arguments array because every
- * var declaration could possibly clash with a variable in the module scope.
- *
- * @param {!String|!Object} varName name of the variable to set
- * @param {String} varValue new value
- * @throws {TypeError}
- * @throws {ReferenceError} When the variable is unknown
- * @return {*}
- */
-function __set__() {
- arguments.varName = arguments[0];
- arguments.varValue = arguments[1];
- arguments.src = "";
- arguments.checkExistsSrc = function (varName, varValue) {
- return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('Cannot __set__(" + varName + ", " + varValue + "): " +
- varName + " is not declared within the module.'); } ";
- };
-
- if (typeof arguments[0] === "object" && arguments.length === 1) {
- arguments.env = arguments.varName;
- if (!arguments.env || Array.isArray(arguments.env)) {
- throw new TypeError("__set__ expects an object as env");
- }
- for (arguments.varName in arguments.env) {
- if (arguments.env.hasOwnProperty(arguments.varName)) {
- arguments.varValue = arguments.env[arguments.varName];
- arguments.src += arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.env." + arguments.varName + ";";
- }
- }
- } else if (typeof arguments.varName === "string" && arguments.length === 2) {
- if (!arguments.varName) {
- throw new TypeError("__set__ expects a non-empty string as a variable name");
- }
- arguments.src = arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.varValue;";
- } else {
- throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
- }
-
- eval(arguments.src);
-}
-
+/** + * This function will be stringified and then injected into every rewired module. + * Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue); + * + * All variables within this function are namespaced in the arguments array because every + * var declaration could possibly clash with a variable in the module scope. + * + * @param {!String|!Object} varName name of the variable to set + * @param {String} varValue new value + * @throws {TypeError} + * @throws {ReferenceError} When the variable is unknown + * @return {*} + */ +function __set__() { + arguments.varName = arguments[0]; + arguments.varValue = arguments[1]; + arguments.src = ""; + + if (typeof arguments[0] === "object" && arguments.length === 1) { + arguments.env = arguments.varName; + if (!arguments.env || Array.isArray(arguments.env)) { + throw new TypeError("__set__ expects an object as env"); + } + for (arguments.varName in arguments.env) { + if (arguments.env.hasOwnProperty(arguments.varName)) { + arguments.varValue = arguments.env[arguments.varName]; + arguments.src += arguments.varName + " = arguments.env." + arguments.varName + "; "; + } + } + } else if (typeof arguments.varName === "string" && arguments.length === 2) { + if (!arguments.varName) { + throw new TypeError("__set__ expects a non-empty string as a variable name"); + } + arguments.src = arguments.varName + " = arguments.varValue;"; + } else { + throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name"); + } + + eval(arguments.src); +} + module.exports = __set__;
\ No newline at end of file diff --git a/lib/getImportGlobalsSrc.js b/lib/getImportGlobalsSrc.js index f8c73c2..0e573f2 100644 --- a/lib/getImportGlobalsSrc.js +++ b/lib/getImportGlobalsSrc.js @@ -1,31 +1,29 @@ -/**
- * Declares all globals with a var and assigns the global object. Thus you're able to
- * override globals without changing the global object itself.
- *
- * Returns something like
- * "var console = console; var process = process; ..."
- *
- * @return {String}
- */
-function getImportGlobalsSrc(ignore) {
- var key,
- value,
- src = "",
- globalObj = typeof global === "undefined"? window: global;
-
- ignore = ignore || [];
-
- for (key in globalObj) {
- if (globalObj.hasOwnProperty === undefined || globalObj.hasOwnProperty(key)) { // in IE8 window.hasOwnProperty is undefined
- if (key !== "global" && ignore.indexOf(key) === -1) {
- value = globalObj[key];
- src += "var " + key + " = global." + key + "; ";
- }
- }
- }
-
-
- return src;
-}
-
+/** + * Declares all globals with a var and assigns the global object. Thus you're able to + * override globals without changing the global object itself. + * + * Returns something like + * "var console = console; var process = process; ..." + * + * @return {String} + */ +function getImportGlobalsSrc(ignore) { + var key, + value, + src = "", + globalObj = typeof global === "undefined"? window: global; + + ignore = ignore || []; + + for (key in globalObj) { + if (key !== "global" && ignore.indexOf(key) === -1) { // we don't use hasOwnProperty here because in some browsers not all global objects will be enumerated + value = globalObj[key]; + src += "var " + key + " = global." + key + "; "; + } + } + + + return src; +} + module.exports = getImportGlobalsSrc;
\ No newline at end of file |