diff --git a/tests/core.js b/tests/core.js index 8c25f22..3c140fa 100644 --- a/tests/core.js +++ b/tests/core.js @@ -79,6 +79,21 @@ test('basic classical inheritence works', function() { 'base HouseCat is not affected by subclass mutations'); }); +test('inheritance of getters and setters works', function() { + cat = Cat(); + parasite = Parasite(); + lion = Lion(); + + equal(cat.furryness, 20, + 'getter was inherted from base') + + equal(parasite.furryness, 0, + 'getters can override base') + + equal(lion.furryness, 40, + 'getters can use $super to get base getter') +}); + test('instanceof works', function() { var lion = Lion(); ok(lion instanceof Lion, diff --git a/tests/data.js b/tests/data.js index 5b17e20..94040aa 100644 --- a/tests/data.js +++ b/tests/data.js @@ -7,6 +7,7 @@ var Animal = Class.$extend({ // required defaults this.name = (typeof(options.name) == 'string' ? options.name : 'Animal'); this.health = (typeof(options.health) == 'integer' ? options.health : 100); + this._furryness = (typeof(options.furryness) == 'integer' ? options.furryness : 20); }, die: function() { @@ -19,6 +20,14 @@ var Animal = Class.$extend({ dead: function() { return (this.health <= 0); + }, + + get furryness() { + return this._furryness; + }, + + set furryness(val) { + this._furryness = val; } }); @@ -26,6 +35,9 @@ var Parasite = Animal.$extend({ eat: function(animal) { this.$super(animal); animal.health -= 5; + }, + get furryness() { + return 0; } }); @@ -43,5 +55,8 @@ var HouseCat = Cat.$extend({ var Lion = Cat.$extend({ 'cute': false, - 'scary': true + 'scary': true, + get furryness() { + return this.$super() * 2; + } });