Counters.test.js 1.8 KB

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