|
| 1 | +var MetaCoin = artifacts.require("./MetaCoin.sol"); |
| 2 | + |
| 3 | +contract('MetaCoin', function(accounts) { |
| 4 | + it("should put 10000 MetaCoin in the first account", function() { |
| 5 | + return MetaCoin.deployed().then(function(instance) { |
| 6 | + return instance.getBalance.call(accounts[0]); |
| 7 | + }).then(function(balance) { |
| 8 | + assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account"); |
| 9 | + }); |
| 10 | + }); |
| 11 | + it("should call a function that depends on a linked library", function() { |
| 12 | + var meta; |
| 13 | + var metaCoinBalance; |
| 14 | + var metaCoinEthBalance; |
| 15 | + |
| 16 | + return MetaCoin.deployed().then(function(instance) { |
| 17 | + meta = instance; |
| 18 | + return meta.getBalance.call(accounts[0]); |
| 19 | + }).then(function(outCoinBalance) { |
| 20 | + metaCoinBalance = outCoinBalance.toNumber(); |
| 21 | + return meta.getBalanceInEth.call(accounts[0]); |
| 22 | + }).then(function(outCoinBalanceEth) { |
| 23 | + metaCoinEthBalance = outCoinBalanceEth.toNumber(); |
| 24 | + }).then(function() { |
| 25 | + assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, "Library function returned unexpected function, linkage may be broken"); |
| 26 | + }); |
| 27 | + }); |
| 28 | + it("should send coin correctly", function() { |
| 29 | + var meta; |
| 30 | + |
| 31 | + // Get initial balances of first and second account. |
| 32 | + var account_one = accounts[0]; |
| 33 | + var account_two = accounts[1]; |
| 34 | + |
| 35 | + var account_one_starting_balance; |
| 36 | + var account_two_starting_balance; |
| 37 | + var account_one_ending_balance; |
| 38 | + var account_two_ending_balance; |
| 39 | + |
| 40 | + var amount = 10; |
| 41 | + |
| 42 | + return MetaCoin.deployed().then(function(instance) { |
| 43 | + meta = instance; |
| 44 | + return meta.getBalance.call(account_one); |
| 45 | + }).then(function(balance) { |
| 46 | + account_one_starting_balance = balance.toNumber(); |
| 47 | + return meta.getBalance.call(account_two); |
| 48 | + }).then(function(balance) { |
| 49 | + account_two_starting_balance = balance.toNumber(); |
| 50 | + return meta.sendCoin(account_two, amount, {from: account_one}); |
| 51 | + }).then(function() { |
| 52 | + return meta.getBalance.call(account_one); |
| 53 | + }).then(function(balance) { |
| 54 | + account_one_ending_balance = balance.toNumber(); |
| 55 | + return meta.getBalance.call(account_two); |
| 56 | + }).then(function(balance) { |
| 57 | + account_two_ending_balance = balance.toNumber(); |
| 58 | + |
| 59 | + assert.equal(account_one_ending_balance, account_one_starting_balance - amount, "Amount wasn't correctly taken from the sender"); |
| 60 | + assert.equal(account_two_ending_balance, account_two_starting_balance + amount, "Amount wasn't correctly sent to the receiver"); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments