11var expect = require ( "expect.js" ) ,
2- setModule = require ( "../lib/__set__.js" )
3- __set__ = setModule [ "__set__" ] ,
4- __with__ = setModule [ "__with__" ] ,
2+ __set__ = require ( "../lib/__set__.js" ) ,
53 vm = require ( "vm" ) ,
64
75 expectReferenceError = expectError ( ReferenceError ) ,
@@ -18,12 +16,15 @@ describe("__set__", function () {
1816
1917 beforeEach ( function ( ) {
2018 moduleFake = {
19+ module : {
20+ exports : { }
21+ } ,
2122 myValue : 0 , // copy by value
2223 myReference : { } // copy by reference
2324 } ;
2425
2526 vm . runInNewContext (
26- "__set__ = " + __set__ . toString ( ) + "; " +
27+ "__set__ = module.exports.__set__ = " + __set__ . toString ( ) + "; " +
2728 "getValue = function () { return myValue; }; " +
2829 "getReference = function () { return myReference; }; " ,
2930 moduleFake
@@ -72,10 +73,10 @@ describe("__set__", function () {
7273 expect ( moduleFake . getReference ( ) ) . to . be ( newObj ) ;
7374 } ) ;
7475 it ( "should return a function that when invoked reverts to the values before set was called" , function ( ) {
75- undo = moduleFake . __set__ ( "myValue" , 4 )
76+ undo = moduleFake . __set__ ( "myValue" , 4 ) ;
7677 expect ( typeof undo ) . to . be ( "function" ) ;
7778 expect ( moduleFake . getValue ( ) ) . to . be ( 4 ) ;
78- undo ( )
79+ undo ( ) ;
7980 expect ( moduleFake . getValue ( ) ) . to . be ( 0 ) ;
8081 } ) ;
8182 it ( "should be able to revert when calling with an env-obj" , function ( ) {
@@ -126,78 +127,4 @@ describe("__set__", function () {
126127 moduleFake . __set__ ( "someVar" ) ; // misfitting number of params
127128 } ) . to . throwException ( expectTypeError ) ;
128129 } ) ;
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- } ) ;
130+ } ) ;
0 commit comments