11var expect = require ( "expect.js" ) ,
22__with__ = require ( "../lib/__with__.js" ) ,
33__set__ = require ( "../lib/__set__.js" ) ,
4- vm = require ( "vm" ) ;
4+ vm = require ( "vm" ) ,
5+
6+ expectTypeError = expectError ( TypeError ) ;
57
68function expectError ( ErrConstructor ) {
79return function expectReferenceError ( err ) {
@@ -41,7 +43,7 @@ describe("__with__", function() {
4143} ) ) . to . be . a ( "function" ) ;
4244} ) ;
4345
44- 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 ( ) {
46+ 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 ( ) {
4547expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
4648expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
4749
@@ -59,6 +61,24 @@ describe("__with__", function() {
5961expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
6062} ) ;
6163
64+ it ( "should also accept a variable name and a variable value (just like __set__)" , function ( ) {
65+ expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
66+
67+ moduleFake . __with__ ( "myValue" , 2 ) ( function ( ) {
68+ expect ( moduleFake . getValue ( ) ) . to . be ( 2 ) ;
69+ } ) ;
70+
71+ expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
72+
73+ expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
74+
75+ moduleFake . __with__ ( "myReference" , newObj ) ( function ( ) {
76+ expect ( moduleFake . getReference ( ) ) . to . be ( newObj ) ;
77+ } ) ;
78+
79+ expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
80+ } ) ;
81+
6282it ( "should still revert values if the callback throws an exception" , function ( ) {
6383expect ( function withError ( ) {
6484moduleFake . __with__ ( {
@@ -86,9 +106,9 @@ describe("__with__", function() {
86106} ;
87107}
88108
89- expect ( callWithFunction ( 1 ) ) . to . throwError ( ) ;
90- expect ( callWithFunction ( "a string" ) ) . to . throwError ( ) ;
91- expect ( callWithFunction ( { } ) ) . to . throwError ( ) ;
92- expect ( callWithFunction ( function ( ) { } ) ) . to . not . throwError ( ) ;
109+ expect ( callWithFunction ( 1 ) ) . to . throwError ( expectTypeError ) ;
110+ expect ( callWithFunction ( "a string" ) ) . to . throwError ( expectTypeError ) ;
111+ expect ( callWithFunction ( { } ) ) . to . throwError ( expectTypeError ) ;
112+ expect ( callWithFunction ( function ( ) { } ) ) . to . not . throwError ( expectTypeError ) ;
93113} ) ;
94114} ) ;