From 9f194aa707e7635e9f75371c7e6aef5f744582dd Mon Sep 17 00:00:00 2001 From: y1j2x34 Date: Wed, 8 Nov 2017 21:25:33 +0800 Subject: [PATCH] bug fix --- Class.js | 8 ++- karma.conf.js | 2 +- test/inheritance.js | 17 ++++++ test/main.js | 4 ++ test/mixin.js | 134 ++++++++++++++++++++++++++++++++++---------- 5 files changed, 132 insertions(+), 33 deletions(-) create mode 100644 test/main.js diff --git a/Class.js b/Class.js index bd3c66a..3176aa1 100644 --- a/Class.js +++ b/Class.js @@ -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); diff --git a/karma.conf.js b/karma.conf.js index 517e4da..bf15d92 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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 diff --git a/test/inheritance.js b/test/inheritance.js index 24df373..764b55e 100644 --- a/test/inheritance.js +++ b/test/inheritance.js @@ -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; @@ -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; diff --git a/test/main.js b/test/main.js new file mode 100644 index 0000000..b99d988 --- /dev/null +++ b/test/main.js @@ -0,0 +1,4 @@ +require('./create'); +require('./inheritance'); +require('./mixin'); +require('./proxy'); \ No newline at end of file diff --git a/test/mixin.js b/test/mixin.js index af0bd90..8119460 100644 --- a/test/mixin.js +++ b/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; } }); @@ -53,13 +54,13 @@ 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; } }); @@ -67,14 +68,14 @@ describe('test mixin', () => { 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; } }); @@ -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; -}); \ No newline at end of file + 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]); + }); +});