Skip to content

Commit

Permalink
Some additional specs for Class.refactor + a small code improvement t…
Browse files Browse the repository at this point in the history
…o find the original method
  • Loading branch information
Arian authored and anutron committed Apr 1, 2011
1 parent 96ac2b4 commit 8281281
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Source/Class/Class.Refactor.js
Expand Up @@ -26,10 +26,10 @@ Class.refactor = function(original, refactors){

Object.each(refactors, function(item, name){
var origin = original.prototype[name];
if (origin && origin.$origin) origin = origin.$origin;
origin = (origin && origin.$origin) || origin || function(){};
original.implement(name, (typeof item == 'function') ? function(){
var old = this.previous;
this.previous = origin || function(){};
this.previous = origin;
var value = item.apply(this, arguments);
this.previous = old;
return value;
Expand Down
26 changes: 24 additions & 2 deletions Specs/1.3/Class/Class.Refactor.js
Expand Up @@ -20,9 +20,9 @@ describe('Class.Refactor', function(){
return 'altered';
}
});
Test.static_method = function(){ return 'static';};
Test.static_method = function(){return 'static';};
Class.refactor(Test, {
options: { foo: 'rab' },
options: {foo: 'rab'},
altered: function(){
return 'this is ' + this.previous();
}
Expand Down Expand Up @@ -78,4 +78,26 @@ describe('Class.Refactor', function(){
expect(new Test3().origin()).toEqual('refactored origin original origin');
});

var Test4 = new Class({
untouched: function(){
return 'untouched';
}
});
var RefactoredTest4 = Class.refactor(Test4, {
foo: function(){
return this.previous();
}
});

it('should return the class refactored class as well', function(){
expect(Test4).toEqual(RefactoredTest4);
});

it('should have a previous method for each refactored method', function(){
var test = new Test4();
expect(test.foo).not.toThrow();
expect(test.foo()).toBe(undefined);
expect(test.untouched()).toEqual('untouched');
});

});

0 comments on commit 8281281

Please sign in to comment.