Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
y1j2x34 committed Nov 8, 2017
1 parent 7f54cd1 commit 9f194aa
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 33 deletions.
8 changes: 6 additions & 2 deletions Class.js
Expand Up @@ -154,8 +154,12 @@
function $super(first) {
var self = this;
var args = arguments;
if (isPythonicOn && isArgument(first)) {
args = slice(first, 1);
if (isArgument(first)) {
if(isPythonicOn) {
args = slice(first, 1);
} else {
args = first;
}
}
if (_isAssignable(clazz, Array)) {
self.push.apply(self, args);
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -28,7 +28,7 @@ module.exports = function(config) {
files: [
'./node_modules/phantomjs-polyfill-object-assign/object-assign-polyfill.js',
{
pattern: 'test/*.js',
pattern: 'test/main.js',
watched: true,
included: true,
served: true
Expand Down
17 changes: 17 additions & 0 deletions test/inheritance.js
Expand Up @@ -9,6 +9,15 @@ describe('test inheritance', () => {
expect(() => {
Class.extend(SuperClass, {});
}).not.toThrow();
expect(() => {
Class.extend(SuperClass);
}).not.toThrow();
expect(() => {
Class.extend({});
}).not.toThrow();
expect(() => {
SuperClass.extend({});
}).not.toThrow();
});
it('test call parent constructor', () => {
var beCalled = false;
Expand Down Expand Up @@ -54,11 +63,19 @@ describe('test inheritance', () => {
const SubClass = Class.extend(SuperClass, {
method: function(self) {
self.$callSuper('method');
},
callNotExitMethod: function(self){
self.$callSuper('xxxadsdsad');
}
});
expect(beCalled).toBeFalsy();
new SubClass().method();
expect(beCalled).toBeTruthy();

expect(function(){
new SubClass().callNotExitMethod();
}).toThrow();

});
it('test constructor call sequence', () => {
let flag = 0;
Expand Down
4 changes: 4 additions & 0 deletions test/main.js
@@ -0,0 +1,4 @@
require('./create');
require('./inheritance');
require('./mixin');
require('./proxy');
134 changes: 104 additions & 30 deletions test/mixin.js
@@ -1,46 +1,47 @@
const Class = require('../Class.js');

describe('test mixin', () => {
var Consts = {
ANIMAL_BITE: 'animal bite',
MAMMAL_BITE: 'mammal bite',
FLYABLE_FLYING: 'flyable flying',
FLYABLE_LANDING: 'flyable landing',
ANIMAL: 'ANIMAL',
MAMMAL: 'MAMMAL',
FLYABLE: 'FLYABLE',
SONAR: 'SONAR',
var Consts = {
ANIMAL_BITE: 'animal bite',
MAMMAL_BITE: 'mammal bite',
FLYABLE_FLYING: 'flyable flying',
FLYABLE_LANDING: 'flyable landing',
ANIMAL: 'ANIMAL',
MAMMAL: 'MAMMAL',
FLYABLE: 'FLYABLE',
SONAR: 'SONAR',
MIX_CLASS: 'MIX_CLASS',

FLYABLE_DESTROYED: 'flyable destroyed',
SONAR_DESTROYED: 'sonar destroyed',
SONAR_LOCATE: 'sonar locate',
ANIMAL_STATIC_VALUE: 'animal static value',
FLYABLE_STATIC_VALUE: 'flyable static value',
SONAR_STATIC_VALUE: 'sonar static value'
};
FLYABLE_DESTROYED: 'flyable destroyed',
SONAR_DESTROYED: 'sonar destroyed',
SONAR_LOCATE: 'sonar locate',
ANIMAL_STATIC_VALUE: 'animal static value',
FLYABLE_STATIC_VALUE: 'flyable static value',
SONAR_STATIC_VALUE: 'sonar static value'
};
describe('test mixin(pythonic)', () => {
var Animal, Mammal, Flyable, Sonar;

beforeAll(() => {
Animal = Class.create({
statics: {
STATIC_VALUE: Consts.ANIMAL_STATIC_VALUE
},
name: Consts.ANIMAL,
init: function(self, arr){
init: function(self, arr) {
self.$super(arguments);
arr.push(Consts.ANIMAL);
},
bite: function(){
bite: function() {
return Consts.ANIMAL_BITE;
}
});
Mammal = Class.extend(Animal, {
name: Consts.MAMMAL,
init: function(self, arr){
init: function(self, arr) {
self.$super(arguments);
arr.push(Consts.MAMMAL);
},
bite: function(){
bite: function() {
return Consts.MAMMAL_BITE;
}
});
Expand All @@ -53,28 +54,28 @@ describe('test mixin', () => {
self.$super(arguments);
arr.push(Consts.FLYABLE);
},
fly: function(){
fly: function() {
return Consts.FLYABLE_FLYING;
},
landing: function(){
landing: function() {
return Consts.FLYABLE_LANDING;
},
destroy: function(){
destroy: function() {
return Consts.FLYABLE_DESTROYED;
}
});
Sonar = Class.create({
statics: {
STATIC_VALUE: Consts.SONAR_STATIC_VALUE
},
init: function(self, arr){
init: function(self, arr) {
self.$super(arguments);
arr.push(Consts.SONAR);
},
locate: function(){
locate: function() {
return Consts.SONAR_LOCATE;
},
destroy: function(){
destroy: function() {
return Consts.SONAR_DESTROYED;
}
});
Expand Down Expand Up @@ -102,10 +103,83 @@ describe('test mixin', () => {
expect(bat.locate()).toBe(Consts.SONAR_LOCATE);
expect(bat.destroy()).toBe(Consts.SONAR_DESTROYED);
});

it('test mixin static members', () => {
var Bat = Class.mix(Mammal).with(Flyable, Sonar);
expect(Bat.STATIC_VALUE).toBe(Consts.SONAR_STATIC_VALUE);
});
});

describe('test mixin(normal)', () => {
var Animal, Mammal, Flyable, Sonar;

});
beforeAll(() => {
Animal = Class.create({
statics: {
STATIC_VALUE: Consts.ANIMAL_STATIC_VALUE
},
name: Consts.ANIMAL,
pythonic: false,
init: function(arr) {
this.$super(arguments);
arr.push(Consts.ANIMAL);
},
bite: function() {
return Consts.ANIMAL_BITE;
}
});
Mammal = Class.extend(Animal, {
name: Consts.MAMMAL,
pythonic: false,
init: function(arr) {
this.$super(arguments);
arr.push(Consts.MAMMAL);
},
bite: function() {
return Consts.MAMMAL_BITE;
}
});
Flyable = Class.create({
statics: {
STATIC_VALUE: Consts.FLYABLE_STATIC_VALUE
},
name: Consts.FLYABLE,
pythonic: false,
init: function(arr) {
this.$super(arguments);
arr.push(Consts.FLYABLE);
},
fly: function() {
return Consts.FLYABLE_FLYING;
},
landing: function() {
return Consts.FLYABLE_LANDING;
},
destroy: function() {
return Consts.FLYABLE_DESTROYED;
}
});
Sonar = Class.create({
statics: {
STATIC_VALUE: Consts.SONAR_STATIC_VALUE
},
pythonic: false,
init: function(arr) {
this.$super(arguments);
arr.push(Consts.SONAR);
},
locate: function() {
return Consts.SONAR_LOCATE;
},
destroy: function() {
return Consts.SONAR_DESTROYED;
}
});
});
it('test mixin constructor call sequence', () => {
var Bat = Class.mix(Mammal).with(Flyable, Sonar);
var sequence = [];
new Bat(sequence);
expect(sequence).toEqual([Consts.ANIMAL, Consts.MAMMAL, Consts.FLYABLE, Consts.SONAR]);
});
});

0 comments on commit 9f194aa

Please sign in to comment.