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

Commit71733c8

Browse files
committed
Move __with__-function into own module
1 parentba5edff commit71733c8

File tree

5 files changed

+136
-109
lines changed

5 files changed

+136
-109
lines changed

‎lib/__set__.js

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
*
88
*@param {String|Object} varName name of the variable to set
99
*@param {String} varValue new value
10-
*@throws {TypeError}
11-
*@throws {ReferenceError} When the variable is unknown
12-
*@return {*}
10+
*@return {Function}
1311
*/
1412
function__set__(){
1513
arguments.varName=arguments[0];
@@ -42,28 +40,8 @@ function __set__() {
4240
eval(arguments.src);
4341

4442
returnfunction(snapshot){
45-
__set__(snapshot);
43+
module.exports.__set__(snapshot);
4644
}.bind(null,arguments.snapshot);
4745
}
4846

49-
function__with__(){
50-
varargs=arguments;
51-
52-
returnfunction(callback){
53-
varundo;
54-
55-
if(typeofcallback!=="function"){
56-
thrownewTypeError("__with__ expects a callback function")
57-
}
58-
59-
undo=__set__.apply(null,args);
60-
61-
try{
62-
callback();
63-
}finally{
64-
undo();
65-
}
66-
}
67-
}
68-
69-
module.exports={"__set__":__set__,"__with__":__with__};
47+
module.exports=__set__;

‎lib/__with__.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict";
2+
3+
/**
4+
* This function will be stringified and then injected into every rewired module.
5+
*
6+
* Calling myModule.__with__("myPrivateVar", newValue) returns a function where
7+
* you can place your tests. As long as the returned function is executed variables
8+
* will be set to the given value, after that all changed variables are reset back to normal.
9+
*
10+
*@param {String|Object} varName name of the variable to set
11+
*@param {String} varValue new value
12+
*@return {Function}
13+
*/
14+
function__with__(){
15+
varargs=arguments;
16+
17+
returnfunction(callback){
18+
varundo;
19+
20+
if(typeofcallback!=="function"){
21+
thrownewTypeError("__with__ expects a callback function");
22+
}
23+
24+
undo=module.exports.__set__.apply(null,args);
25+
26+
try{
27+
callback();
28+
}finally{
29+
undo();
30+
}
31+
};
32+
}
33+
34+
module.exports=__with__;

‎lib/rewire.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
varModule=require("module"),
22
fs=require("fs"),
33
__get__=require("./__get__.js"),
4-
setModule=require("./__set__.js"),
5-
__set__=setModule["__set__"],
6-
__with__=setModule["__with__"],
4+
__set__=require("./__set__.js"),
5+
__with__=require("./__with__.js"),
76
getImportGlobalsSrc=require("./getImportGlobalsSrc.js"),
87
detectStrictMode=require("./detectStrictMode.js"),
98
moduleEnv=require("./moduleEnv.js");

‎test/__set__.test.js

Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
varexpect=require("expect.js"),
2-
setModule=require("../lib/__set__.js")
3-
__set__=setModule["__set__"],
4-
__with__=setModule["__with__"],
2+
__set__=require("../lib/__set__.js"),
53
vm=require("vm"),
64

75
expectReferenceError=expectError(ReferenceError),
@@ -18,12 +16,15 @@ describe("__set__", function () {
1816

1917
beforeEach(function(){
2018
moduleFake={
19+
module:{
20+
exports:{}
21+
},
2122
myValue:0,// copy by value
2223
myReference:{}// copy by reference
2324
};
2425

2526
vm.runInNewContext(
26-
"__set__ = "+__set__.toString()+"; "+
27+
"__set__ =module.exports.__set__ ="+__set__.toString()+"; "+
2728
"getValue = function () { return myValue; }; "+
2829
"getReference = function () { return myReference; }; ",
2930
moduleFake
@@ -72,10 +73,10 @@ describe("__set__", function () {
7273
expect(moduleFake.getReference()).to.be(newObj);
7374
});
7475
it("should return a function that when invoked reverts to the values before set was called",function(){
75-
undo=moduleFake.__set__("myValue",4)
76+
undo=moduleFake.__set__("myValue",4);
7677
expect(typeofundo).to.be("function");
7778
expect(moduleFake.getValue()).to.be(4);
78-
undo()
79+
undo();
7980
expect(moduleFake.getValue()).to.be(0);
8081
});
8182
it("should be able to revert when calling with an env-obj",function(){
@@ -126,78 +127,4 @@ describe("__set__", function () {
126127
moduleFake.__set__("someVar");// misfitting number of params
127128
}).to.throwException(expectTypeError);
128129
});
129-
});
130-
131-
describe("__with__",function(){
132-
varmoduleFake;
133-
134-
beforeEach(function(){
135-
moduleFake={
136-
myValue:0,// copy by value
137-
myReference:{}// copy by reference
138-
};
139-
140-
//__with__ requires __set__ to be in scope
141-
vm.runInNewContext(
142-
"__set__ = "+__set__.toString()+"; "+
143-
"__with__ = "+__with__.toString()+"; "+
144-
"getValue = function () { return myValue; }; "+
145-
"getReference = function () { return myReference; }; ",
146-
moduleFake
147-
);
148-
});
149-
150-
it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end",function(){
151-
varnewObj={hello:"hello"};
152-
153-
expect(moduleFake.getValue()).to.be(0);
154-
expect(moduleFake.getReference()).to.eql({});
155-
156-
moduleFake.__with__({
157-
myValue:2,
158-
myReference:newObj
159-
})(function(){
160-
//changes will be visible from within this callback function
161-
expect(moduleFake.getValue()).to.be(2);
162-
expect(moduleFake.getReference()).to.be(newObj);
163-
})
164-
165-
//undo will automatically get called for you after returning from your callback function
166-
expect(moduleFake.getValue()).to.be(0);
167-
expect(moduleFake.getReference()).to.eql({});
168-
});
169-
170-
it("should still revert values if the callback throws an exception",function(){
171-
varnewObj={hello:"hello"};
172-
functionwithError(){
173-
moduleFake.__with__({
174-
myValue:2,
175-
myReference:newObj
176-
})(function(){
177-
thrownewError("something went wrong...");
178-
})
179-
}
180-
expect(withError).to.throwError();
181-
expect(moduleFake.getValue()).to.be(0);
182-
expect(moduleFake.getReference()).to.eql({});
183-
});
184-
185-
it("should throw an error if something other than a function is passed as the callback",function(){
186-
varnewObj={hello:"hello"},
187-
withFunction=moduleFake.__with__({
188-
myValue:2,
189-
myReference:newObj
190-
})
191-
callWithFunction=function(){
192-
varargs=arguments;
193-
returnfunction(){
194-
withFunction.apply(null,args);
195-
};
196-
};
197-
198-
expect(callWithFunction(1)).to.throwError();
199-
expect(callWithFunction("a string")).to.throwError();
200-
expect(callWithFunction({})).to.throwError();
201-
expect(callWithFunction(function(){})).to.not.throwError();
202-
});
203-
});
130+
});

