Permalink
Browse files

Add SnapshotTest

  • Loading branch information...
Recmo committed Aug 22, 2017
1 parent 413543e commit c0ffee4eea5200ab099b6dab76b934e5c675eb58
Showing with 162 additions and 5 deletions.
  1. +58 −0 contracts/test/SnapshotTest.sol
  2. +1 −0 test/Neumark.js
  3. +100 −0 test/SnapshotTest.js
  4. +1 −1 test/helpers/expectThrow.js
  5. +2 −4 test/helpers/increaseTime.js
@@ -0,0 +1,58 @@
pragma solidity ^0.4.11;
import 'snapshottoken/contracts/Snapshot/Snapshot.sol';
import 'snapshottoken/contracts/Snapshot/DailyAndSnapshotable.sol';
contract SnapshotTest is
Snapshot,
DailyAndSnapshotable
{
Values[] val;
function curDays()
public
constant
returns (uint)
{
return block.timestamp / 1 days;
}
function hasValue()
public
constant
returns (bool)
{
return hasValue(val);
}
function getValue(uint256 def)
public
constant
returns (uint256)
{
return getValue(val, def);
}
function hasValueAt(uint256 snapshot)
public
constant
returns (bool)
{
return hasValueAt(val, snapshot);
}
function getValueAt(uint256 snapshot, uint256 def)
public
constant
returns (uint256)
{
return getValueAt(val, snapshot, def);
}
function setValue(uint256 x)
public
{
setValue(val, x);
}
}
View
@@ -42,6 +42,7 @@ contract("Neumark", accounts => {
"10000 wasn't in the first account"
);
});
it("should allow controller to burn tokens", async () => {
assert(await controller.generateTokens(accounts[0], 10000, { from: accounts[0] }));
assert(await controller.destroyTokens(accounts[0], 1000, { from: accounts[0] }));
View
@@ -0,0 +1,100 @@
import gasCost from "./helpers/gasCost";
import { latestTimestamp } from "./helpers/latestTime";
import increaseTime from "./helpers/increaseTime";
import moment from "moment";
import expectThrow from "./helpers/expectThrow";
const SnapshotTest = artifacts.require("./test/SnapshotTest.sol");
const day = 24 * 3600;
contract("Snapshot", accounts => {
let value;
const createSnapshot = async () => {
const r = await value.createSnapshot();
assert.equal(r.logs.length, 1);
assert.equal(r.logs[0].event, "SnapshotCreated");
return r.logs[0].args.snapshot;
};
beforeEach(async () => {
value = await SnapshotTest.new();
});
it("should deploy", async () => {
console.log(`\tSnapshot took ${gasCost(value)}.`);
});
it("should be initially unset", async () => {
assert.isFalse(await value.hasValue.call());
});
it("should initially return default", async () => {
assert.isFalse(await value.hasValue.call());
});
it("should initially return default", async () => {
assert.equal(12, await value.getValue.call(12));
assert.equal(42, await value.getValue.call(42));
});
it("should create a snapshot", async () => {
const r = await value.createSnapshot();
console.log(`\tcreateSnapshot took ${gasCost(r)}`);
});
it("should set a new value", async () => {
const r = await value.setValue(1234);
console.log(`\tsetValue took ${gasCost(r)}`);
assert.equal(1234, await value.getValue.call(12));
assert.isTrue(await value.hasValue.call());
});
it("should reset value", async () => {
await value.setValue(1234);
const r = await value.setValue(12345);
console.log(`\tsetValue took ${gasCost(r)}`);
assert.equal(12345, await value.getValue.call(12));
assert.isTrue(await value.hasValue.call());
});
it("should keep values in snapshots", async () => {
const before = await createSnapshot();
await value.setValue(100);
const middle = await createSnapshot();
await value.setValue(200);
const after = await createSnapshot();
assert.isFalse(await value.hasValueAt.call(before));
assert.isTrue(await value.hasValueAt.call(middle));
assert.isTrue(await value.hasValueAt.call(after));
assert.equal(41, await value.getValueAt.call(before, 41));
assert.equal(100, await value.getValueAt.call(middle, 41));
assert.equal(200, await value.getValueAt.call(after, 41));
});
it("should create daily snapshots", async () => {
const day0 = await value.snapshotAt.call(latestTimestamp() + 0 * day);
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day);
const day2 = await value.snapshotAt.call(latestTimestamp() + 2 * day);
const day3 = await value.snapshotAt.call(latestTimestamp() + 3 * day);
await value.setValue(100);
await increaseTime(moment.duration({ days: 1 }));
await value.setValue(200);
await increaseTime(moment.duration({ days: 1 }));
await value.setValue(300);
assert.equal(41, await value.getValueAt.call(day0, 41));
assert.equal(100, await value.getValueAt.call(day1, 41));
assert.equal(200, await value.getValueAt.call(day2, 41));
});
it("should throw when queried in the future", async () => {
const day1 = await value.snapshotAt.call(latestTimestamp() + 1 * day);
await expectThrow(value.getValueAt.call(day1, 41));
});
});
@@ -10,7 +10,7 @@ export default async promise => {
// we distinguish this from an actual out of gas event? (The
// testrpc log actually show an 'invalid jump' event.)
const outOfGas = error.message.search("out of gas") >= 0;
assert(invalidOpcode || outOfGas, "Expected throw, got '" + error + "' instead");
assert(invalidOpcode || outOfGas, `Expected throw, got '${error}' instead`);
return;
}
assert.fail("Expected throw not received");
@@ -10,7 +10,7 @@ export default function increaseTime(duration) {
jsonrpc: "2.0",
method: "evm_increaseTime",
params: [duration.asSeconds()],
id: id,
id,
},
err1 => {
if (err1) return reject(err1);
@@ -21,9 +21,7 @@ export default function increaseTime(duration) {
method: "evm_mine",
id: id + 1,
},
(err2, res) => {
return err2 ? reject(err2) : resolve(res);
}
(err2, res) => (err2 ? reject(err2) : resolve(res))
);
}
);

0 comments on commit c0ffee4

Please sign in to comment.