Counters.test.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const { expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const CountersImpl = artifacts.require('CountersImpl');
  4. contract('Counters', function (accounts) {
  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. context('starting from 0', 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. });
  25. describe('decrement', function () {
  26. beforeEach(async function () {
  27. await this.counter.increment();
  28. expect(await this.counter.current()).to.be.bignumber.equal('1');
  29. });
  30. context('starting from 1', function () {
  31. it('decrements the current value by one', async function () {
  32. await this.counter.decrement();
  33. expect(await this.counter.current()).to.be.bignumber.equal('0');
  34. });
  35. it('reverts if the current value is 0', async function () {
  36. await this.counter.decrement();
  37. await expectRevert(this.counter.decrement(), 'SafeMath: subtraction overflow');
  38. });
  39. });
  40. context('after incremented to 3', function () {
  41. it('can be called multiple times', async function () {
  42. await this.counter.increment();
  43. await this.counter.increment();
  44. expect(await this.counter.current()).to.be.bignumber.equal('3');
  45. await this.counter.decrement();
  46. await this.counter.decrement();
  47. await this.counter.decrement();
  48. expect(await this.counter.current()).to.be.bignumber.equal('0');
  49. });
  50. });
  51. });
  52. });