send.test.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const send = require('../send');
  2. const shouldFail = require('../shouldFail');
  3. const expectEvent = require('../expectEvent');
  4. const { ether } = require('../ether');
  5. const { ethGetBalance } = require('../web3');
  6. const Acknowledger = artifacts.require('Acknowledger');
  7. require('../setup');
  8. contract('send', function ([sender, receiver]) {
  9. describe('ether', function () {
  10. it('sends ether with no gas cost', async function () {
  11. const value = ether(1);
  12. const initialSenderBalance = await ethGetBalance(sender);
  13. const initialReceiverBalance = await ethGetBalance(receiver);
  14. await send.ether(sender, receiver, value);
  15. const finalSenderBalance = await ethGetBalance(sender);
  16. const finalReceiverBalance = await ethGetBalance(receiver);
  17. finalSenderBalance.sub(initialSenderBalance).should.be.bignumber.equal(-value);
  18. finalReceiverBalance.sub(initialReceiverBalance).should.be.bignumber.equal(value);
  19. });
  20. it('throws if the sender balance is insufficient', async function () {
  21. const value = (await ethGetBalance(sender)).plus(1);
  22. await shouldFail(send.ether(sender, receiver, value));
  23. });
  24. });
  25. describe('transaction', function () {
  26. beforeEach(async function () {
  27. this.acknowledger = await Acknowledger.new();
  28. });
  29. it('calls a function from its signature ', async function () {
  30. const { logs } = await send.transaction(this.acknowledger, 'foo', 'uint256', [3]);
  31. expectEvent.inLogs(logs, 'AcknowledgeFoo', { a: 3 });
  32. });
  33. it('calls overloaded functions with less arguments', async function () {
  34. const { logs } = await send.transaction(this.acknowledger, 'bar', 'uint256', [3]);
  35. expectEvent.inLogs(logs, 'AcknowledgeBarSingle', { a: 3 });
  36. });
  37. it('calls overloaded functions with more arguments', async function () {
  38. const { logs } = await send.transaction(this.acknowledger, 'bar', 'uint256,uint256', [3, 5]);
  39. expectEvent.inLogs(logs, 'AcknowledgeBarDouble', { a: 3, b: 5 });
  40. });
  41. it('throws if the number of arguments does not match', async function () {
  42. await shouldFail(send.transaction(this.acknowledger, 'foo', 'uint256, uint256', [3, 5]));
  43. });
  44. it('throws if the method does not exist', async function () {
  45. await shouldFail(send.transaction(this.acknowledger, 'baz', 'uint256', [3]));
  46. });
  47. it('throws if there is a mismatch in the number of types and values', async function () {
  48. await shouldFail(send.transaction(this.acknowledger, 'foo', 'uint256', [3, 3]));
  49. });
  50. });
  51. });