From ca709ede5ac0d633f5fec7a6e1e2c40a4db3c48e Mon Sep 17 00:00:00 2001 From: Bob Pace Date: Mon, 7 Jul 2014 11:48:47 -0600 Subject: added __with__ function according to proposed syntax in issue #29 --- test/__set__.test.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'test/__set__.test.js') diff --git a/test/__set__.test.js b/test/__set__.test.js index ff1a3d2..fe6f529 100644 --- a/test/__set__.test.js +++ b/test/__set__.test.js @@ -1,5 +1,7 @@ var expect = require("expect.js"), - __set__ = require("../lib/__set__.js"), + setModule = require("../lib/__set__.js") + __set__ = setModule["__set__"], + __with__ = setModule["__with__"], vm = require("vm"), expectReferenceError = expectError(ReferenceError), @@ -125,3 +127,77 @@ describe("__set__", function () { }).to.throwException(expectTypeError); }); }); + +describe("__with__", function() { + var moduleFake; + + beforeEach(function () { + moduleFake = { + myValue: 0, // copy by value + myReference: {} // copy by reference + }; + + //__with__ requires __set__ to be in scope + vm.runInNewContext( + "__set__ = " + __set__.toString() + "; " + + "__with__ = " + __with__.toString() + "; " + + "getValue = function () { return myValue; }; " + + "getReference = function () { return myReference; }; ", + moduleFake + ); + }); + + 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 () { + var newObj = { hello: "hello" }; + + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + + moduleFake.__with__({ + myValue: 2, + myReference: newObj + })(function() { + //changes will be visible from within this callback function + expect(moduleFake.getValue()).to.be(2); + expect(moduleFake.getReference()).to.be(newObj); + }) + + //undo will automatically get called for you after returning from your callback function + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + }); + + it("should still revert values if the callback throws an exception", function(){ + var newObj = { hello: "hello" }; + function withError(){ + moduleFake.__with__({ + myValue: 2, + myReference: newObj + })(function() { + throw new Error("something went wrong..."); + }) + } + expect(withError).to.throwError(); + expect(moduleFake.getValue()).to.be(0); + expect(moduleFake.getReference()).to.eql({}); + }); + + it("should throw an error if something other than a function is passed as the callback", function() { + var newObj = { hello: "hello" }, + withFunction = moduleFake.__with__({ + myValue: 2, + myReference: newObj + }) + callWithFunction = function(){ + var args = arguments; + return function() { + withFunction.apply(null, args); + }; + }; + + expect(callWithFunction(1)).to.throwError(); + expect(callWithFunction("a string")).to.throwError(); + expect(callWithFunction({})).to.throwError(); + expect(callWithFunction(function(){})).to.not.throwError(); + }); +}); -- cgit v1.2.3