From 37d66b375d19f9863dd7ddc15ad21c2e57f200cf Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Fri, 17 Jan 2014 23:26:14 -0500 Subject: [PATCH] Moved test-class-gettersetter into suite as Class/GetterSetterTest --- test/Class/GetterSetterTest.js | 119 +++++++++++++++++++++++++++++++ test/test-class-gettersetter.js | 120 -------------------------------- 2 files changed, 119 insertions(+), 120 deletions(-) create mode 100644 test/Class/GetterSetterTest.js delete mode 100644 test/test-class-gettersetter.js diff --git a/test/Class/GetterSetterTest.js b/test/Class/GetterSetterTest.js new file mode 100644 index 0000000..73f6875 --- /dev/null +++ b/test/Class/GetterSetterTest.js @@ -0,0 +1,119 @@ +/** + * Tests class getter/setter inheritance + * + * Copyright (C) 2010, 2011, 2013 Mike Gerwitz + * + * This file is part of GNU ease.js. + * + * 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. + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +require( 'common' ).testCase( +{ + caseSetUp: function() + { + this.Sut = this.require( 'class' ); + this.util = this.require( 'util' ); + }, + + + setUp: function() + { + // don't perform these tests if getters/setters are unsupported + if ( this.util.definePropertyFallback() ) + { + this.skip(); + } + + var foo_def = {}, + sub_foo_def = {}; + + // to prevent syntax errors in environments that do not support + // getters/setters in object notation + Object.defineProperty( foo_def, 'foo', { + get: function () + { + return this._foo; + }, + set: function ( val ) + { + this._foo = ''+( val ); + }, + + enumerable: true, + } ); + + Object.defineProperty( foo_def, 'virtual bar', { + get: function () + { + return 'durp'; + }, + set: function ( val ) + { + }, + + enumerable: true, + } ); + + Object.defineProperty( sub_foo_def, 'override bar', { + get: function () + { + return this.bar2; + }, + set: function ( val ) + { + this.bar2 = val; + }, + + enumerable: true, + } ); + + // this is important since the system may freeze the object, so we + // must have declared it in advance + foo_def.bar2 = ''; + + var Foo = this.Sut.extend( foo_def ), + SubFoo = Foo.extend( sub_foo_def ); + + this.sub = new SubFoo(); + }, + + + /** + * Getters/setters should be inherited from the prototype as-is (if this + * doesn't work, someone went out of their way to break it, as it works + * by default!) + */ + 'Subtypes inherit getters/setters': function() + { + var val = 'foo'; + + this.sub.foo = val; + this.assertEqual( this.sub.foo, val ); + }, + + + /** + * Just as methods can be overridden, so should getters/setters, which + * act as methods do. + */ + 'Subtypes should be able to override getters/setters': function() + { + var val = 'bar'; + + this.sub.bar = val; + this.assertEqual( this.sub.bar, val ); + this.assertEqual( this.sub.bar2, val ); + }, +} ); diff --git a/test/test-class-gettersetter.js b/test/test-class-gettersetter.js deleted file mode 100644 index ca6cc9e..0000000 --- a/test/test-class-gettersetter.js +++ /dev/null @@ -1,120 +0,0 @@ -/** - * Tests class getter/setter inheritance - * - * Copyright (C) 2010, 2011, 2013 Mike Gerwitz - * - * This file is part of GNU ease.js. - * - * 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. - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -var common = require( './common' ), - assert = require( 'assert' ), - Class = common.require( 'class' ), - util = common.require( 'util' ), - - foo_def = {}, - sub_foo_def = {} -; - -// don't perform these tests if getters/setters are unsupported -if ( util.definePropertyFallback() ) -{ - return; -} - - -// to prevent syntax errors in browsers that do not support getters/setters in -// object notation -Object.defineProperty( foo_def, 'foo', { - get: function () - { - return this._foo; - }, - set: function ( val ) - { - this._foo = ''+( val ); - }, - - enumerable: true, -} ); - -Object.defineProperty( foo_def, 'virtual bar', { - get: function () - { - return 'durp'; - }, - set: function ( val ) - { - }, - - enumerable: true, -} ); - -Object.defineProperty( sub_foo_def, 'override bar', { - get: function () - { - return this.bar2; - }, - set: function ( val ) - { - this.bar2 = val; - }, - - enumerable: true, -} ); - -// this is important since the system may freeze the object, so we must have -// declared it in advance -foo_def.bar2 = ''; - - -var Foo = Class.extend( foo_def ), - SubFoo = Foo.extend( sub_foo_def ); - -var foo = new Foo(), - sub = new SubFoo(), - val = 'val'; - - -// ensure we have our act together before continuing -foo.foo = val; -assert.equal( - foo.foo, - val, - "Sanity check" -); - -// foo should be inherited as-is (if this doesn't work, someone went out of -// their way to break it, as it works by default!) -sub.foo = val = 'val2'; -assert.equal( - sub.foo, - val, - "Subtypes should inherit getter/setters" -); - -sub.bar = val = 'val3'; -assert.equal( - sub.bar, - val, - "Subtypes should be able to override getter/setters" -); - -assert.equal( - sub.bar2, - val, - "Subtypes should be able to override getter/setters (check 2)" -); -