8000 - fixed parsing error when trying to set a function as mock · ShMcK/rewire-coderoad@0bb70ad · GitHub
[go: up one dir, main page]

Skip to content

Commit 0bb70ad

Browse files
author
Johannes
committed
- fixed parsing error when trying to set a function as mock
- update to mocha 1.3.x - fixed minor IE issues
1 parent b443c61 commit 0bb70ad

File tree

11 files changed

+857
-835
lines changed

11 files changed

+857
-835
lines changed

lib/__set__.js

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,42 @@
1-
/**
2-
* This function will be stringified and then injected into every rewired module.
3-
* Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
4-
*
5-
* All variables within this function are namespaced in the arguments array because every
6-
* var declaration could possibly clash with a variable in the module scope.
7-
*
8-
* @param {!String|!Object} varName name of the variable to set
9-
* @param {String} varValue new value
10-
* @throws {TypeError}
11-
* @throws {ReferenceError} When the variable is unknown
12-
* @return {*}
13-
*/
14-
function __set__() {
15-
arguments.varName = arguments[0];
16-
arguments.varValue = arguments[1];
17-
arguments.src = "";
18-
arguments.checkExistsSrc = function (varName, varValue) {
19-
return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('Cannot __set__(" + varName + ", " + varValue + "): " +
20-
varName + " is not declared within the module.'); } ";
21-
};
22-
23-
if (typeof arguments[0] === "object" && arguments.length === 1) {
24-
arguments.env = arguments.varName;
25-
if (!arguments.env || Array.isArray(arguments.env)) {
26-
throw new TypeError("__set__ expects an object as env");
27-
}
28-
for (arguments.varName in arguments.env) {
29-
if (arguments.env.hasOwnProperty(arguments.varName)) {
30-
arguments.varValue = arguments.env[arguments.varName];
31-
arguments.src += arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.env." + arguments.varName + ";";
32-
}
33-
}
34-
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
35-
if (!arguments.varName) {
36-
throw new TypeError("__set__ expects a non-empty string as a variable name");
37-
}
38-
arguments.src = arguments.checkExistsSrc(arguments.varName, arguments.varValue) + arguments.varName + " = arguments.varValue;";
39-
} else {
40-
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
41-
}
42-
43-
eval(arguments.src);
44-
}
45-
1+
/**
2+
* This function will be stringified and then injected into every rewired module.
3+
* Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
4+
*
5+
* All variables within this function are namespaced in the arguments array because every
6+
* var declaration could possibly clash with a variable in the module scope.
7+
*
8+
* @param {!String|!Object} varName name of the variable to set
9+
* @param {String} varValue new value
10+
* @throws {TypeError}
11+
* @throws {ReferenceError} When the variable is unknown
12+
* @return {*}
13+
*/
14+
function __set__() {
15+
arguments.varName = arguments[0];
16+
arguments.varValue = arguments[1];
17+
arguments.src = "";
18+
19+
if (typeof arguments[0] === "object" && arguments.length === 1) {
20+
arguments.env = arguments.varName;
21+
if (!arguments.env || Array.isArray(arguments.env)) {
22+
throw new TypeError("__set__ expec 10000 ts an object as env");
23+
}
24+
for (arguments.varName in arguments.env) {
25+
if (arguments.env.hasOwnProperty(arguments.varName)) {
26+
arguments.varValue = arguments.env[arguments.varName];
27+
arguments.src += arguments.varName + " = arguments.env." + arguments.varName + "; ";
28+
}
29+
}
30+
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
31+
if (!arguments.varName) {
32+
throw new TypeError("__set__ expects a non-empty string as a variable name");
33+
}
34+
arguments.src = arguments.varName + " = arguments.varValue;";
35+
} else {
36+
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
37+
}
38+
39+
eval(arguments.src);
40+
}
41+
4642
module.exports = __set__;

lib/getImportGlobalsSrc.js

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
/**
2-
* Declares all globals with a var and assigns the global object. Thus you're able to
3-
* override globals without changing the global object itself.
4-
*
5-
* Returns something like
6-
* "var console = console; var process = process; ..."
7-
*
8-
* @return {String}
9-
*/
10-
function getImportGlobalsSrc(ignore) {
11-
var key,
12-
value,
13-
src = "",
14-
globalObj = typeof global === "undefined"? window: global;
15-
16-
ignore = ignore || [];
17-
18-
for (key in globalObj) {
19-
if (globalObj.hasOwnProperty === undefined || globalObj.hasOwnProperty(key)) { // in IE8 window.hasOwnProperty is undefined
20-
if (key !== "global" && ignore.indexOf(key) === -1) {
21-
value = globalObj[key];
22-
src += "var " + key + " = global." + key + "; ";
23-
}
24-
}
25-
}
26-
27-
28-
return src;
29-
}
30-
1+
/**
2+
* Declares all globals with a var and assigns the global object. Thus you're able to
3+
* override globals without changing the global object itself.
4+
*
5+
* Returns something like
6+
* "var console = console; var process = process; ..."
7+
*
8+
* @return {String}
9+
*/
10+
function getImportGlobalsSrc(ignore) {
11+
var key,
12+
value,
13+
src = "",
14+
globalObj = typeof global === "undefined"? window: global;
15+
16+
ignore = ignore || [];
17+
18+
for (key in globalObj) {
19+
if (key !== "global" && ignore.indexOf(key) === -1) { // we don't use hasOwnProperty here because in some browsers not all global objects will be enumerated
20+
value = globalObj[key];
21+
src += "var " + key + " = global." + key + "; ";
22+
}
23+
}
24+
25+
26+
return src;
27+
}
28+
3129
module.exports = getImportGlobalsSrc;

package.json

Lines changed: 39 additions & 39 deletions
Original file line numberDiff li 57AE ne numberDiff line change
@@ -1,40 +1,40 @@
1-
{
2-
"name" : "rewire",
3-
"version" : "0.3.1",
4-
"description" : "Dependency injection for node.js applications",
5-
"keywords" : [
6-
"dependency",
7-
"injection",
8-
"mock",
9-
"unit",
10-
"test",
11-
"leak",
12-
"inspect"
13-
],
14-
"author" : {
15-
"name" : "Johannes Ewald",
16-
"email" : "mail@johannesewald.de",
17-
"web" : "http://johannesewald.de"
18-
},
19-
"main" : "lib/index.js",
20-
"homepage": "http://jhnns.github.com/rewire",
21-
"bugs" : {
22-
"url" : "http://github.com/jhnns/rewire/issues",
23-
"email" : "mail@johannesewald.de"
24-
},
25-
"repository": {
26-
"type": "git",
27-
"url": "git://github.com/jhnns/rewire.git"
28-
},
29-
"engines" : {
30-
"node" : "<0.9.x"
31-
},
32-
"devDependencies": {
33-
"mocha": "1.2.x",
34-
"expect.js": "0.1.x",
35-
"browserify": ">=1.13.5 <1.14.x"
36-
},
37-
"scripts" : {
38-
"test" : "mocha -R spec"
39-
}
1+
{
2+
"name" : "rewire",
3+
"version" : "0.3.2",
4+
"description" : "Dependency injection for node.js applications",
5+
"keywords" : [
6+
"dependency",
7+
"injection",
8+
"mock",
9+
"unit",
10+
"test",
11+
"leak",
12+
"inspect"
13+
],
14+
"author" : {
15+
"name" : "Johannes Ewald",
16+
"email" : "mail@johannesewald.de",
17+
"web" : "http://johannesewald.de"
18+
},
19+
"main" : "lib/index.js",
20+
"homepage": "http://jhnns.github.com/rewire",
21+
"bugs" : {
22+
"url" : "http://github.com/jhnns/rewire/issues",
23+
"email" : "mail@johannesewald.de"
24+
},
25+
"repository": {
26+
"type": "git",
27+
"url": "git://github.com/jhnns/rewire.git"
28+
},
29+
"engines" : {
30+
"node" : "<0.9.x"
31+
},
32+
"devDependencies": {
33+
"mocha": "1.3.x",
34+
"expect.js": "0.1.x",
35+
"browserify": ">=1.13.5 <1.14.x"
36+
},
37+
"scripts" : {
38+
"test" : "node node_modules/mocha/bin/mocha -R spec"
39+
}
4040
}

test/__get__.test.js

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,79 @@
1-
var expect = require("expect.js"),
2-
vm = require("vm"),
3-
__get__ = require("../lib/__get__.js"),
4-
5-
expectReferenceError = expectError(ReferenceError),
6-
expectTypeError = expectError(TypeError);
7-
8-
function expectError(ErrConstructor) {
9-
return function expectReferenceError(err) {
10-
expect(err.constructor.name === ErrConstructor.name).to.be(true);
11-
};
12-
}
13-
14-
15-
describe("__get__", function () {
16-
var moduleFake;
17-
18-
beforeEach(function () {
19-
moduleFake = {
20-
__filename: "some/file.js",
21-
myNumber: 0,
22-
myObj: {}
23-
};
24-
25-
vm.runInNewContext(
26-
"__get__ = " + __get__.toString() + "; " +
27-
"setNumber = function (value) { myNumber = value; }; " +
28-
"setObj = function (value) { myObj = value; }; ",
29-
moduleFake,
30-
__filename
31-
);
32-
});
33-
it("should return the initial value", function () {
34-
expect(moduleFake.__get__("myNumber") === 0).to.be(true);
35-
expect(moduleFake.__get__("myObj")).to.eql({});
36-
});
37-
it("should return the changed value of the number", function () {
38-
var newObj = { hello: "hello" };
39-
40-
moduleFake.setNumber(2);
41-
moduleFake.setObj(newObj);
42-
expect(moduleFake.__get__("myNumber") === 2).to.be(true);
43-
expect(moduleFake.__get__("myObj") === newObj).to.be(true);
44-
});
45-
it("should throw a ReferenceError when getting not existing vars", function () {
46-
expect(function () {
47-
moduleFake.__get__("blabla");
48-
}).to.throwException(expectReferenceError);
49-
});
50-
it("should throw a TypeError when passing misfitting params", function () {
51-
expect(function () {
52-
moduleFake.__get__();
53-
}).to.throwException(expectTypeError);
54-
expect(function () {
55-
moduleFake.__get__(undefined);
56-
}).to.throwException(expectTypeError);
57-
expect(function () {
58-
moduleFake.__get__(null);
59-
}).to.throwException(expectTypeError);
60-
expect(function () {
61-
moduleFake.__get__(true);
62-
}).to.throwException(expectTypeError);
63-
expect(function () {
64-
moduleFake.__get__(2);
65-
}).to.throwException(expectTypeError);
66-
expect(function () {
67-
moduleFake.__get__("");
68-
}).to.throwException(expectTypeError);
69-
expect(function () {
70-
moduleFake.__get__([]);
71-
}).to.throwException(expectTypeError);
72-
expect(function () {
73-
moduleFake.__get__({});
74-
}).to.throwException(expectTypeError);
75-
expect(function () {
76-
moduleFake.__get__(function () {});
77-
}).to.throwException(expectTypeError);
78-
});
1+
var expect = require("expect.js"),
2+
vm = require("vm"),
3+
__get__ = require("../lib/__get__.js"),
4+
5+
expectReferenceError = expectError(ReferenceError),
6+
expectTypeError = expectError(TypeError);
7+
8+
function expectError(ErrConstructor) {
9+
return function expectReferenceError(err) {
10+
expect(err.constructor.name).to.be(ErrConstructor.name);
11+
};
12+
}
13+
14+
15+
describe("__get__", function () {
16+
var moduleFake;
17+
18+
beforeEach(function () {
19+
moduleFake = {
20+
__filename: "some/file.js",
21+
myNumber: 0,
22+
myObj: {}
23+
};
24+
25+
vm.runInNewContext(
26+
"__get__ = " + __get__.toString() + "; " +
27+
"setNumber = function (value) { myNumber = value; }; " +
28+
"setObj = function (value) { myObj = value; }; ",
29+
moduleFake,
30+
__filename
31+
);
32+
});
33+
it("should return the initial value", function () {
34+
expect(moduleFake.__get__("myNumber")).to.be(0);
35+
expect(moduleFake.__get__("myObj")).to.eql({});
36+
});
37+
it("should return the changed value of the number", function () {
38+
var newObj = { hello: "hello" };
39+
40+
moduleFake.setNumber(2);
41+
moduleFake.setObj(newObj);
42+
expect(moduleFake.__get__("myNumber")).to.be(2);
43+
expect(moduleFake.__get__("myObj")).to.be(newObj);
44+
});
45+
it("should throw a ReferenceError when getting not existing vars", function () {
46+
expect(function () {
47+
moduleFake.__get__("blabla");
48+
}).to.throwException(expectReferenceError);
49+
});
50+
it("should throw a TypeError when passing misfitting params", function () {
51+
expect(function () {
52+
moduleFake.__get__();
53+
}).to.throwException(expectTypeError);
54+
expect(function () {
55+
moduleFake.__get__(undefined);
56+
}).to.throwException(expectTypeError);
57+
expect(function () {
58+
moduleFake.__get__(null);
59+
}).to.throwException(expectTypeError);
60+
expect(function () {
61+
moduleFake.__get__(true);
62+
}).to.throwException(expectTypeError);
63+
expect(function () {
64+
moduleFake.__get__(2);
65+
}).to.throwException(expectTypeError);
66+
expect(function () {
67+
moduleFake.__get__("");
68+
}).to.throwException(expectTypeError);
69+
expect(function () {
70+
moduleFake.__get__([]);
71+
}).to.throwException(expectTypeError);
72+
expect(function () {
73+
moduleFake.__get__({});
74+
}).to.throwException(expectTypeError);
75+
expect(function () {
76+
moduleFake.__get__(function () {});
77+
}).to.throwException(expectTypeError);
78+
});
7979
});

0 commit comments

Comments
 (0)
0