‎test/__with__.test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
varexpect=require("expect.js"),
2+
__with__=require("../lib/__with__.js"),
3+
__set__=require("../lib/__set__.js"),
4+
vm=require("vm"),
5+
expectReferenceError=expectError(ReferenceError),
6+
expectTypeError=expectError(TypeError);
7+
8+
functionexpectError(ErrConstructor){
9+
returnfunctionexpectReferenceError(err){
10+
expect(err.constructor.name).to.be(ErrConstructor.name);
11+
};
12+
}
13+
14+
describe("__with__",function(){
15+
varmoduleFake;
16+
17+
beforeEach(function(){
18+
moduleFake={
19+
module:{
20+
exports:{}
21+
},
22+
myValue:0,// copy by value
23+
myReference:{}// copy by reference
24+
};
25+
26+
//__with__ requires __set__ to be in scope
27+
vm.runInNewContext(
28+
"module.exports.__set__ = "+__set__.toString()+"; "+
29+
"__with__ = "+__with__.toString()+"; "+
30+
"getValue = function () { return myValue; }; "+
31+
"getReference = function () { return myReference; }; ",
32+
moduleFake
33+
);
34+
});
35+
36+
it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end",function(){
37+
varnewObj={hello:"hello"};
38+
39+
expect(moduleFake.getValue()).to.be(0);
40+
expect(moduleFake.getReference()).to.eql({});
41+
42+
moduleFake.__with__({
43+
myValue:2,
44+
myReference:newObj
45+
})(function(){
46+
//changes will be visible from within this callback function
47+
expect(moduleFake.getValue()).to.be(2);
48+
expect(moduleFake.getReference()).to.be(newObj);
49+
});
50+
51+
//undo will automatically get called for you after returning from your callback function
52+
expect(moduleFake.getValue()).to.be(0);
53+
expect(moduleFake.getReference()).to.eql({});
54+
});
55+
56+
it("should still revert values if the callback throws an exception",function(){
57+
varnewObj={hello:"hello"};
58+
functionwithError(){
59+
moduleFake.__with__({
60+
myValue:2,
61+
myReference:newObj
62+
})(function(){
63+
thrownewError("something went wrong...");
64+
});
65+
}
66+
expect(withError).to.throwError();
67+
expect(moduleFake.getValue()).to.be(0);
68+
expect(moduleFake.getReference()).to.eql({});
69+
});
70+
71+
it("should throw an error if something other than a function is passed as the callback",function(){
72+
varnewObj={hello:"hello"},
73+
withFunction=moduleFake.__with__({
74+
myValue:2,
75+
myReference:newObj
76+
});
77+
callWithFunction=function(){
78+
varargs=arguments;
79+
returnfunction(){
80+
withFunction.apply(null,args);
81+
};
82+
};
83+
84+
expect(callWithFunction(1)).to.throwError();
85+
expect(callWithFunction("a string")).to.throwError();
86+
expect(callWithFunction({})).to.throwError();
87+
expect(callWithFunction(function(){})).to.not.throwError();
88+
});
89+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp