1
0

test-contract.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* eslint no-undef: "off" */
  2. const logger = console.log.bind(console) // eslint-disable-line no-console
  3. function logEvents(result, name) { // eslint-disable-line no-unused-vars
  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. it('should buy & sell', () => {
  97. const player1 = accounts[1]
  98. const player2 = accounts[2]
  99. let id1
  100. return ShowTrader.deployed()
  101. .then((instance) => {
  102. trader = instance
  103. return trader.getShowsByOwner.call(player1)
  104. })
  105. .then((shows1) => {
  106. id1 = shows1[0].toNumber()
  107. logger(id1)
  108. return trader.initAccount({ from: player1 })
  109. })
  110. .then(() => {
  111. logger()
  112. return trader.ownerAccount(player1)
  113. })
  114. .then((remains) => {
  115. logger(remains.toNumber())
  116. return trader.initAccount({ from: player2 })
  117. })
  118. .then(() => {
  119. logger()
  120. return trader.ownerAccount(player2)
  121. })
  122. .then((remains) => {
  123. logger(remains.toNumber())
  124. return trader.sell(id1, 233, { from: player1 })
  125. })
  126. .then(() => {
  127. logger()
  128. return trader.showToPrice(id1)
  129. })
  130. .then((price) => {
  131. logger(price.toNumber())
  132. return trader.buy(id1, { from: player2 })
  133. })
  134. .then(() => {
  135. logger()
  136. return trader.showToOwner(id1)
  137. })
  138. .then((owner) => {
  139. logger(owner)
  140. logger(player1)
  141. return trader.ownerAccount(player1)
  142. })
  143. .then((remains) => {
  144. logger(remains.toNumber())
  145. return trader.ownerAccount(player2)
  146. })
  147. .then((remains) => {
  148. logger(remains.toNumber())
  149. })
  150. })
  151. })