Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Commit c0ffee4

Browse files
author
Remco Bloemen
committed
Add SnapshotTest
1 parent 413543e commit c0ffee4

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

contracts/test/SnapshotTest.sol

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
pragma solidity ^0.4.11;
2+
3+
import 'snapshottoken/contracts/Snapshot/Snapshot.sol';
4+
import 'snapshottoken/contracts/Snapshot/DailyAndSnapshotable.sol';
5+
6+
contract SnapshotTest is
7+
Snapshot,
8+
DailyAndSnapshotable
9+
{
10+
Values[] val;
11+
12+
function curDays()
13+
public
14+
constant
15+
returns (uint)
16+
{
17+
return block.timestamp / 1 days;
18+
}
19+
20+
function hasValue()
21+
public
22+
constant
23+
returns (bool)
24+
{
25+
return hasValue(val);
26+
}
27+
28+
29+
function getValue(uint256 def)
30+
public
31+
constant
32+
returns (uint256)
33+
{
34+
return getValue(val, def);
35+
}
36+
37+
function hasValueAt(uint256 snapshot)
38+
public
39+
constant
40+
returns (bool)
41+
{
42+
return hasValueAt(val, snapshot);
43+
}
44+
45+
function getValueAt(uint256 snapshot, uint256 def)
46+
public
47+
constant
48+
returns (uint256)
49+
{
50+
return getValueAt(val, snapshot, def);
51+
}
52+
53+
function setValue(uint256 x)
54+
public
55+
{
56+
setValue(val, x);
57+
}
58+
}

test/Neumark.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ contract("Neumark", accounts => {
4242
"10000 wasn't in the first account"
4343
);
4444
});
45+
4546
it("should allow controller to burn tokens", async () => {
4647
assert(await controller.generateTokens(accounts[0], 10000, { from: accounts[0] }));
4748
assert(await controller.destroyTokens(accounts[0], 1000, { from: accounts[0] }));

test/SnapshotTest.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import gasCost from "./helpers/gasCost";
2+
import { latestTimestamp } from "./helpers/latestTime";
3+
import increaseTime from "./helpers/increaseTime";
4+
import moment from "moment";
5+
import expectThrow from "./helpers/expectThrow";
6+
7+
const SnapshotTest = artifacts.require("./test/SnapshotTest.sol");
8+
9+
const day = 24 * 3600;
10+
11+
contract("Snapshot", accounts => {
12+
let value;
13+
14+
const createSnapshot = async () => {
15+
const r = await value.createSnapshot();
16+
assert.equal(r.logs.length, 1);
17+
assert.equal(r.logs[0].event, "SnapshotCreated");
18+
return r.logs[0].args.snapshot;
19+
};
20+
21+
beforeEach(async () => {
22+
value = await SnapshotTest.new();
23+
});
24+
25+
it("should deploy", async () => {
26+
console.log(`\tSnapshot took ${gasCost(value)}.`);
27+
});
28+
29+
it("should be initially unset", async () => {
30+
assert.isFalse(await value.hasValue.call());
31+
});
32+
33+
it("should initially return default", async () => {
34+
assert.isFalse(await value.hasValue.call());
35+
});
36+
37+
it("should initially return default", async () => {
38+
assert.equal(12, await value.getValue.call(12));
39+
assert.equal(42, await value.getValue.call(42));
40+
});
41+
42+
it("should create a snapshot", async () => {
43+
const r = await value.createSnapshot();
44+
console.log(`\tcreateSnapshot took ${gasCost(r)}`);
45+
});
46+
47+
it("should set a new value", async () => {
48+
const r = await value.setValue(1234);
49+
console.log(`\tsetValue took ${gasCost(r)}`);
50+
51+
assert.equal(1234, await value.getValue.call(12));
52+
assert.isTrue(await value.hasValue.call());
53+
});
54+
55+
it("should reset value", async () => {
56+
await value.setValue(1234);
57+
58+
const r = await value.setValue(12345);
59+
console.log(`\tsetValue took ${gasCost(r)}`);
60+
61+
assert.equal(12345, await value.getValue.call(12));
62+
assert.isTrue(await value.hasValue.call());
63+
});
64+
65+
it("should keep values in snapshots", async () => {
66+
const before = await createSnapshot();
67+
await value.setValue(100);
68+
const middle = await createSnapshot();
69+
await value.setValue(200);
70+
const after = await createSnapshot();
71+
72+
assert.isFalse(await value.hasValueAt.call(before));
73+
assert.isTrue(await value.hasValueAt.call(middle));
74+
assert.isTrue(await value.hasValueAt.call(after));
75+
assert.equal(41, await value.getValueAt.call(before, 41));
76+
assert.equal(100, await value.getValueAt.call(middle, 41));
77+
assert.equal(200, await value.getValueAt.call(after, 41));
78+
});
79+
80+
it("should create daily snapshots", async () => {
81+
const day0 = await value.snapshotAt.call(latestTimestamp() + 0 * day);
82+
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day);
83+
const day2 = await value.snapshotAt.call(latestTimestamp() + 2 * day);
84+
const day3 = await value.snapshotAt.call(latestTimestamp() + 3 * day);
85+
await value.setValue(100);
86+
await increaseTime(moment.duration({ days: 1 }));
87+
await value.setValue(200);
88+
await increaseTime(moment.duration({ days: 1 }));
89+
await value.setValue(300);
90+
91+
assert.equal(41, await value.getValueAt.call(day0, 41));
92+
assert.equal(100, await value.getValueAt.call(day1, 41));
93+
assert.equal(200, await value.getValueAt.call(day2, 41));
94+
});
95+
96+
it("should throw when queried in the future", async () => {
97+
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day);
98+
await expectThrow(value.getValueAt.call(day1, 41));
99+
});
100+
});

test/helpers/expectThrow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default async promise => {
1010
// we distinguish this from an actual out of gas event? (The
1111
// testrpc log actually show an 'invalid jump' event.)
1212
const outOfGas = error.message.search("out of gas") >= 0;
13-
assert(invalidOpcode || outOfGas, "Expected throw, got '" + error + "' instead");
13+
assert(invalidOpcode || outOfGas, `Expected throw, got '${error}' instead`);
1414
return;
1515
}
1616
assert.fail("Expected throw not received");

test/helpers/increaseTime.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function increaseTime(duration) {
1010
jsonrpc: "2.0",
1111
method: "evm_increaseTime",
1212
params: [duration.asSeconds()],
13-
id: id,
13+
id,
1414
},
1515
err1 => {
1616
if (err1) return reject(err1);
@@ -21,9 +21,7 @@ export default function increaseTime(duration) {
2121
method: "evm_mine",
2222
id: id + 1,
2323
},
24-
(err2, res) => {
25-
return err2 ? reject(err2) : resolve(res);
26-
}
24+
(err2, res) => (err2 ? reject(err2) : resolve(res))
2725
);
2826
}
2927
);

0 commit comments

Comments
 (0)