/* eslint no-undef: "off" */ const logger = console.log.bind(console) // eslint-disable-line no-console function logEvents(result, name) { // eslint-disable-line no-unused-vars result.logs.forEach((log) => { if (log.event === name) { logger(log) } }) } function assertEventInResult(result, name) { let isIn = false result.logs.forEach((log) => { if (log.event === name) { isIn = true } }) assert.equal(isIn, true, `Event ${name} should be in the result!`) } const ShowTrader = artifacts.require('ShowTrader') contract('ShowTrader Test', (accounts) => { let trader const danceData = '0x111000' const musicData = '0x000111' it('should create 1 show', () => { const player = accounts[0] const showName = '1st show' return ShowTrader.deployed() .then((instance) => { trader = instance return trader.createFirstShow(showName, danceData, musicData, { from: player }) }) .then((result) => { // logEvents(result, 'NewShow') assertEventInResult(result, 'NewShow') return trader.getShowsByOwner.call(player) }) .then((shows) => { const id = shows[0].toNumber() return trader.shows(id) }) .then((show) => { assert.equal(show[0], showName) assert.equal(show[1], danceData) assert.equal(show[2], musicData) }) }) // The second test begin... it('shoud exchange 2 shows', () => { const name1 = 'show1' const name2 = 'show2' const player1 = accounts[1] // The account[0] is used for the 1st test // Note: 'VM Exception while processing transaction: revert', // usually means that the require() statement is not met const player2 = accounts[2] let id1 let id2 return ShowTrader.deployed() .then((instance) => { trader = instance return trader.createFirstShow(name1, danceData, musicData, { from: player1 }) }) .then((result) => { assertEventInResult(result, 'NewShow') return trader.createFirstShow(name2, danceData, musicData, { from: player2 }) }) .then((result) => { assertEventInResult(result, 'NewShow') return trader.getShowsByOwner.call(player1) }) .then((shows1) => { id1 = shows1[0].toNumber() return trader.getShowsByOwner.call(player2) }) .then((shows2) => { id2 = shows2[0].toNumber() return trader.wantToExchange(id1, id2, { from: player1 }) }) .then((result) => { // logEvents(result, 'WantToExchange') assertEventInResult(result, 'WantToExchange') return trader.wantToExchange(id2, id1, { from: player2 }) }) .then((result) => { // logEvents(result, 'Transfer') assertEventInResult(result, 'Transfer') return trader.showToOwner(id1) }) .then((owner1) => { assert.equal(owner1, player2, 'owner of show1 should be player2!') return trader.showToOwner(id2) }) .then((owner2) => { assert.equal(owner2, player1, 'owner of show2 should be player1!') }) }) it('should buy & sell', () => { const player1 = accounts[1] const player2 = accounts[2] let id1 return ShowTrader.deployed() .then((instance) => { trader = instance return trader.getShowsByOwner.call(player1) }) .then((shows1) => { id1 = shows1[0].toNumber() logger(id1) return trader.initAccount({ from: player1 }) }) .then(() => { logger() return trader.ownerAccount(player1) }) .then((remains) => { logger(remains.toNumber()) return trader.initAccount({ from: player2 }) }) .then(() => { logger() return trader.ownerAccount(player2) }) .then((remains) => { logger(remains.toNumber()) return trader.sell(id1, 233, { from: player1 }) }) .then(() => { logger() return trader.showToPrice(id1) }) .then((price) => { logger(price.toNumber()) return trader.buy(id1, { from: player2 }) }) .then(() => { logger() return trader.showToOwner(id1) }) .then((owner) => { logger(owner) logger(player1) return trader.ownerAccount(player1) }) .then((remains) => { logger(remains.toNumber()) return trader.ownerAccount(player2) }) .then((remains) => { logger(remains.toNumber()) }) }) })