1
0
Fork 0

[#25] Began converting test-class_builder-const to test case

- Not yet renaming, as the conversion is not yet complete
closure/master
Mike Gerwitz 2011-11-02 19:11:12 -04:00
parent a0ba2feb33
commit 110d937838
1 changed files with 112 additions and 107 deletions

View File

@ -23,130 +23,135 @@
*/ */
var common = require( './common' ), var common = require( './common' ),
assert = require( 'assert' ),
// XXX: get rid of this disgusting mess; we're mid-refactor and all these // XXX: get rid of this disgusting mess; we're mid-refactor and all these
// dependencies should not be necessary for testing // dependencies should not be necessary for testing
ClassBuilder = common.require( '/ClassBuilder' ), ClassBuilder = common.require( '/ClassBuilder' ),
MethodWrapperFactory = common.require( '/MethodWrapperFactory' ), MethodWrapperFactory = common.require( '/MethodWrapperFactory' ),
wrappers = common.require( '/MethodWrappers' ).standard, wrappers = common.require( '/MethodWrappers' ).standard
builder = ClassBuilder(
common.require( '/MemberBuilder' )(
MethodWrapperFactory( wrappers.wrapNew ),
MethodWrapperFactory( wrappers.wrapOverride )
),
common.require( '/VisibilityObjectFactoryFactory' ).fromEnvironment()
)
; ;
require( 'common' ).testCase(
/**
* The const keyword should result in a static property. The rationale for this
* is that, if a value is constant, then instances do not make sense.
*/
( function testConstKeywordDeclaresPropertiesAsStatic()
{ {
var val = 'baz', setUp: function()
Foo = builder.build( {
this.builder = ClassBuilder(
this.require( '/MemberBuilder' )(
MethodWrapperFactory( wrappers.wrapNew ),
MethodWrapperFactory( wrappers.wrapOverride )
),
this.require( '/VisibilityObjectFactoryFactory' ).fromEnvironment()
)
},
/**
* The const keyword should result in a static property. The rationale for this
* is that, if a value is constant, then instances do not make sense.
*/
'const keyword declares properties as static': function()
{
var val = 'baz',
Foo = this.builder.build(
{
'const foo': val,
} )
;
this.assertEqual( val, Foo.$('foo'),
"Const keyword should declare properties as static"
);
},
/**
* As the name implies, constant properties should not be writable.
*/
'const keyword creates immutable property': function()
{
try
{ {
'const foo': val, // this should fail (trying to alter const prop foo)
} ) this.builder.build( { 'const foo': 'bar' } ).$( 'foo', 'baz' );
; }
catch ( e )
{
this.assertOk(
e.message.search( 'foo' ) !== -1,
"Const modification error should contain name of property"
);
assert.equal( val, Foo.$('foo'), return;
"Const keyword should declare properties as static" }
);
} )(); this.fail( "Constant properties should not be writable" );
},
/** /**
* As the name implies, constant properties should not be writable. * Unlike other languages such as PHP, the const keyword can have different
*/ * levels of visibility.
( function testConstKeywordCreatesImmutableProperty() */
{ 'Access modifiers are permitted with const keyword': function()
try
{ {
// this should fail (trying to alter const prop foo) var protval = 'bar',
builder.build( { 'const foo': 'bar' } ).$( 'foo', 'baz' ); privval = 'baz',
}
catch ( e ) Foo = this.builder.build(
{ {
assert.ok( 'protected const prot': protval,
e.message.search( 'foo' ) !== -1, 'private const priv': privval,
"Const modification error should contain name of property"
'public static getProt': function()
{
return this.$('prot');
},
'public static getPriv': function()
{
return this.$('priv');
},
} ),
// be sure to override each method to ensure we're checking references
// on the subtype, *not* the parent type
SubFoo = this.builder.build( Foo,
{
'public static getProt': function()
{
return this.$('prot');
},
'public static getPriv': function()
{
return this.$('priv');
},
} )
;
this.assertEqual( Foo.$('prot'), undefined,
"Protected constants are not available publicly"
); );
return; this.assertEqual( Foo.$('priv'), undefined,
} "Private constants are not available publicly"
);
assert.fail( "Constant properties should not be writable" ); this.assertEqual( Foo.getProt(), protval,
} )(); "Protected constants are available internally"
);
this.assertEqual( Foo.getPriv(), privval,
"Private constants are available internally"
);
/** this.assertEqual( SubFoo.getProt(), protval,
* Unlike other languages such as PHP, the const keyword can have different "Protected constants are available to subtypes internally"
* levels of visibility. );
*/
( function testVisibilityModifiersArePermittedWithConstKeyword()
{
var protval = 'bar',
privval = 'baz',
Foo = builder.build( this.assertEqual( SubFoo.getPriv(), undefined,
{ "Private constants are NOT available to subtypes internally"
'protected const prot': protval, );
'private const priv': privval, },
} );
'public static getProt': function()
{
return this.$('prot');
},
'public static getPriv': function()
{
return this.$('priv');
},
} ),
// be sure to override each method to ensure we're checking references
// on the subtype, *not* the parent type
SubFoo = builder.build( Foo,
{
'public static getProt': function()
{
return this.$('prot');
},
'public static getPriv': function()
{
return this.$('priv');
},
} )
;
assert.equal( Foo.$('prot'), undefined,
"Protected constants are not available publicly"
);
assert.equal( Foo.$('priv'), undefined,
"Private constants are not available publicly"
);
assert.equal( Foo.getProt(), protval,
"Protected constants are available internally"
);
assert.equal( Foo.getPriv(), privval,
"Private constants are available internally"
);
assert.equal( SubFoo.getProt(), protval,
"Protected constants are available to subtypes internally"
);
assert.equal( SubFoo.getPriv(), undefined,
"Private constants are NOT available to subtypes internally"
);
} )();