Skip to content

Commit

Permalink
Test findContract instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestognw committed Oct 20, 2023
1 parent 9155f51 commit 75cd6c8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 14 additions & 9 deletions packages/core/src/cli/validate/find-contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import test from 'ava';

import { isMatch } from './find-contract';
import { findContract } from './find-contract';

test('fully qualified - match', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'contracts/MyContract.sol:Foo',
};

t.true(isMatch('contracts/MyContract.sol:Foo', sourceContract));
t.deepEqual(findContract('MyContract.sol:Foo', sourceContract, [sourceContract]), sourceContract);
});

test('fully qualified without folder - match', async t => {
Expand All @@ -17,7 +17,7 @@ test('fully qualified without folder - match', async t => {
fullyQualifiedName: 'MyContract.sol:Foo',
};

t.true(isMatch('MyContract.sol:Foo', sourceContract));
t.deepEqual(findContract('MyContract.sol:Foo', sourceContract, [sourceContract]), sourceContract);
});

test('short name - match', async t => {
Expand All @@ -26,7 +26,7 @@ test('short name - match', async t => {
fullyQualifiedName: 'contracts/MyContract.sol:Foo',
};

t.true(isMatch('Foo', sourceContract));
t.deepEqual(findContract('Foo', sourceContract, [sourceContract]), sourceContract);
});

test('short name - match without folder', async t => {
Expand All @@ -35,7 +35,7 @@ test('short name - match without folder', async t => {
fullyQualifiedName: 'MyContract.sol:Foo',
};

t.true(isMatch('Foo', sourceContract));
t.deepEqual(findContract('Foo', sourceContract, [sourceContract]), sourceContract);
});

test('short name with .sol - no match', async t => {
Expand All @@ -44,8 +44,13 @@ test('short name with .sol - no match', async t => {
fullyQualifiedName: 'contracts/MyContract.sol:Foo',
};

t.false(isMatch('MyContract.sol', sourceContract));
t.false(isMatch('Foo.sol', sourceContract));
for (const name of ['MyContract.sol', 'Foo.sol']) {
const error = t.throws(() => findContract(name, sourceContract, [sourceContract]));
t.assert(
error?.message.includes(`Could not find contract ${name} referenced in ${sourceContract.fullyQualifiedName}`),
error?.message,
);
}
});

test('short name with .sol - match', async t => {
Expand All @@ -54,7 +59,7 @@ test('short name with .sol - match', async t => {
fullyQualifiedName: 'contracts/Foo.sol:Foo',
};

t.true(isMatch('Foo.sol', sourceContract));
t.deepEqual(findContract('Foo.sol', sourceContract, [sourceContract]), sourceContract);
});

test('short name with .sol - match without folder', async t => {
Expand All @@ -63,5 +68,5 @@ test('short name with .sol - match without folder', async t => {
fullyQualifiedName: 'Foo.sol:Foo',
};

t.true(isMatch('Foo.sol', sourceContract));
t.deepEqual(findContract('Foo.sol', sourceContract, [sourceContract]), sourceContract);
});
12 changes: 8 additions & 4 deletions packages/core/src/cli/validate/find-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export class ReferenceContractNotFound extends Error {
}
}

export function findContract(contractName: string, origin: SourceContract | undefined, allContracts: SourceContract[]) {
type SourceContractIdentifier = Pick<SourceContract, 'name' | 'fullyQualifiedName'>;

export function findContract<T extends SourceContractIdentifier>(
contractName: string,
origin: T | undefined,
allContracts: T[],
): T {
const foundContracts = allContracts.filter(c => isMatch(contractName, c));

if (foundContracts.length > 1) {
Expand All @@ -43,9 +49,7 @@ export function findContract(contractName: string, origin: SourceContract | unde
}
}

type SourceContractIdentifier = Pick<SourceContract, 'name' | 'fullyQualifiedName'>;

export function isMatch(contractName: string, contract: SourceContractIdentifier) {
function isMatch(contractName: string, contract: SourceContractIdentifier) {
return (
contract.fullyQualifiedName === contractName || // contracts/MyContract.sol:MyContract
contract.name === contractName || // MyContract
Expand Down

0 comments on commit 75cd6c8

Please sign in to comment.