1
0
Fork 0

[#25] Began moving test-class_builder-visibility over to new test case format

closure/master
Mike Gerwitz 2011-11-02 21:37:28 -04:00
parent de78a472f0
commit 48dbfea990
1 changed files with 52 additions and 45 deletions

View File

@ -32,55 +32,62 @@ var common = require( './common' ),
// dependencies should not be necessary for testing
ClassBuilder = common.require( '/ClassBuilder' ),
MethodWrapperFactory = common.require( '/MethodWrapperFactory' ),
wrappers = common.require( '/MethodWrappers' ).standard,
builder = ClassBuilder(
common.require( '/MemberBuilder' )(
MethodWrapperFactory( wrappers.wrapNew ),
MethodWrapperFactory( wrappers.wrapOverride )
),
common.require( '/VisibilityObjectFactoryFactory' ).fromEnvironment()
)
wrappers = common.require( '/MethodWrappers' ).standard
;
/**
* As discussed in GH#15, there's a bit of an issue when passing around 'this'
* from within a method. For example, passing 'this' as an argument or invoking
* a method with it as the context will effectively defeat encapsulation.
* Unfortunately, there's really no way around that. Maybe a more elegant
* solution will arise in the future. For now, not likely.
*
* We need to provide a means to reference the actual instance. __inst is that
* solution.
*/
( function testSelfPropertyReferencesInstanceRatherThanPropObj()
require( 'common' ).testCase(
{
var result = null
ref = null,
foo = builder.build( {
'public __construct': function()
{
// rather than returning, assign to external var so that we can
// rest assured that the return value wasn't manipulated
result = this.__inst;
ref = this;
}
} )();
assert.deepEqual( result, foo,
"this.__inst returns reference to actual instance"
);
// the property should be read-only
if ( util.definePropertyFallback() === false )
setUp: function()
{
assert.equal(
Object.getOwnPropertyDescriptor( ref, '__inst' ).writable,
false,
"this.__inst is not writable"
this.builder = ClassBuilder(
this.require( '/MemberBuilder' )(
MethodWrapperFactory( wrappers.wrapNew ),
MethodWrapperFactory( wrappers.wrapOverride )
),
this.require( '/VisibilityObjectFactoryFactory' ).fromEnvironment()
);
}
} )();
},
/**
* As discussed in GH#15, there's a bit of an issue when passing around
* 'this' from within a method. For example, passing 'this' as an argument
* or invoking a method with it as the context will effectively defeat
* encapsulation. Unfortunately, there's really no way around that. Maybe a
* more elegant solution will arise in the future. For now, not likely.
*
* We need to provide a means to reference the actual instance. __inst is
* that solution.
*/
'Self property references instance rather than property object': function()
{
var result = null
ref = null,
foo = this.builder.build( {
'public __construct': function()
{
// rather than returning, assign to external var so that we can
// rest assured that the return value wasn't manipulated
result = this.__inst;
ref = this;
}
} )();
this.assertDeepEqual( result, foo,
"this.__inst returns reference to actual instance"
);
// the property should be read-only
if ( util.definePropertyFallback() === false )
{
assert.equal(
Object.getOwnPropertyDescriptor( ref, '__inst' ).writable,
false,
"this.__inst is not writable"
);
}
},
} );