123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /* eslint no-undef: "off" */
- const logger = console.log.bind(console) // eslint-disable-line no-console
- function logEvents(result, name) {
- 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!')
- })
- })
- })
|