SafeERC20.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const shouldFail = require('../../helpers/shouldFail');
  2. require('../../helpers/setup');
  3. const SafeERC20Helper = artifacts.require('SafeERC20Helper');
  4. contract('SafeERC20', function () {
  5. beforeEach(async function () {
  6. this.helper = await SafeERC20Helper.new();
  7. });
  8. describe('with token that returns false on all calls', function () {
  9. it('reverts on transfer', async function () {
  10. await shouldFail.reverting(this.helper.doFailingTransfer());
  11. });
  12. it('reverts on transferFrom', async function () {
  13. await shouldFail.reverting(this.helper.doFailingTransferFrom());
  14. });
  15. it('reverts on approve', async function () {
  16. await shouldFail.reverting(this.helper.doFailingApprove());
  17. });
  18. it('reverts on increaseAllowance', async function () {
  19. await shouldFail.reverting(this.helper.doFailingIncreaseAllowance());
  20. });
  21. it('reverts on decreaseAllowance', async function () {
  22. await shouldFail.reverting(this.helper.doFailingDecreaseAllowance());
  23. });
  24. });
  25. describe('with token that returns true on all calls', function () {
  26. it('doesn\'t revert on transfer', async function () {
  27. await this.helper.doSucceedingTransfer();
  28. });
  29. it('doesn\'t revert on transferFrom', async function () {
  30. await this.helper.doSucceedingTransferFrom();
  31. });
  32. describe('approvals', function () {
  33. context('with zero allowance', function () {
  34. beforeEach(async function () {
  35. await this.helper.setAllowance(0);
  36. });
  37. it('doesn\'t revert when approving a non-zero allowance', async function () {
  38. await this.helper.doSucceedingApprove(100);
  39. });
  40. it('doesn\'t revert when approving a zero allowance', async function () {
  41. await this.helper.doSucceedingApprove(0);
  42. });
  43. it('doesn\'t revert when increasing the allowance', async function () {
  44. await this.helper.doSucceedingIncreaseAllowance(10);
  45. });
  46. it('reverts when decreasing the allowance', async function () {
  47. await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(10));
  48. });
  49. });
  50. context('with non-zero allowance', function () {
  51. beforeEach(async function () {
  52. await this.helper.setAllowance(100);
  53. });
  54. it('reverts when approving a non-zero allowance', async function () {
  55. await shouldFail.reverting(this.helper.doSucceedingApprove(20));
  56. });
  57. it('doesn\'t revert when approving a zero allowance', async function () {
  58. await this.helper.doSucceedingApprove(0);
  59. });
  60. it('doesn\'t revert when increasing the allowance', async function () {
  61. await this.helper.doSucceedingIncreaseAllowance(10);
  62. });
  63. it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
  64. await this.helper.doSucceedingDecreaseAllowance(50);
  65. });
  66. it('reverts when decreasing the allowance to a negative value', async function () {
  67. await shouldFail.reverting(this.helper.doSucceedingDecreaseAllowance(200));
  68. });
  69. });
  70. });
  71. });
  72. });