Counters.test.js 1.8 KB

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