Skip to content

Commit 6407d78

Browse files
authored
Added time helper tests. (#1521)
* Added time tests. * Minor improvements.
1 parent c2de8ff commit 6407d78

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

test/helpers/test/time.test.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const time = require('../time');
2+
const shouldFail = require('../shouldFail');
3+
const { advanceBlock } = require('../advanceToBlock');
4+
5+
const BigNumber = web3.BigNumber;
6+
require('chai')
7+
.use(require('chai-bignumber')(BigNumber))
8+
.should();
9+
10+
describe('time', function () {
11+
const TOLERANCE_SECONDS = 1;
12+
13+
beforeEach(async function () {
14+
await advanceBlock();
15+
this.start = await time.latest();
16+
});
17+
18+
describe('increase', function () {
19+
it('increases time by a duration', async function () {
20+
await time.increase(time.duration.hours(1));
21+
22+
const end = this.start + time.duration.hours(1);
23+
(await time.latest()).should.be.closeTo(end, TOLERANCE_SECONDS);
24+
});
25+
26+
it('throws with negative durations', async function () {
27+
await shouldFail(time.increase(-1));
28+
});
29+
});
30+
31+
describe('increaseTo', function () {
32+
it('increases time to a time in the future', async function () {
33+
const end = this.start + time.duration.hours(1);
34+
await time.increaseTo(end);
35+
(await time.latest()).should.be.closeTo(end, TOLERANCE_SECONDS);
36+
});
37+
38+
it('throws with a time in the past', async function () {
39+
await shouldFail(time.increaseTo(this.start - 30));
40+
});
41+
});
42+
43+
describe('duration', function () {
44+
it('converts seconds to seconds', function () {
45+
time.duration.seconds(1).should.equal(1);
46+
});
47+
48+
it('converts minutes to seconds', function () {
49+
time.duration.minutes(1).should.equal(60);
50+
});
51+
52+
it('converts hours to seconds', function () {
53+
time.duration.hours(1).should.equal(60 * 60);
54+
});
55+
56+
it('converts days to seconds', function () {
57+
time.duration.days(1).should.equal(60 * 60 * 24);
58+
});
59+
60+
it('converts weeks to seconds', function () {
61+
time.duration.weeks(1).should.equal(60 * 60 * 24 * 7);
62+
});
63+
64+
it('converts years to seconds', function () {
65+
time.duration.years(1).should.equal(60 * 60 * 24 * 365);
66+
});
67+
});
68+
});

test/helpers/time.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ function increase (duration) {
1111
const id = Date.now();
1212

1313
return new Promise((resolve, reject) => {
14+
if (duration < 0) throw Error(`Cannot increase time by a negative amount (${duration})`);
15+
1416
web3.currentProvider.sendAsync({
1517
jsonrpc: '2.0',
1618
method: 'evm_increaseTime',
@@ -40,7 +42,7 @@ function increase (duration) {
4042
async function increaseTo (target) {
4143
const now = (await latest());
4244

43-
if (target < now) throw Error(`Cannot increase current time(${now}) to a moment in the past(${target})`);
45+
if (target < now) throw Error(`Cannot increase current time (${now}) to a moment in the past (${target})`);
4446
const diff = target - now;
4547
return increase(diff);
4648
}

0 commit comments

Comments
 (0)