Skip to content

Commit f5f398f

Browse files
committed
Add async/await support to test cases. Add test case for setInitialOwner.
1 parent 33c62f5 commit f5f398f

File tree

4 files changed

+69
-43
lines changed

4 files changed

+69
-43
lines changed

.babelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ignore": ["*.min.js"],
3+
"compact": false,
4+
"presets": ["es2015", "stage-2", "stage-3"]
5+
}

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": {
3+
"babel-preset-es2015": "^6.3.13",
4+
"babel-preset-stage-2": "^6.3.13",
5+
"babel-preset-stage-3": "^6.3.13",
6+
"babel-polyfill": "^6.7.4"
7+
}
8+
}

test/cryptopunks-setinitial.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require('babel-polyfill');
2+
var CryptoPunks2 = artifacts.require("./CryptoPunks2.sol");
3+
4+
contract('CryptoPunks2', function (accounts) {
5+
it("Should start with 0 balance", async function () {
6+
var contract = await CryptoPunks2.deployed();
7+
8+
await contract.setInitialOwner(accounts[0],0);
9+
var balance = await contract.balanceOf.call(accounts[0]);
10+
assert.equal(balance.valueOf(), 1, "Didn't get the initial punk");
11+
var owner = await contract.punkIndexToAddress.call(0);
12+
assert.equal(owner, accounts[0], "Ownership array wrong");
13+
var remaining = await contract.punksRemainingToAssign.call();
14+
assert.equal(9999, remaining);
15+
}),
16+
it("Can not claim punk after set initial owners assigned", async function () {
17+
var contract = await CryptoPunks2.deployed();
18+
await contract.allInitialOwnersAssigned();
19+
try {
20+
await contract.setInitialOwner(accounts[0],0);
21+
assert(false, "Should have thrown exception.");
22+
} catch (err) {
23+
// Should catch an exception
24+
}
25+
26+
}),
27+
it("can not pass an invalid index to assign initial", async function () {
28+
var contract = await CryptoPunks2.deployed();
29+
try {
30+
await contract.setInitialOwner(accounts[0],10000);
31+
assert(false, "Should have thrown exception.");
32+
} catch (err) {
33+
// Should catch an exception
34+
}
35+
36+
}),
37+
it("only owner can assign initial", async function () {
38+
var contract = await CryptoPunks2.deployed();
39+
try {
40+
await contract.setInitialOwner(accounts[1],1);
41+
assert(false, "Should have thrown exception.");
42+
} catch (err) {
43+
// Should catch an exception
44+
}
45+
46+
})
47+
});

test/cryptopunks2.js

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require('babel-polyfill');
12
var CryptoPunks2 = artifacts.require("./CryptoPunks2.sol");
23

34
contract('CryptoPunks2', function (accounts) {
@@ -193,48 +194,13 @@ contract('CryptoPunks2', function (accounts) {
193194
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
194195
})
195196
}),
196-
it("only owner can call setInitialOwner", function () {
197-
var contract;
198-
return CryptoPunks2.deployed().then(function (instance) {
199-
contract = instance;
200-
return instance.setInitialOwner(accounts[1], 10000);
201-
}).then(function () {
202-
// console.log("Bought punk.");
203-
assert(false, "Was supposed to throw but didn't.");
204-
}).catch(function (error) {
205-
if (error.toString().indexOf("invalid opcode") != -1) {
206-
// Expecting a throw here
207-
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
208-
} else {
209-
// if the error is something else (e.g., the assert from previous promise), then we fail the test
210-
assert(false, error.toString());
211-
}
212-
// Get account 0 to buy a punk with enough ether
213-
// console.log("Buying punk 1001 with account 2 which should be allowed.");
214-
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
215-
})
216-
}),
217-
it("should not be able to call setInitialOwner after contract set to all initial assigned", function () {
218-
var contract;
219-
return CryptoPunks2.deployed().then(function (instance) {
220-
contract = instance;
221-
return contract.allInitialOwnersAssigned();
222-
}).then(function () {
223-
return contract.setInitialOwner(accounts[0], 0);
224-
}).then(function () {
225-
// console.log("Bought punk.");
226-
assert(false, "Was supposed to throw but didn't.");
227-
}).catch(function (error) {
228-
if (error.toString().indexOf("invalid opcode") != -1) {
229-
// Expecting a throw here
230-
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
231-
} else {
232-
// if the error is something else (e.g., the assert from previous promise), then we fail the test
233-
assert(false, error.toString());
234-
}
235-
// Get account 0 to buy a punk with enough ether
236-
// console.log("Buying punk 1001 with account 2 which should be allowed.");
237-
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
238-
})
197+
it("only owner can call setInitialOwner", async function () {
198+
var contract = await CryptoPunks2.deployed();
199+
try {
200+
await instance.setInitialOwner(accounts[1], 10000);
201+
assert(false, "Should have thrown exception.");
202+
} catch (err) {
203+
// Should catch an exception
204+
}
239205
});
240206
});

0 commit comments

Comments
 (0)