|
| 1 | +import "module-alias/register"; |
| 2 | +import { BigNumber } from "ethers"; |
| 3 | +import { solidityPack } from "ethers/lib/utils"; |
| 4 | + |
| 5 | +import { Address, Bytes } from "@utils/types"; |
| 6 | +import { Account } from "@utils/test/types"; |
| 7 | +import { MAX_UINT_256 } from "@utils/constants"; |
| 8 | +import { BytesArrayUtilsMock } from "@utils/contracts"; |
| 9 | +import DeployHelper from "@utils/deploys"; |
| 10 | +import { |
| 11 | + addSnapshotBeforeRestoreAfterEach, |
| 12 | + getAccounts, |
| 13 | + getWaffleExpect, |
| 14 | + getRandomAddress |
| 15 | +} from "@utils/test/index"; |
| 16 | + |
| 17 | +const expect = getWaffleExpect(); |
| 18 | + |
| 19 | +describe("BytesArrayUtils", () => { |
| 20 | + let owner: Account; |
| 21 | + let deployer: DeployHelper; |
| 22 | + |
| 23 | + let bytesArrayUtils: BytesArrayUtilsMock; |
| 24 | + |
| 25 | + |
| 26 | + before(async () => { |
| 27 | + [ |
| 28 | + owner, |
| 29 | + ] = await getAccounts(); |
| 30 | + |
| 31 | + deployer = new DeployHelper(owner.wallet); |
| 32 | + bytesArrayUtils = await deployer.mocks.deployBytesArrayUtilsMock(); |
| 33 | + }); |
| 34 | + |
| 35 | + addSnapshotBeforeRestoreAfterEach(); |
| 36 | + |
| 37 | + describe("#toBool", async () => { |
| 38 | + let bool: boolean; |
| 39 | + let randomAddress: Address; |
| 40 | + |
| 41 | + let subjectBytes: Bytes; |
| 42 | + let subjectStart: BigNumber; |
| 43 | + |
| 44 | + before(async () => { |
| 45 | + randomAddress = await getRandomAddress(); |
| 46 | + }); |
| 47 | + |
| 48 | + beforeEach(async() => { |
| 49 | + bool = true; |
| 50 | + |
| 51 | + subjectBytes = solidityPack( |
| 52 | + ["address", "bool"], |
| 53 | + [randomAddress, bool] |
| 54 | + ); |
| 55 | + subjectStart = BigNumber.from(20); // Address is 20 bytes long |
| 56 | + }); |
| 57 | + |
| 58 | + async function subject(): Promise<boolean> { |
| 59 | + return await bytesArrayUtils.testToBool(subjectBytes, subjectStart); |
| 60 | + } |
| 61 | + |
| 62 | + it("should return correct bool", async () => { |
| 63 | + const actualBool = await subject(); |
| 64 | + |
| 65 | + expect(actualBool).to.eq(bool); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("when bool is false", async () => { |
| 69 | + beforeEach(async() => { |
| 70 | + bool = false; |
| 71 | + |
| 72 | + subjectBytes = solidityPack( |
| 73 | + ["address", "bool"], |
| 74 | + [randomAddress, bool] |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should return correct bool", async () => { |
| 79 | + const actualBool = await subject(); |
| 80 | + |
| 81 | + expect(actualBool).to.eq(bool); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("when start is max uint 256", async () => { |
| 86 | + beforeEach(() => { |
| 87 | + subjectStart = MAX_UINT_256; |
| 88 | + }); |
| 89 | + |
| 90 | + it("should revert", async () => { |
| 91 | + await expect(subject()).to.be.revertedWith("toBool_overflow"); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + |
| 96 | + describe("when start is out of bounds", async () => { |
| 97 | + beforeEach(() => { |
| 98 | + subjectStart = BigNumber.from(subjectBytes.length); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should revert", async () => { |
| 102 | + await expect(subject()).to.be.revertedWith("toBool_outOfBounds"); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments