Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit512548d

Browse files
committed
namespaced all variables within the __set__ method so you can really set all variables
1 parentd936fe1 commit512548d

File tree

6 files changed

+35
-21
lines changed

6 files changed

+35
-21
lines changed

‎lib/__get__.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*@return {*}
1010
*/
1111
module.exports=function__get__(name){
12-
if(typeofname!=="string"||name==false){
12+
if(typeofname!=="string"||name.length===0){
1313
thrownewTypeError("__get__ expects a non-empty string");
1414
}
1515

‎lib/__set__.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,40 @@
44
* This function will be stringified and then injected into every rewired module.
55
* Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
66
*
7+
* All variables within this function are namespaced in the arguments array because every
8+
* var declaration could possibly clash with a variable in the module scope.
9+
*
710
*@param {!String|!Object} varName name of the variable to set
811
*@param {String} varValue new value
912
*@throws {TypeError}
1013
*@return {*}
1114
*/
12-
module.exports=function__set__(varName,varValue){
13-
varkey,
14-
env,
15-
src="";
16-
17-
functioncheckExistsSrc(varName){
15+
module.exports=function__set__(){
16+
arguments.varName=arguments[0];
17+
arguments.varValue=arguments[1];
18+
arguments.src="";
19+
arguments.checkExistsSrc=function(varName){
1820
return"if (typeof "+varName+" === 'undefined') { throw new ReferenceError('"+varName+" is not defined');} ";
19-
}
21+
};
2022

21-
if(typeofvarName==="object"){
22-
env=varName;
23-
if(!env||Array.isArray(env)){
23+
if(typeofarguments[0]==="object"){
24+
arguments.env=arguments.varName;
25+
if(!arguments.env||Array.isArray(arguments.env)){
2426
thrownewTypeError("__set__ expects an object as env");
2527
}
26-
for(keyinenv){
27-
if(env.hasOwnProperty(key)){
28-
src+=checkExistsSrc(key)+key+" = env."+key+";";
28+
for(arguments.keyinarguments.env){
29+
if(arguments.env.hasOwnProperty(arguments.key)){
30+
arguments.src+=arguments.checkExistsSrc(arguments.key)+arguments.key+" =arguments.env."+arguments.key+";";
2931
}
3032
}
31-
}elseif(typeofvarName==="string"){
32-
if(!varName){
33+
}elseif(typeofarguments.varName==="string"){
34+
if(!arguments.varName){
3335
thrownewTypeError("__set__ expects a non-empty string as a variable name");
3436
}
35-
src=checkExistsSrc(varName)+varName+" = varValue;"
37+
arguments.src=arguments.checkExistsSrc(arguments.varName)+arguments.varName+" =arguments.varValue;";
3638
}else{
3739
thrownewTypeError("__set__ expects an environment object or a non-empty string as a variable name");
3840
}
3941

40-
eval(src);
42+
eval(arguments.src);
4143
};

‎package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" :"rewire",
3-
"version" :"0.2.0",
3+
"version" :"0.2.1",
44
"description" :"Dependency injection for node.js applications",
55
"keywords" : [
66
"dependency",
@@ -27,7 +27,7 @@
2727
"url":"git://github.com/jhnns/rewire.git"
2828
},
2929
"engines" : {
30-
"node" :"0.6.x"
30+
"node" :"0.8.x"
3131
},
3232
"devDependencies": {
3333
"mocha":"1.1.x",

‎test/__set__.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ describe("__set__", function () {
1919
beforeEach(function(){
2020
moduleFake={
2121
myNumber:0,// copy by value
22-
myObj:{}// copy by reference
22+
myObj:{},// copy by reference
23+
24+
// these variables are used within the set method
25+
// because there is a eval() statement within the set method
26+
// these variables should not override same-named vars of the module
27+
key:"key",
28+
env:"env",
29+
src:"src"
2330
};
2431

2532
vm.runInNewContext(
@@ -66,6 +73,9 @@ describe("__set__", function () {
6673
notExistingAsWell:"blabla"
6774
});
6875
}).to.throwException(expectReferenceError);
76+
});
77+
it("should not clash with vars used within the set method",function(){
78+
6979
});
7080
it("should throw a TypeError when passing misfitting params",function(){
7181
expect(function(){

‎test/rewire.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe("rewire", function () {
6363
expect(rewiredModuleA.getMyNumber()).to.be(2);
6464
rewiredModuleA.__set__("myObj",newObj);
6565
expect(rewiredModuleA.getMyObj()).to.be(newObj);
66+
rewiredModuleA.__set__("env","ENVENV");
6667
});
6768
it("should provide the ability to get private vars",function(){
6869
varrewiredModuleA=rewire(testModules.A);

‎test/testModules/moduleA.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
varsomeOtherModule=require("./someOtherModule.js"),
44
myNumber=0,// copy by value
55
myObj={},// copy by reference
6+
env="bla",
67
fs=require("fs");
78

89
// We need getters and setters for private vars to check if our injected setters and getters actual work

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp