Skip to content

Commit

Permalink
Update to use tape
Browse files Browse the repository at this point in the history
Funny behavior with the mocha test runner, globals not so good..
  • Loading branch information
Ilya Radchenko committed Feb 5, 2015
1 parent 042a7ac commit f6b0394
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 63 deletions.
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -2,6 +2,11 @@
"name": "interval",
"version": "0.0.9",
"description": "Converts common units of time to milliseconds",
"author": "J. Jordan <jordan@styosis.com>",
"main": "./lib/interval.js",
"scripts": {
"test": "node test | tap-spec"
},
"keywords": [
"interval",
"timespan",
Expand All @@ -21,14 +26,9 @@
"lib": "./lib",
"test": "./test"
},
"author": "J. Jordan <jordan@styosis.com>",
"main": "./lib/interval.js",
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": "*",
"expect.js": "*"
"tap-spec": "^2.2.0",
"tape": "^3.5.0"
},
"engines": {
"node": ">= 0.4.0",
Expand Down
5 changes: 5 additions & 0 deletions test/index.js
@@ -0,0 +1,5 @@
'use strict';

// run tests
require('./interval');
require('./math');
121 changes: 73 additions & 48 deletions test/interval.js
@@ -1,91 +1,116 @@
var expect = require('expect.js');
var interval = require('../');

describe('interval', function() {
'use strict';

// zero undefined properties
function create(i) {
var props = ['days', 'hours', 'minutes', 'seconds', 'milliseconds'];
props.forEach(function(prop) {
if (!i[prop]) {
i[prop] = 0;
}
});
return i;
}
var test = require('tape');
var interval = require('../');

it('should convert days property to milliseconds', function() {
expect(interval({ days: 4 })).to.be(4 * 24 * 60 * 60 * 1000);
test('interval', function (t) {
t.test('should convert days property to milliseconds', function (n) {
n.equal(interval({ days: 4 }), 4 * 24 * 60 * 60 * 1000);
n.end();
});

it('should convert hours property to milliseconds', function() {
expect(interval({ hours: 2 })).to.be(2 * 60 * 60 * 1000);
t.test('should convert hours property to milliseconds', function (n) {
n.equal(interval({ hours: 2 }), 2 * 60 * 60 * 1000);
n.end();
});

it('should convert minutes property to milliseconds', function() {
expect(interval({ minutes: 6 })).to.be(6 * 60 * 1000);
t.test('should convert minutes property to milliseconds', function (n) {
n.equal(interval({ minutes: 6 }), 6 * 60 * 1000);
n.end();
});

it('should convert seconds property to milliseconds', function() {
expect(interval({ seconds: 25 })).to.be(25 * 1000);
t.test('should convert seconds property to milliseconds', function (n) {
n.equal(interval({ seconds: 25 }), 25 * 1000);
n.end();
});

it('should convert milliseconds property to milliseconds', function() {
expect(interval({ milliseconds: 25 })).to.be(25);
t.test('should convert milliseconds property to milliseconds', function (n) {
n.equal(interval({ milliseconds: 25 }), 25);
n.end();
});

it('should convert composite units to milliseconds', function() {
t.test('should convert composite units to milliseconds', function (n) {
var actual = interval({ hours: 7, milliseconds: 43 });
expect(actual).to.be(7 * 60 * 60 * 1000 + 43);

n.equal(actual, 7 * 60 * 60 * 1000 + 43);
n.end();
});

it('should return numeric parameter as milliseconds', function() {
expect(interval(852)).to.be(852);
t.test('should return numeric parameter as milliseconds', function (n) {
n.equal(interval(852), 852);
n.end();
});

it('should stringify hours', function() {
expect(interval.stringify({ hours: 1 })).to.be('1 hours');
t.test('should stringify hours', function (n) {
n.equal(interval.stringify({ hours: 1 }), '1 hours');
n.end();
});

it('should stringify minutes', function() {
expect(interval.stringify({ minutes: 1 })).to.be('1 minutes');
t.test('should stringify minutes', function (n) {
n.equal(interval.stringify({ minutes: 1 }), '1 minutes');
n.end();
});

it('should build object from milliseconds', function() {
t.test('should build object from milliseconds', function (n) {
var actual = interval.fromMilliseconds((50 * 1000) + 40);
var expected = create({ seconds: 50, milliseconds: 40 });
expect(actual).to.eql(expected);

n.same(actual, expected);
n.end();
});

it('should normalize over accounted units', function() {
t.test('should normalize over accounted units', function (n) {
var source = { seconds: 75 };
var expected = create({ minutes: 1, seconds: 15 });
expect(interval.normalize(source)).to.eql(expected);

n.same(interval.normalize(source), expected);
n.end();
});

it('should add interval to date', function() {
t.test('should add interval to date', function (n) {
var date = new Date(2000, 1, 2);
var actual = interval.add(date, {days: 1});
var actual = interval.add(date, { days: 1 });
var expected = new Date(2000, 1, 3);
expect(actual).to.eql(expected);

n.same(actual, expected);
n.end();
});

it('should add negative interval to date', function() {
t.test('should add negative interval to date', function (n) {
var date = new Date(2000, 1, 2);
var actual = interval.add(date, {days: -1});
var actual = interval.add(date, { days: -1 });
var expected = new Date(2000, 1, 1);
expect(actual).to.eql(expected);

n.same(actual, expected);
n.end();
});

it('should subtract interval from date', function() {
t.test('should subtract interval from date', function (n) {
var date = new Date(2100, 5, 10);
var actual = interval.subtract(date, {days: 7});
var actual = interval.subtract(date, { days: 7 });
var expected = new Date(2100, 5, 3);
expect(actual).to.eql(expected);

n.same(actual, expected);
n.end();
});

it('should add intervals', function() {
var actual = interval.add({hours: 5}, {days: 7});
expect(actual).to.eql(interval({hours: 5, days: 7}));
t.test('should add intervals', function (n) {
var actual = interval.add({ hours: 5 }, { days: 7 });

n.equal(actual, interval({ hours: 5, days: 7 }));
n.end();
});
});

// zero undefined properties
function create(i) {
var props = ['days', 'hours', 'minutes', 'seconds', 'milliseconds'];

props.forEach(function(prop) {
if (!i[prop]) {
i[prop] = 0;
}
});

return i;
}
21 changes: 13 additions & 8 deletions test/math.js
@@ -1,15 +1,20 @@
var expect = require('expect.js');
var math = require('../lib/math');
'use strict';

describe('math', function () {
var test = require('tape');
var math = require('../lib/math');

it('should divide evenly', function () {
test('math', function (t) {
t.test('should divide evenly', function (n) {
var result = math.integerDivision(100, 10);
expect(result).to.eql({ quotient: 10, remainder: 0});

n.same(result, { quotient: 10, remainder: 0 });
n.end();
});
it('should have remainder', function () {

t.test('should have remainder', function (n) {
var result = math.integerDivision(15, 10);
expect(result).to.eql({ quotient: 1, remainder: 5});

n.same(result, { quotient: 1, remainder: 5 });
n.end();
});
});

0 comments on commit f6b0394

Please sign in to comment.