1
1
var expect = require ( "expect.js" ) ,
2
2
__with__ = require ( "../lib/__with__.js" ) ,
3
3
__set__ = require ( "../lib/__set__.js" ) ,
4
- vm = require ( "vm" ) ;
4
+ vm = require ( "vm" ) ,
5
+
6
+ expectTypeError = expectError ( TypeError ) ;
5
7
6
8
function expectError ( ErrConstructor ) {
7
9
return function expectReferenceError ( err ) {
@@ -41,7 +43,7 @@ describe("__with__", function() {
41
43
} ) ) . to . be . a ( "function" ) ;
42
44
} ) ;
43
45
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 ( ) {
45
47
expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
46
48
expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
47
49
@@ -59,6 +61,24 @@ describe("__with__", function() {
59
61
expect ( moduleFake . getReference ( ) ) . to . eql ( { } ) ;
60
62
} ) ;
61
63
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
+
62
82
it ( "should still revert values if the callback throws an exception" , function ( ) {
63
83
expect ( function withError ( ) {
64
84
moduleFake . __with__ ( {
@@ -86,9 +106,9 @@ describe("__with__", function() {
86
106
} ;
87
107
}
88
108
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 ) ;
93
113
} ) ;
94
114
} ) ;