SafeERC20.test.js 3.1 KB

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