StorageSlot.test.js 2.99 KB
Newer Older
vicotor's avatar
vicotor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
const { constants, BN } = require('@openzeppelin/test-helpers');

const { expect } = require('chai');

const StorageSlotMock = artifacts.require('StorageSlotMock');

const slot = web3.utils.keccak256('some.storage.slot');
const otherSlot = web3.utils.keccak256('some.other.storage.slot');

contract('StorageSlot', function (accounts) {
  beforeEach(async function () {
    this.store = await StorageSlotMock.new();
  });

  describe('boolean storage slot', function () {
    beforeEach(async function () {
      this.value = true;
    });

    it('set', async function () {
      await this.store.setBoolean(slot, this.value);
    });

    describe('get', function () {
      beforeEach(async function () {
        await this.store.setBoolean(slot, this.value);
      });

      it('from right slot', async function () {
        expect(await this.store.getBoolean(slot)).to.be.equal(this.value);
      });

      it('from other slot', async function () {
        expect(await this.store.getBoolean(otherSlot)).to.be.equal(false);
      });
    });
  });

  describe('address storage slot', function () {
    beforeEach(async function () {
      this.value = accounts[1];
    });

    it('set', async function () {
      await this.store.setAddress(slot, this.value);
    });

    describe('get', function () {
      beforeEach(async function () {
        await this.store.setAddress(slot, this.value);
      });

      it('from right slot', async function () {
        expect(await this.store.getAddress(slot)).to.be.equal(this.value);
      });

      it('from other slot', async function () {
        expect(await this.store.getAddress(otherSlot)).to.be.equal(constants.ZERO_ADDRESS);
      });
    });
  });

  describe('bytes32 storage slot', function () {
    beforeEach(async function () {
      this.value = web3.utils.keccak256('some byte32 value');
    });

    it('set', async function () {
      await this.store.setBytes32(slot, this.value);
    });

    describe('get', function () {
      beforeEach(async function () {
        await this.store.setBytes32(slot, this.value);
      });

      it('from right slot', async function () {
        expect(await this.store.getBytes32(slot)).to.be.equal(this.value);
      });

      it('from other slot', async function () {
        expect(await this.store.getBytes32(otherSlot)).to.be.equal(constants.ZERO_BYTES32);
      });
    });
  });

  describe('uint256 storage slot', function () {
    beforeEach(async function () {
      this.value = new BN(1742);
    });

    it('set', async function () {
      await this.store.setUint256(slot, this.value);
    });

    describe('get', function () {
      beforeEach(async function () {
        await this.store.setUint256(slot, this.value);
      });

      it('from right slot', async function () {
        expect(await this.store.getUint256(slot)).to.be.bignumber.equal(this.value);
      });

      it('from other slot', async function () {
        expect(await this.store.getUint256(otherSlot)).to.be.bignumber.equal('0');
      });
    });
  });
});