SafeERC20.test.js 3.1 KB

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