Skip to content

Commit

Permalink
Add BigNumber assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLefrere committed May 5, 2019
1 parent 0581946 commit 44e6062
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deployContract, accounts } from "./web3";
import { deployContract, accounts, isBigNumber } from "./web3";
import { ContractWithOverloads } from "./types/web3-contracts/ContractWithOverloads";

import { expect } from "chai";
Expand All @@ -7,8 +7,8 @@ describe("ContractWithOverloads", () => {
it("should work", async () => {
const contract = await deployContract<ContractWithOverloads>("ContractWithOverloads");

expect((await contract.methods.counter().call({ from: accounts[0] })).toString()).to.be.deep.eq(
"0",
);
const res = await contract.methods.counter().call({ from: accounts[0] });
expect(isBigNumber(res)).to.be.true;
expect(res.toString()).to.be.eq("0");
});
});
24 changes: 19 additions & 5 deletions test/integration/targets/web3-1.0.0/DumbContract.spec.web3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deployContract, accounts } from "./web3";
import { deployContract, accounts, isBigNumber } from "./web3";
import { DumbContract } from "./types/web3-contracts/DumbContract";

import { expect } from "chai";
Expand All @@ -8,6 +8,8 @@ describe("DumbContract", () => {
const contract: DumbContract = await deployContract<DumbContract>("DumbContract");

const res = await contract.methods.returnAll().call({ from: accounts[0] });
expect(isBigNumber(res[0])).to.be.true;
expect(isBigNumber(res[1])).to.be.true;
expect(res[0].toString()).to.be.eq("0");
expect(res[1].toString()).to.be.eq("5");
});
Expand All @@ -22,16 +24,26 @@ describe("DumbContract", () => {
const contract = await deployContract<DumbContract>("DumbContract");

await contract.methods.countup(2).send({ from: accounts[0] });
expect((await contract.methods.counter().call()).toString()).to.be.eq("2");
const withNumber = await contract.methods.counter().call();
expect(isBigNumber(withNumber)).to.be.true;
expect(withNumber.toString()).to.be.eq("2");

await contract.methods.countup("2").send({ from: accounts[0] });
expect((await contract.methods.counter().call()).toString()).to.be.eq("4");
const withString = await contract.methods.counter().call();
expect(isBigNumber(withString)).to.be.true;
expect(withString.toString()).to.be.eq("4");
});

it("should allow to pass signed values in multiple ways", async () => {
const contract = await deployContract<DumbContract>("DumbContract");

expect((await contract.methods.returnSigned(2).call()).toString()).to.be.eq("2");
expect((await contract.methods.returnSigned("2").call()).toString()).to.be.eq("2");
const withNumber = await contract.methods.returnSigned(2).call();
expect(isBigNumber(withNumber)).to.be.true;
expect(withNumber.toString()).to.be.eq("2");

const withString = await contract.methods.returnSigned("2").call();
expect(isBigNumber(withString)).to.be.true;
expect(withString.toString()).to.be.eq("2");
});

it("should allow to pass address values in multiple ways", async () => {
Expand Down Expand Up @@ -65,6 +77,8 @@ describe("DumbContract", () => {

const res = await contract.methods.callWithArray2(["1", 2]).call();
expect(res.length).to.be.eq(2);
expect(isBigNumber(res[0])).to.be.true;
expect(isBigNumber(res[1])).to.be.true;
expect(res[0].toString()).to.be.eq("1");
expect(res[1].toString()).to.be.eq("2");
});
Expand Down
6 changes: 6 additions & 0 deletions test/integration/targets/web3-1.0.0/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ export async function deployContract<T>(contractName: string): Promise<T> {
gas: GAS_LIMIT_STANDARD,
}) as any)) as T;
}

export function isBigNumber(object: any): boolean {
// Cribbed from web3-utils until BNs/BigNumbers are resolved in web3
// https://github.com/ethereum/web3.js/issues/2468
return object && object.constructor && object.constructor.name === "BigNumber";
}

0 comments on commit 44e6062

Please sign in to comment.