1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
const Lottery = artifacts.require("Lottery")
const PREFIX = "Returned error: VM Exception while processing transaction: ";
const tryCatch = async function(promise, errType) {
try {
await promise;
throw null;
}
catch (error) {
assert(error, "Expected an error but did not get one");
assert(error.message.startsWith(PREFIX + errType), "Expected an error starting with '" + PREFIX + errType + "' but got '" + error.message + "' instead");
}
};
contract('Lottery Contract', (accounts) => {
let lotteryContract
before(async () => {
lotteryContract = await Lottery.new();
})
it("should not have revealed times at first", async function() {
const count = await lotteryContract.revealedTimesCount();
assert.equal(count.toString(), '0')
})
it("should have revealed data after calling reveal", async function() {
await lotteryContract.reveal(12345);
const count = await lotteryContract.revealedTimesCount();
assert.equal(count.toString(), '1');
const revealedTime = await lotteryContract.revealedTimes(0);
assert.equal(revealedTime.toString(), '12345');
const number = await lotteryContract.numberOfTime(12345);
assert.notEqual(number.toString(), '0');
const futureNumber = await lotteryContract.numberOfTime(12346);
assert.equal(futureNumber.toString(), '0');
})
it("should have more revealed data after calling reveal again", async function() {
await lotteryContract.reveal(12346);
const count = await lotteryContract.revealedTimesCount();
assert.equal(count.toString(), '2');
const revealedTime = await lotteryContract.revealedTimes(1);
assert.equal(revealedTime.toString(), '12346');
const number = await lotteryContract.numberOfTime(12346);
assert.notEqual(number.toString(), '0');
})
it("should reject future time", async function() {
await tryCatch(lotteryContract.reveal(1664631204), 'revert');
})
it("should reject duplicated time", async function() {
await tryCatch(lotteryContract.reveal(12345), 'revert');
})
})
|