Counters.test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const CountersImpl = contract.fromArtifact('CountersImpl');
  5. describe('Counters', function () {
  6. beforeEach(async function () {
  7. this.counter = await CountersImpl.new();
  8. });
  9. it('starts at zero', async function () {
  10. expect(await this.counter.current()).to.be.bignumber.equal('0');
  11. });
  12. describe('increment', function () {
  13. context('starting from 0', function () {
  14. it('increments the current value by one', async function () {
  15. await this.counter.increment();
  16. expect(await this.counter.current()).to.be.bignumber.equal('1');
  17. });
  18. it('can be called multiple times', async function () {
  19. await this.counter.increment();
  20. await this.counter.increment();
  21. await this.counter.increment();
  22. expect(await this.counter.current()).to.be.bignumber.equal('3');
  23. });
  24. });
  25. });
  26. describe('decrement', function () {
  27. beforeEach(async function () {
  28. await this.counter.increment();
  29. expect(await this.counter.current()).to.be.bignumber.equal('1');
  30. });
  31. context('starting from 1', function () {
  32. it('decrements the current value by one', async function () {
  33. await this.counter.decrement();
  34. expect(await this.counter.current()).to.be.bignumber.equal('0');
  35. });
  36. it('reverts if the current value is 0', async function () {
  37. await this.counter.decrement();
  38. await expectRevert(this.counter.decrement(), 'SafeMath: subtraction overflow');
  39. });
  40. });
  41. context('after incremented to 3', function () {
  42. it('can be called multiple times', async function () {
  43. await this.counter.increment();
  44. await this.counter.increment();
  45. expect(await this.counter.current()).to.be.bignumber.equal('3');
  46. await this.counter.decrement();
  47. await this.counter.decrement();
  48. await this.counter.decrement();
  49. expect(await this.counter.current()).to.be.bignumber.equal('0');
  50. });
  51. });
  52. });
  53. });