From d044f04e4c6a5e9bff37c384a2256d771aa0e5bc Mon Sep 17 00:00:00 2001 From: Johannes Ewald Date: Tue, 8 Jul 2014 01:37:32 +0200 Subject: Improve __with__ tests --- test/__with__.test.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'test/__with__.test.js') diff --git a/test/__with__.test.js b/test/__with__.test.js index 7ce86b5..532ef91 100644 --- a/test/__with__.test.js +++ b/test/__with__.test.js @@ -1,7 +1,9 @@ var expect = require("expect.js"), __with__ = require("../lib/__with__.js"), __set__ = require("../lib/__set__.js"), - vm = require("vm"); + vm = require("vm"), + + expectTypeError = expectError(TypeError); function expectError(ErrConstructor) { return function expectReferenceError(err) { @@ -41,7 +43,7 @@ describe("__with__", function() { })).to.be.a("function"); }); - 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 () { + it("should return a function that can be invoked with a callback which guarantees __set__'s undo function is called for you at the end", function () { expect(moduleFake.getValue()).to.be(0); expect(moduleFake.getReference()).to.eql({}); @@ -59,6 +61,24 @@ describe("__with__", function() { expect(moduleFake.getReference()).to.eql({}); }); + it("should also accept a variable name and a variable value (just like __set__)", function () { + expect(moduleFake.getValue()).to.be(0); + + moduleFake.__with__("myValue", 2)(function () { + expect(moduleFake.getValue()).to.be(2); + }); + + expect(moduleFake.getValue()).to.be(0); + + expect(moduleFake.getReference()).to.eql({}); + + moduleFake.__with__("myReference", newObj)(function () { + expect(moduleFake.getReference()).to.be(newObj); + }); + + expect(moduleFake.getReference()).to.eql({}); + }); + it("should still revert values if the callback throws an exception", function(){ expect(function withError() { moduleFake.__with__({ @@ -86,9 +106,9 @@ describe("__with__", function() { }; } - expect(callWithFunction(1)).to.throwError(); - expect(callWithFunction("a string")).to.throwError(); - expect(callWithFunction({})).to.throwError(); - expect(callWithFunction(function(){})).to.not.throwError(); + expect(callWithFunction(1)).to.throwError(expectTypeError); + expect(callWithFunction("a string")).to.throwError(expectTypeError); + expect(callWithFunction({})).to.throwError(expectTypeError); + expect(callWithFunction(function(){})).to.not.throwError(expectTypeError); }); }); -- cgit v1.2.3