2011-05-19 07:54:51 -04:00
|
|
|
/**
|
|
|
|
* Tests const keyword
|
|
|
|
*
|
2014-04-09 18:59:22 -04:00
|
|
|
* Copyright (C) 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
|
2011-05-19 07:54:51 -04:00
|
|
|
*
|
2013-12-22 09:37:21 -05:00
|
|
|
* This file is part of GNU ease.js.
|
2011-05-19 07:54:51 -04:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* ease.js is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2011-05-19 07:54:51 -04:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2011-05-19 07:54:51 -04:00
|
|
|
*
|
2014-01-15 23:56:00 -05:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-05-19 07:54:51 -04:00
|
|
|
*/
|
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
require( 'common' ).testCase(
|
2011-05-19 19:07:31 -04:00
|
|
|
{
|
2014-01-07 04:06:41 -05:00
|
|
|
caseSetUp: function()
|
|
|
|
{
|
|
|
|
// XXX: get rid of this disgusting mess; we're mid-refactor and all
|
|
|
|
// these dependencies should not be necessary for testing
|
|
|
|
this.Sut = this.require( 'ClassBuilder' );
|
|
|
|
this.MethodWrapperFactory = this.require( 'MethodWrapperFactory' );
|
|
|
|
|
|
|
|
this.wrappers = this.require( 'MethodWrappers' ).standard;
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
setUp: function()
|
2011-05-19 19:48:47 -04:00
|
|
|
{
|
2014-01-07 04:06:41 -05:00
|
|
|
this.builder = this.Sut(
|
2011-11-02 19:11:12 -04:00
|
|
|
this.require( '/MemberBuilder' )(
|
2014-01-07 04:06:41 -05:00
|
|
|
this.MethodWrapperFactory( this.wrappers.wrapNew ),
|
|
|
|
this.MethodWrapperFactory( this.wrappers.wrapOverride ),
|
|
|
|
this.MethodWrapperFactory( this.wrappers.wrapProxy ),
|
2011-11-02 23:28:23 -04:00
|
|
|
this.getMock( 'MemberBuilderValidator' )
|
2011-11-02 19:11:12 -04:00
|
|
|
),
|
2014-01-07 04:06:41 -05:00
|
|
|
this.require( '/VisibilityObjectFactoryFactory' )
|
|
|
|
.fromEnvironment()
|
2011-11-02 19:11:12 -04:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2011-11-02 23:28:23 -04:00
|
|
|
/** 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.
|
2011-11-02 19:11:12 -04:00
|
|
|
*/
|
|
|
|
'const keyword declares properties as static': function()
|
2011-05-19 19:48:47 -04:00
|
|
|
{
|
2011-11-02 19:11:12 -04:00
|
|
|
var val = 'baz',
|
|
|
|
Foo = this.builder.build(
|
|
|
|
{
|
|
|
|
'const foo': val,
|
|
|
|
} )
|
|
|
|
;
|
|
|
|
|
|
|
|
this.assertEqual( val, Foo.$('foo'),
|
|
|
|
"Const keyword should declare properties as static"
|
2011-05-19 19:48:47 -04:00
|
|
|
);
|
2011-11-02 19:11:12 -04:00
|
|
|
},
|
2011-05-19 19:48:47 -04:00
|
|
|
|
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
/**
|
|
|
|
* As the name implies, constant properties should not be writable.
|
|
|
|
*/
|
|
|
|
'const keyword creates immutable property': function()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// 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"
|
|
|
|
);
|
2011-05-19 19:48:47 -04:00
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2011-05-30 10:55:40 -04:00
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
this.fail( "Constant properties should not be writable" );
|
|
|
|
},
|
2011-05-30 10:55:40 -04:00
|
|
|
|
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
/**
|
|
|
|
* Unlike other languages such as PHP, the const keyword can have different
|
|
|
|
* levels of visibility.
|
|
|
|
*/
|
|
|
|
'Access modifiers are permitted with const keyword': function()
|
|
|
|
{
|
|
|
|
var protval = 'bar',
|
|
|
|
privval = 'baz',
|
2011-05-30 10:55:40 -04:00
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
Foo = this.builder.build(
|
2011-05-30 10:55:40 -04:00
|
|
|
{
|
2011-11-02 19:11:12 -04:00
|
|
|
'protected const prot': protval,
|
|
|
|
'private const priv': privval,
|
|
|
|
|
|
|
|
'public static getProt': function()
|
|
|
|
{
|
|
|
|
return this.$('prot');
|
|
|
|
},
|
|
|
|
|
|
|
|
'public static getPriv': function()
|
|
|
|
{
|
|
|
|
return this.$('priv');
|
|
|
|
},
|
|
|
|
} ),
|
|
|
|
|
2011-11-02 23:28:23 -04:00
|
|
|
// be sure to override each method to ensure we're checking
|
|
|
|
// references on the subtype, *not* the parent type
|
2011-11-02 19:11:12 -04:00
|
|
|
SubFoo = this.builder.build( Foo,
|
2011-05-30 10:55:40 -04:00
|
|
|
{
|
2011-11-02 19:11:12 -04:00
|
|
|
'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"
|
|
|
|
);
|
2011-05-30 10:55:40 -04:00
|
|
|
|
2011-11-02 19:11:12 -04:00
|
|
|
this.assertEqual( Foo.$('priv'), undefined,
|
|
|
|
"Private constants are not available publicly"
|
|
|
|
);
|
|
|
|
|
|
|
|
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,
|
|
|
|
"Protected constants are available to subtypes internally"
|
|
|
|
);
|
|
|
|
|
|
|
|
this.assertEqual( SubFoo.getPriv(), undefined,
|
|
|
|
"Private constants are NOT available to subtypes internally"
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} );
|
2011-05-30 10:55:40 -04:00
|
|
|
|