test-contract.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* eslint no-undef: "off" */
  2. const logger = console.log.bind(console) // eslint-disable-line no-console
  3. function logEvents(result, name) {
  4. result.logs.forEach((log) => {
  5. if (log.event === name) {
  6. logger(log)
  7. }
  8. })
  9. }
  10. function assertEventInResult(result, name) {
  11. let isIn = false
  12. result.logs.forEach((log) => {
  13. if (log.event === name) {
  14. isIn = true
  15. }
  16. })
  17. assert.equal(isIn, true, `Event ${name} should be in the result!`)
  18. }
  19. const ShowTrader = artifacts.require('ShowTrader')
  20. contract('ShowTrader Test', (accounts) => {
  21. let trader
  22. const danceData = '0x111000'
  23. const musicData = '0x000111'
  24. it('should create 1 show', () => {
  25. const player = accounts[0]
  26. const showName = '1st show'
  27. return ShowTrader.deployed()
  28. .then((instance) => {
  29. trader = instance
  30. return trader.createFirstShow(showName, danceData, musicData, { from: player })
  31. })
  32. .then((result) => {
  33. // logEvents(result, 'NewShow')
  34. assertEventInResult(result, 'NewShow')
  35. return trader.getShowsByOwner.call(player)
  36. })
  37. .then((shows) => {
  38. const id = shows[0].toNumber()
  39. return trader.shows(id)
  40. })
  41. .then((show) => {
  42. assert.equal(show[0], showName)
  43. assert.equal(show[1], danceData)
  44. assert.equal(show[2], musicData)
  45. })
  46. })
  47. // The second test begin...
  48. it('shoud exchange 2 shows', () => {
  49. const name1 = 'show1'
  50. const name2 = 'show2'
  51. const player1 = accounts[1] // The account[0] is used for the 1st test
  52. // Note: 'VM Exception while processing transaction: revert',
  53. // usually means that the require() statement is not met
  54. const player2 = accounts[2]
  55. let id1
  56. let id2
  57. return ShowTrader.deployed()
  58. .then((instance) => {
  59. trader = instance
  60. return trader.createFirstShow(name1, danceData, musicData, { from: player1 })
  61. })
  62. .then((result) => {
  63. assertEventInResult(result, 'NewShow')
  64. return trader.createFirstShow(name2, danceData, musicData, { from: player2 })
  65. })
  66. .then((result) => {
  67. assertEventInResult(result, 'NewShow')
  68. return trader.getShowsByOwner.call(player1)
  69. })
  70. .then((shows1) => {
  71. id1 = shows1[0].toNumber()
  72. return trader.getShowsByOwner.call(player2)
  73. })
  74. .then((shows2) => {
  75. id2 = shows2[0].toNumber()
  76. return trader.wantToExchange(id1, id2, { from: player1 })
  77. })
  78. .then((result) => {
  79. // logEvents(result, 'WantToExchange')
  80. assertEventInResult(result, 'WantToExchange')
  81. return trader.wantToExchange(id2, id1, { from: player2 })
  82. })
  83. .then((result) => {
  84. // logEvents(result, 'Transfer')
  85. assertEventInResult(result, 'Transfer')
  86. return trader.showToOwner(id1)
  87. })
  88. .then((owner1) => {
  89. assert.equal(owner1, player2, 'owner of show1 should be player2!')
  90. return trader.showToOwner(id2)
  91. })
  92. .then((owner2) => {
  93. assert.equal(owner2, player1, 'owner of show2 should be player1!')
  94. })
  95. })
  96. })