Counters.test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. it('increments the current value by one', async function () {
  14. await this.counter.increment();
  15. expect(await this.counter.current()).to.be.bignumber.equal('1');
  16. });
  17. it('can be called multiple times', async function () {
  18. await this.counter.increment();
  19. await this.counter.increment();
  20. await this.counter.increment();
  21. expect(await this.counter.current()).to.be.bignumber.equal('3');
  22. });
  23. });
  24. describe('decrement', function () {
  25. beforeEach(async function () {
  26. await this.counter.increment();
  27. expect(await this.counter.current()).to.be.bignumber.equal('1');
  28. });
  29. it('decrements the current value by one', async function () {
  30. await this.counter.decrement();
  31. expect(await this.counter.current()).to.be.bignumber.equal('0');
  32. });
  33. it('reverts if the current value is 0', async function () {
  34. await this.counter.decrement();
  35. await expectRevert(this.counter.decrement(), 'SafeMath: subtraction overflow');
  36. });
  37. it('can be called multiple times', async function () {
  38. await this.counter.increment();
  39. await this.counter.increment();
  40. expect(await this.counter.current()).to.be.bignumber.equal('3');
  41. await this.counter.decrement();
  42. await this.counter.decrement();
  43. await this.counter.decrement();
  44. expect(await this.counter.current()).to.be.bignumber.equal('0');
  45. });
  46. });
  47. });