Skip to content

Commit

Permalink
Add test coverage, travis, 1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
caseywebdev committed Sep 23, 2013
1 parent 3435a96 commit e14da17
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'
12 changes: 12 additions & 0 deletions Makefile
@@ -0,0 +1,12 @@
BIN=node_modules/.bin/
MOCHA=$(BIN)mocha
WATCHY=$(BIN)watchy
TEST=$(MOCHA) --colors --recursive -R spec

test-w:
NODE_ENV=test $(WATCHY) -w test,underscore-inherit.js -- $(TEST)

test:
NODE_ENV=test $(TEST)

.PHONY: test
2 changes: 2 additions & 0 deletions README.md
@@ -1,6 +1,8 @@
underscore-inherit
==================

[![Build Status](https://secure.travis-ci.org/caseywebdev/underscore-inherit.png)](http://travis-ci.org/caseywebdev/underscore-inherit)

Add constructor extensibility to Underscore.js.

Install
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "underscore-inherit",
"version": "1.0.0",
"version": "1.0.1",
"main": "underscore-inherit.js",
"dependencies": {
"underscore": "x"
Expand Down
10 changes: 8 additions & 2 deletions package.json
@@ -1,13 +1,19 @@
{
"name": "underscore-inherit",
"version": "1.0.0",
"version": "1.0.1",
"author": "Casey Foster <c@sey.me>",
"main": "underscore-inherit",
"repository": {
"type": "git",
"url": "https://github.com/caseywebdev/underscore-inherit"
},
"devDependencies": {
"underscore": "x"
"chai": "x",
"mocha": "x",
"underscore": "x",
"watchy": "x"
},
"scripts": {
"test": "make test"
}
}
16 changes: 16 additions & 0 deletions test/.jshintrc
@@ -0,0 +1,16 @@
{
"boss": true,
"browser": true,
"expr": true,
"eqnull": true,
"indent": 2,
"latedef": true,
"maxlen": 80,
"newcap": false,
"node": true,
"strict": false,
"sub": true,
"trailing": true,
"unused": true,
"white": true
}
79 changes: 79 additions & 0 deletions test/index.js
@@ -0,0 +1,79 @@
var _ = require('underscore');
require('..');
var expect = require('chai').expect;

describe('A simple "class"', function () {
var Klass = _.inherit({
constructor: function () { this.instanceProp = 'a'; },
protoProp: 'b'
}, {
staticProp: 'c'
});

it('is a function', function () {
expect(Klass).to.be.a('function');
});

it('is inherited by its child', function () {
expect(new Klass()).to.be.instanceof(Klass);
});

it('copies static properties', function () {
expect(Klass).to.have.property('staticProp', 'c');
});

it('copies prototype properties', function () {
expect(Klass.prototype).to.have.property('protoProp', 'b');
});

it('equals the special constructor property', function () {
expect(Klass.prototype.constructor).to.equal(Klass);
});

it('invokes the constructor on instance creation', function () {
expect(new Klass()).to.have.property('instanceProp', 'a');
});
});

describe('Single inheritance', function () {
var KlassA = _.inherit({
aProtoMethod: function () {}
}, {
aStaticMethod: function () {}
});

var KlassB = _.inherit(KlassA);

it('works with instanceof', function () {
expect(new KlassB()).to.be.instanceOf(KlassA);
});

it("inherits the parent class's static methods", function () {
expect(KlassB).to.have.property('aStaticMethod');
});

it("inherits the parent class's prototype methods", function () {
expect(KlassB.prototype).to.have.property('aProtoMethod');
});
});

describe('Multiple inheritance', function () {
var KlassA = _.inherit({
theProtoMethod: function () {}
});

var KlassB = _.inherit({
theProtoMethod: function () {}
});

var KlassC = _.inherit(KlassA, KlassB);

it('works with instanceof', function () {
expect(new KlassC()).to.be.instanceOf(KlassA).and.instanceOf(KlassB);
});

it("inherits the most recent parent class's prototype methods", function () {
expect(KlassB.prototype).to.have
.property('theProtoMethod', KlassB.theProtoMethod);
});
});
27 changes: 11 additions & 16 deletions underscore-inherit.js
Expand Up @@ -5,25 +5,20 @@
var node = typeof window === 'undefined';
var _ = node ? require('underscore') : window._;

var slice = [].slice;

// Define the mixin
_.mixin({
inherit: function () {
var protoProps;
var staticProps;
var Parent = _.reduce(arguments, function (Parent, arg) {
if (_.isFunction(arg)) {
if (!Parent) return arg;
return _.inherit(arg, {constructor: Parent});
}
if (_.isObject(arg)) {
if (!protoProps) {
protoProps = arg;
} else if (!staticProps) {
staticProps = arg;
}
}
return Parent;
}, null) || function () {};
var args = slice.call(arguments);
var Parent;
while (_.isFunction(args[0])) {
var arg = args.shift();
Parent = Parent ? _.inherit(arg, {constructor: Parent}) : arg;
}
if (!Parent) Parent = function () {};
var protoProps = args[0];
var staticProps = args[1];

// `Child` is the passed in `constructor` proto property
// or a default function that uses `Parent`'s constructor
Expand Down

0 comments on commit e14da17

Please sign in to comment.