Counters.test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. 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(), 'Counter: decrement 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. describe('reset', function () {
  53. context('null counter', function () {
  54. it('does not throw', async function () {
  55. await this.counter.reset();
  56. expect(await this.counter.current()).to.be.bignumber.equal('0');
  57. });
  58. });
  59. context('non null counter', function () {
  60. beforeEach(async function () {
  61. await this.counter.increment();
  62. expect(await this.counter.current()).to.be.bignumber.equal('1');
  63. });
  64. it('reset to 0', async function () {
  65. await this.counter.reset();
  66. expect(await this.counter.current()).to.be.bignumber.equal('0');
  67. });
  68. });
  69. });
  70. });