Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Pull Request #5 to support getter and setter from Grepsy with 1.4 version #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion tests/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -19,13 +20,24 @@ var Animal = Class.$extend({

dead: function() {
return (this.health <= 0);
},

get furryness() {
return this._furryness;
},

set furryness(val) {
this._furryness = val;
}
});

var Parasite = Animal.$extend({
eat: function(animal) {
this.$super(animal);
animal.health -= 5;
},
get furryness() {
return 0;
}
});

Expand All @@ -43,5 +55,8 @@ var HouseCat = Cat.$extend({

var Lion = Cat.$extend({
'cute': false,
'scary': true
'scary': true,
get furryness() {
return this.$super() * 2;
}
});