Browse files

Some additional specs for Class.refactor + a small code improvement t…

…o find the original method
  • Loading branch information...
1 parent 96ac2b4 commit 82812811c1d0d1e56d890bf696fd50deea3bc505 @arian arian committed with anutron Mar 30, 2011
Showing with 26 additions and 4 deletions.
  1. +2 −2 Source/Class/Class.Refactor.js
  2. +24 −2 Specs/1.3/Class/Class.Refactor.js
View
4 Source/Class/Class.Refactor.js
@@ -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;
View
26 Specs/1.3/Class/Class.Refactor.js
@@ -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();
}
@@ -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.