|
| 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 | +}); |
0 commit comments