Skip to content

Commit

Permalink
Add async/await support to test cases. Add test case for setInitialOw…
Browse files Browse the repository at this point in the history
…ner.
  • Loading branch information
megamattron committed Jun 19, 2017
1 parent 33c62f5 commit f5f398f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 43 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ignore": ["*.min.js"],
"compact": false,
"presets": ["es2015", "stage-2", "stage-3"]
}
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": {
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-2": "^6.3.13",
"babel-preset-stage-3": "^6.3.13",
"babel-polyfill": "^6.7.4"
}
}
47 changes: 47 additions & 0 deletions test/cryptopunks-setinitial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require('babel-polyfill');
var CryptoPunks2 = artifacts.require("./CryptoPunks2.sol");

contract('CryptoPunks2', function (accounts) {
it("Should start with 0 balance", async function () {
var contract = await CryptoPunks2.deployed();

await contract.setInitialOwner(accounts[0],0);
var balance = await contract.balanceOf.call(accounts[0]);
assert.equal(balance.valueOf(), 1, "Didn't get the initial punk");
var owner = await contract.punkIndexToAddress.call(0);
assert.equal(owner, accounts[0], "Ownership array wrong");
var remaining = await contract.punksRemainingToAssign.call();
assert.equal(9999, remaining);
}),
it("Can not claim punk after set initial owners assigned", async function () {
var contract = await CryptoPunks2.deployed();
await contract.allInitialOwnersAssigned();
try {
await contract.setInitialOwner(accounts[0],0);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
}

}),
it("can not pass an invalid index to assign initial", async function () {
var contract = await CryptoPunks2.deployed();
try {
await contract.setInitialOwner(accounts[0],10000);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
}

}),
it("only owner can assign initial", async function () {
var contract = await CryptoPunks2.deployed();
try {
await contract.setInitialOwner(accounts[1],1);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
}

})
});
52 changes: 9 additions & 43 deletions test/cryptopunks2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require('babel-polyfill');
var CryptoPunks2 = artifacts.require("./CryptoPunks2.sol");

contract('CryptoPunks2', function (accounts) {
Expand Down Expand Up @@ -193,48 +194,13 @@ contract('CryptoPunks2', function (accounts) {
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
})
}),
it("only owner can call setInitialOwner", function () {

This comment has been minimized.

Copy link
@mster1222
var contract;
return CryptoPunks2.deployed().then(function (instance) {
contract = instance;
return instance.setInitialOwner(accounts[1], 10000);
}).then(function () {
// console.log("Bought punk.");
assert(false, "Was supposed to throw but didn't.");
}).catch(function (error) {
if (error.toString().indexOf("invalid opcode") != -1) {
// Expecting a throw here
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
} else {
// if the error is something else (e.g., the assert from previous promise), then we fail the test
assert(false, error.toString());
}
// Get account 0 to buy a punk with enough ether
// console.log("Buying punk 1001 with account 2 which should be allowed.");
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
})
}),
it("should not be able to call setInitialOwner after contract set to all initial assigned", function () {
var contract;
return CryptoPunks2.deployed().then(function (instance) {
contract = instance;
return contract.allInitialOwnersAssigned();
}).then(function () {
return contract.setInitialOwner(accounts[0], 0);
}).then(function () {
// console.log("Bought punk.");
assert(false, "Was supposed to throw but didn't.");
}).catch(function (error) {
if (error.toString().indexOf("invalid opcode") != -1) {
// Expecting a throw here
// console.log("We were expecting a Solidity throw (aka an invalid JUMP), we got one. Test succeeded.");
} else {
// if the error is something else (e.g., the assert from previous promise), then we fail the test
assert(false, error.toString());
}
// Get account 0 to buy a punk with enough ether
// console.log("Buying punk 1001 with account 2 which should be allowed.");
// return contract.buyPunk(1001, {from: accounts[2], value: 10000});
})
it("only owner can call setInitialOwner", async function () {
var contract = await CryptoPunks2.deployed();
try {
await instance.setInitialOwner(accounts[1], 10000);
assert(false, "Should have thrown exception.");
} catch (err) {
// Should catch an exception
}
});
});

0 comments on commit f5f398f

Please sign in to comment.