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

add failing test case. #2

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "presets": ["es2015"] }
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ test: prepublish test/test.js
node test/test.js

test/test.js: test/index.js
babel test/index.js > test/test.js
$$(npm bin)/babel test/index.js > test/test.js

prepublish: to-factory.js

to-factory.js: index.js
babel index.js > to-factory.js
$$(npm bin)/babel index.js > to-factory.js

clean:
rm to-factory.js test/test.js
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"author": "Tim Oxley <secoif@gmail.com>",
"license": "MIT",
"devDependencies": {
"babel": "^5.1.13",
"babel": "^6.5.1",
"babel-cli": "^6.5.1",
"babel-preset-es2015": "^6.5.0",
"tape": "^4.0.0"
},
"directories": {
Expand Down
43 changes: 42 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ test('static properties', t => {
t.end()
})

test('inheritance', t => {
test('direct inheritance', t => {
const expected = Symbol()
class A {
constructor(name) {
Expand Down Expand Up @@ -82,3 +82,44 @@ test('inheritance', t => {
t.equal(withoutNew.test2(), expected, 'withoutNew inherited proto method call')
t.end()
})

test('factory inheritance', t => {
const expected = Symbol()
class A {
constructor(name) {
this.name = name
}
test() {
return expected
}
}
A.property = expected

const B = f(A)

class C extends B {
constructor(name) {
super(name.toUpperCase())
}
test2() {
return super.test()
}
}

const D = f(C)

const withNew = new D('withNew')
const withoutNew = D('withoutNew')

t.equal(withNew.constructor, C, 'withNew has the correct constructor')
t.equal(withoutNew.constructor, C, 'withoutNew has the correct constructor')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's debatable whether or not these should have constructor == C or constructor == D.


t.equal(withNew.name, 'WITHNEW', 'withNew has instance property')
t.equal(withoutNew.name, 'WITHOUTNEW', 'withoutNew has instance property')

t.equal(withNew.test(), expected, 'withNew inherited proto method call from A')
t.equal(withoutNew.test(), expected, 'withoutNew inherited proto method call from A')
t.equal(withNew.test2(), expected, 'withNew inherited proto method call from C')
t.equal(withoutNew.test2(), expected, 'withoutNew inherited proto method call from C')
t.end()
});
113 changes: 88 additions & 25 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
'use strict';

var _interopRequireDefault = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };

var _get = function get(object, property, receiver) { var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _inherits = function (subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; };
var _tape = require('tape');

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _tape2 = _interopRequireDefault(_tape);

var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
var _ = require('../');

var _test = require('tape');
var _2 = _interopRequireDefault(_);

var _test2 = _interopRequireDefault(_test);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var _f = require('../');
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

var _f2 = _interopRequireDefault(_f);
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

_test2['default']('class instatiation', function (t) {
var A = _f2['default'](function A(name) {
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

(0, _tape2.default)('class instatiation', function (t) {
var A = (0, _2.default)(function A(name) {
_classCallCheck(this, A);

this.name = name;
Expand All @@ -36,9 +38,9 @@ _test2['default']('class instatiation', function (t) {
t.end();
});

_test2['default']('proto methods', function (t) {
(0, _tape2.default)('proto methods', function (t) {
var expected = Symbol();
var A = _f2['default']((function () {
var A = (0, _2.default)(function () {
function A() {
_classCallCheck(this, A);
}
Expand All @@ -51,7 +53,7 @@ _test2['default']('proto methods', function (t) {
}]);

return A;
})());
}());

var withNew = new A();
var withoutNew = A();
Expand All @@ -60,24 +62,24 @@ _test2['default']('proto methods', function (t) {
t.end();
});

_test2['default']('static properties', function (t) {
(0, _tape2.default)('static properties', function (t) {
var expected = Symbol();

var A = function A() {
_classCallCheck(this, A);
};

A.property = expected;
var B = _f2['default'](A);
var B = (0, _2.default)(A);

t.equal(B.property, expected, 'static property exists');
t.end();
});

_test2['default']('inheritance', function (t) {
(0, _tape2.default)('direct inheritance', function (t) {
var expected = Symbol();

var A = (function () {
var A = function () {
function A(name) {
_classCallCheck(this, A);

Expand All @@ -92,21 +94,21 @@ _test2['default']('inheritance', function (t) {
}]);

return A;
})();
}();

A.property = expected;

var B = _f2['default'](A);
var B = (0, _2.default)(A);

var C = (0, _2.default)(function (_A) {
_inherits(C, _A);

var C = _f2['default']((function (_A) {
function C(name) {
_classCallCheck(this, C);

_get(Object.getPrototypeOf(C.prototype), 'constructor', this).call(this, name.toUpperCase());
return _possibleConstructorReturn(this, Object.getPrototypeOf(C).call(this, name.toUpperCase()));
}

_inherits(C, _A);

_createClass(C, [{
key: 'test2',
value: function test2() {
Expand All @@ -115,7 +117,7 @@ _test2['default']('inheritance', function (t) {
}]);

return C;
})(A));
}(A));

t.equal(B.property, expected, 'static property exists');

Expand All @@ -132,3 +134,64 @@ _test2['default']('inheritance', function (t) {
t.end();
});

(0, _tape2.default)('factory inheritance', function (t) {
var expected = Symbol();

var A = function () {
function A(name) {
_classCallCheck(this, A);

this.name = name;
}

_createClass(A, [{
key: 'test',
value: function test() {
return expected;
}
}]);

return A;
}();

A.property = expected;

var B = (0, _2.default)(A);

var C = function (_B) {
_inherits(C, _B);

function C(name) {
_classCallCheck(this, C);

return _possibleConstructorReturn(this, Object.getPrototypeOf(C).call(this, name.toUpperCase()));
}

_createClass(C, [{
key: 'test2',
value: function test2() {
return _get(Object.getPrototypeOf(C.prototype), 'test', this).call(this);
}
}]);

return C;
}(B);

var D = (0, _2.default)(C);

var withNew = new D('withNew');
var withoutNew = D('withoutNew');

t.equal(withNew.constructor, C, 'withNew has the correct constructor');
t.equal(withoutNew.constructor, C, 'withoutNew has the correct constructor');

t.equal(withNew.name, 'WITHNEW', 'withNew has instance property');
t.equal(withoutNew.name, 'WITHOUTNEW', 'withoutNew has instance property');

t.equal(withNew.test(), expected, 'withNew inherited proto method call from A');
t.equal(withoutNew.test(), expected, 'withoutNew inherited proto method call from A');
t.equal(withNew.test2(), expected, 'withNew inherited proto method call from C');
t.equal(withoutNew.test2(), expected, 'withoutNew inherited proto method call from C');
t.end();
});

3 changes: 1 addition & 2 deletions to-factory.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"use strict";

var _bind = Function.prototype.bind;
function toFactory(Class) {
var Factory = function Factory() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

return new (_bind.apply(Class, [null].concat(args)))();
return new (Function.prototype.bind.apply(Class, [null].concat(args)))();
};
Factory.__proto__ = Class;
Factory.prototype = Class.prototype;
Expand Down