Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
openzeppelin-contracts-upgradeable
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
俞永鹏
openzeppelin-contracts-upgradeable
Commits
6407d781
Unverified
Commit
6407d781
authored
Nov 27, 2018
by
Nicolás Venturo
Committed by
GitHub
Nov 27, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added time helper tests. (#1521)
* Added time tests. * Minor improvements.
parent
c2de8ffd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
1 deletions
+71
-1
time.test.js
test/helpers/test/time.test.js
+68
-0
time.js
test/helpers/time.js
+3
-1
No files found.
test/helpers/test/time.test.js
0 → 100644
View file @
6407d781
const
time
=
require
(
'../time'
);
const
shouldFail
=
require
(
'../shouldFail'
);
const
{
advanceBlock
}
=
require
(
'../advanceToBlock'
);
const
BigNumber
=
web3
.
BigNumber
;
require
(
'chai'
)
.
use
(
require
(
'chai-bignumber'
)(
BigNumber
))
.
should
();
describe
(
'time'
,
function
()
{
const
TOLERANCE_SECONDS
=
1
;
beforeEach
(
async
function
()
{
await
advanceBlock
();
this
.
start
=
await
time
.
latest
();
});
describe
(
'increase'
,
function
()
{
it
(
'increases time by a duration'
,
async
function
()
{
await
time
.
increase
(
time
.
duration
.
hours
(
1
));
const
end
=
this
.
start
+
time
.
duration
.
hours
(
1
);
(
await
time
.
latest
()).
should
.
be
.
closeTo
(
end
,
TOLERANCE_SECONDS
);
});
it
(
'throws with negative durations'
,
async
function
()
{
await
shouldFail
(
time
.
increase
(
-
1
));
});
});
describe
(
'increaseTo'
,
function
()
{
it
(
'increases time to a time in the future'
,
async
function
()
{
const
end
=
this
.
start
+
time
.
duration
.
hours
(
1
);
await
time
.
increaseTo
(
end
);
(
await
time
.
latest
()).
should
.
be
.
closeTo
(
end
,
TOLERANCE_SECONDS
);
});
it
(
'throws with a time in the past'
,
async
function
()
{
await
shouldFail
(
time
.
increaseTo
(
this
.
start
-
30
));
});
});
describe
(
'duration'
,
function
()
{
it
(
'converts seconds to seconds'
,
function
()
{
time
.
duration
.
seconds
(
1
).
should
.
equal
(
1
);
});
it
(
'converts minutes to seconds'
,
function
()
{
time
.
duration
.
minutes
(
1
).
should
.
equal
(
60
);
});
it
(
'converts hours to seconds'
,
function
()
{
time
.
duration
.
hours
(
1
).
should
.
equal
(
60
*
60
);
});
it
(
'converts days to seconds'
,
function
()
{
time
.
duration
.
days
(
1
).
should
.
equal
(
60
*
60
*
24
);
});
it
(
'converts weeks to seconds'
,
function
()
{
time
.
duration
.
weeks
(
1
).
should
.
equal
(
60
*
60
*
24
*
7
);
});
it
(
'converts years to seconds'
,
function
()
{
time
.
duration
.
years
(
1
).
should
.
equal
(
60
*
60
*
24
*
365
);
});
});
});
test/helpers/time.js
View file @
6407d781
...
@@ -11,6 +11,8 @@ function increase (duration) {
...
@@ -11,6 +11,8 @@ function increase (duration) {
const
id
=
Date
.
now
();
const
id
=
Date
.
now
();
return
new
Promise
((
resolve
,
reject
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
if
(
duration
<
0
)
throw
Error
(
`Cannot increase time by a negative amount (
${
duration
}
)`
);
web3
.
currentProvider
.
sendAsync
({
web3
.
currentProvider
.
sendAsync
({
jsonrpc
:
'2.0'
,
jsonrpc
:
'2.0'
,
method
:
'evm_increaseTime'
,
method
:
'evm_increaseTime'
,
...
@@ -40,7 +42,7 @@ function increase (duration) {
...
@@ -40,7 +42,7 @@ function increase (duration) {
async
function
increaseTo
(
target
)
{
async
function
increaseTo
(
target
)
{
const
now
=
(
await
latest
());
const
now
=
(
await
latest
());
if
(
target
<
now
)
throw
Error
(
`Cannot increase current time
(
${
now
}
) to a moment in the past
(
${
target
}
)`
);
if
(
target
<
now
)
throw
Error
(
`Cannot increase current time
(
${
now
}
) to a moment in the past
(
${
target
}
)`
);
const
diff
=
target
-
now
;
const
diff
=
target
-
now
;
return
increase
(
diff
);
return
increase
(
diff
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment