Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quezak committed Jul 11, 2019
1 parent af0270f commit b5866ea
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 6 deletions.
13 changes: 13 additions & 0 deletions __snapshots__/DumbContract.spec.ts.js
Expand Up @@ -175,6 +175,15 @@ export class DumbContract extends TC.TypeChainContract {
stateMutability: "view",
type: "function",
},
{
constant: true,
inputs: [],
name: "testVoidReturn",
outputs: [],
payable: false,
stateMutability: "pure",
type: "function",
},
{
constant: false,
inputs: [{ name: "dynamicBytes", type: "bytes" }],
Expand Down Expand Up @@ -310,6 +319,10 @@ export class DumbContract extends TC.TypeChainContract {
return TC.promisify(this.rawWeb3Contract.counterWithOffset, [offset.toString()]);
}
public testVoidReturn(): Promise<void> {
return TC.promisify(this.rawWeb3Contract.testVoidReturn, []);
}
public countupForEtherTx(): TC.DeferredTransactionWrapper<TC.IPayableTxParams> {
return new TC.DeferredTransactionWrapper<TC.IPayableTxParams>(this, "countupForEther", []);
}
Expand Down
62 changes: 62 additions & 0 deletions lib/targets/ethers/generation.spec.ts
@@ -0,0 +1,62 @@
import { expect } from "chai";

import { Contract } from "../../parser/abiParser";
import { EvmType } from "../../parser/typeParser";
import { codegenContractFactory, codegenContractTypings } from "./generation";

class FakeEvmType extends EvmType {}

describe("Ethers generation edge cases", () => {
const emptyContract: Contract = {
name: "TestContract",
constantFunctions: [],
constants: [],
functions: [],
events: [],
constructor: { inputs: [], payable: false },
};

it("should throw on invalid function input type", () => {
const contract: Contract = {
...emptyContract,
constantFunctions: [
{
name: "testFunction",
inputs: [
{
name: "testInput",
type: new FakeEvmType(),
},
],
outputs: [],
},
],
};
expect(() => codegenContractTypings(contract)).to.throw("Unrecognized type FakeEvmType");
});

it("should throw on invalid function output type", () => {
const contract: Contract = {
...emptyContract,
constantFunctions: [
{
name: "testFunction",
inputs: [],
outputs: [
{
name: "testOutput",
type: new FakeEvmType(),
},
],
},
],
};
expect(() => codegenContractTypings(contract)).to.throw("Unrecognized type FakeEvmType");
});

it("should generate simple factory when no bytecode available", () => {
expect(codegenContractFactory(emptyContract, "abi", "")).to.match(
/export class TestContractFactory \{/,
);
});
});
4 changes: 2 additions & 2 deletions lib/targets/ethers/generation.ts
Expand Up @@ -262,7 +262,7 @@ function generateInputType(evmType: EvmType): string {
return generateTupleType(evmType as TupleType, generateInputType);

default:
throw new Error(`Unrecognized type ${evmType}`);
throw new Error(`Unrecognized type ${evmType.constructor.name}`);
}
}

Expand All @@ -289,7 +289,7 @@ function generateOutputType(evmType: EvmType): string {
return generateTupleType(evmType as TupleType, generateOutputType);

default:
throw new Error(`Unrecognized type ${evmType}`);
throw new Error(`Unrecognized type ${evmType.constructor.name}`);
}
}

Expand Down
2 changes: 2 additions & 0 deletions test/integration/contracts/DumbContract.sol
Expand Up @@ -91,6 +91,8 @@ contract DumbContract {
return a;
}

function testVoidReturn() pure public {}

event Deposit(
address indexed from,
uint value
Expand Down
22 changes: 18 additions & 4 deletions test/integration/targets/ethers/DumbContract.spec.ts
@@ -1,11 +1,11 @@
import { DumbContract } from "./types/ethers-contracts/DumbContract";
import { DumbContractFactory } from "./types/ethers-contracts/DumbContractFactory";
import { BigNumber } from "ethers/utils";

import { expect } from "chai";
import { Event } from "ethers";
import { BigNumber } from "ethers/utils";
import { arrayify } from "ethers/utils/bytes";

import { getTestSigner } from "./ethers";
import { DumbContract } from "./types/ethers-contracts/DumbContract";
import { DumbContractFactory } from "./types/ethers-contracts/DumbContractFactory";

describe("DumbContract", () => {
function deployDumbContract(): Promise<DumbContract> {
Expand Down Expand Up @@ -36,6 +36,20 @@ describe("DumbContract", () => {
expect(await contract3.functions.counter()).to.be.deep.eq(new BigNumber("5678567856785678567"));
});

it("should allow connecting to an existing contract instance with signer or provider", async () => {
const contract1 = await new DumbContractFactory(getTestSigner()).deploy(42);
const contract2 = DumbContractFactory.connect(
contract1.address,
getTestSigner(),
);
expect(await contract2.functions.counter()).to.be.deep.eq(new BigNumber("42"));
const contract3 = DumbContractFactory.connect(
contract1.address,
getTestSigner().provider!,
);
expect(await contract3.functions.counter()).to.be.deep.eq(new BigNumber("42"));
});

it("should allow to pass unsigned values in multiple ways", async () => {
const contract = await deployDumbContract();

Expand Down

0 comments on commit b5866ea

Please sign in to comment.