|
1 | 1 | var expect = require("expect.js"),
|
2 |
| - __set__ = require("../lib/__set__.js"), |
| 2 | + setModule = require("../lib/__set__.js") |
| 3 | + __set__ = setModule["__set__"], |
| 4 | + __with__ = setModule["__with__"], |
3 | 5 | vm = require("vm"),
|
4 | 6 |
|
5 | 7 | expectReferenceError = expectError(ReferenceError),
|
@@ -125,3 +127,77 @@ describe("__set__", function () {
|
125 | 127 | }).to.throwException(expectTypeError);
|
126 | 128 | });
|
127 | 129 | });
|
| 130 | + |
| 131 | +describe("__with__", function() { |
| 132 | + var moduleFake; |
| 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 | + var newObj = { 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 | + var newObj = { hello: "hello" }; |
| 172 | + function withError(){ |
| 173 | + moduleFake.__with__({ |
| 174 | + myValue: 2, |
| 175 | + myReference: newObj |
| 176 | + })(function() { |
| 177 | + throw new Error("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 | + var newObj = { hello: "hello" }, |
| 187 | + withFunction = moduleFake.__with__({ |
| 188 | + myValue: 2, |
| 189 | + myReference: newObj |
| 190 | + }) |
| 191 | + callWithFunction = function(){ |
| 192 | + var args = arguments; |
| 193 | + return function() { |
| 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 | +}); |
0 commit comments