Skip to content

Commit

Permalink
refactor: findNode helper
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Feb 15, 2024
1 parent b0020d6 commit 21c0c84
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 51 deletions.
46 changes: 23 additions & 23 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ContractDefinition } from 'solc-typed-ast';
import { parseNodeNatspec } from '../src/utils';
import { getFileCompiledSource } from './utils/helpers';
import { getFileCompiledSource, findNode } from './utils/helpers';
import { mockNatspec } from './utils/mocks';

describe('Parser', () => {
Expand All @@ -13,7 +13,7 @@ describe('Parser', () => {
});

it('should parse the inheritdoc tag', async () => {
const node = contract.vFunctions.find(({ name }) => name === 'viewFunctionNoParams')!;
const node = findNode(contract.vFunctions, 'viewFunctionNoParams');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -30,7 +30,7 @@ describe('Parser', () => {
});

it('should parse constant', async () => {
const node = contract.vStateVariables.find(({ name }) => name === 'SOME_CONSTANT')!;
const node = findNode(contract.vStateVariables, 'SOME_CONSTANT');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -43,7 +43,7 @@ describe('Parser', () => {
});

it('should parse variable', async () => {
const node = contract.vStateVariables.find(({ name }) => name === 'someVariable')!;
const node = findNode(contract.vStateVariables, 'someVariable');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -56,7 +56,7 @@ describe('Parser', () => {
});

it('should parse modifier', async () => {
const node = contract.vModifiers.find(({ name }) => name === 'someModifier')!;
const node = findNode(contract.vModifiers, 'someModifier');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -78,7 +78,7 @@ describe('Parser', () => {
});

it('should parse external function', async () => {
const node = contract.vFunctions.find(({ name }) => name === 'viewFunctionNoParams')!;
const node = findNode(contract.vFunctions, 'viewFunctionNoParams');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -97,7 +97,7 @@ describe('Parser', () => {
});

it('should parse private function', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewPrivate')!;
const node = findNode(contract.vFunctions, '_viewPrivate');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('Parser', () => {
});

it('should parse multiline descriptions', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewMultiline')!;
const node = findNode(contract.vFunctions, '_viewMultiline');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -145,7 +145,7 @@ describe('Parser', () => {
});

it('should parse multiple of the same tag', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewDuplicateTag')!;
const node = findNode(contract.vFunctions, '_viewDuplicateTag');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -172,7 +172,7 @@ describe('Parser', () => {
});

it('should parse error', async () => {
const node = contract.vErrors.find(({ name }) => name === 'SimpleError')!;
const node = findNode(contract.vErrors, 'SimpleError');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -188,7 +188,7 @@ describe('Parser', () => {
});

it('should parse event', async () => {
const node = contract.vEvents.find(({ name }) => name === 'SimpleEvent')!;
const node = findNode(contract.vEvents, 'SimpleEvent');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -204,7 +204,7 @@ describe('Parser', () => {
});

it('should parse struct', async () => {
const node = contract.vStructs.find(({ name }) => name === 'SimplestStruct')!;
const node = findNode(contract.vStructs, 'SimplestStruct');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('Parser', () => {
});

it('should parse external function without parameters', async () => {
const node = contract.vFunctions.find(({ name }) => name === 'viewFunctionNoParams')!;
const node = findNode(contract.vFunctions, 'viewFunctionNoParams');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -258,7 +258,7 @@ describe('Parser', () => {
});

it('should parse external function with parameters', async () => {
const node = contract.vFunctions.find(({ name }) => name === 'viewFunctionWithParams')!;
const node = findNode(contract.vFunctions, 'viewFunctionWithParams');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand Down Expand Up @@ -297,7 +297,7 @@ describe('Parser', () => {
});

it('should parse struct', async () => {
const node = contract.vStructs.find(({ name }) => name === 'SimpleStruct')!;
const node = findNode(contract.vStructs, 'SimpleStruct');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -308,7 +308,7 @@ describe('Parser', () => {
});

it('should parse inheritdoc + natspec', async () => {
const node = contract.vStateVariables.find(({ name }) => name === 'someVariable')!;
const node = findNode(contract.vStateVariables, 'someVariable');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -327,21 +327,21 @@ describe('Parser', () => {
});

it('should not parse the inheritdoc tag with just 2 slashes', async () => {
const node = contract.vStateVariables.find(({ name }) => name === 'SOME_CONSTANT')!;
const node = findNode(contract.vStateVariables, 'SOME_CONSTANT');
const result = parseNodeNatspec(node);

expect(result).toEqual(mockNatspec({}));
});

it('should not parse regular comments as natspec', async () => {
const node = contract.vFunctions.find(({ name }) => name === 'viewFunctionWithParams')!;
const node = findNode(contract.vFunctions, 'viewFunctionWithParams');
const result = parseNodeNatspec(node);

expect(result).toEqual(mockNatspec({}));
});

it('should parse natspec with multiple spaces', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewPrivate')!;
const node = findNode(contract.vFunctions, '_viewPrivate');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand Down Expand Up @@ -369,14 +369,14 @@ describe('Parser', () => {
});

it('should not parse natspec with invalid number of slashes', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewInternal')!;
const node = findNode(contract.vFunctions, '_viewInternal');
const result = parseNodeNatspec(node);

expect(result).toEqual(mockNatspec({}));
});

it('should parse block natspec with invalid formatting', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewBlockLinterFail')!;
const node = findNode(contract.vFunctions, '_viewBlockLinterFail');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -392,7 +392,7 @@ describe('Parser', () => {
});

it('should parse block natspec with invalid formatting', async () => {
const node = contract.vFunctions.find(({ name }) => name === '_viewLinterFail')!;
const node = findNode(contract.vFunctions, '_viewLinterFail');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand All @@ -412,7 +412,7 @@ describe('Parser', () => {
});

it('should correctly parse empty return tag', async () => {
const node = contract.vFunctions.find(({ name }) => name === 'functionUnnamedEmptyReturn')!;
const node = findNode(contract.vFunctions, 'functionUnnamedEmptyReturn');
const result = parseNodeNatspec(node);

expect(result).toEqual(
Expand Down
5 changes: 5 additions & 0 deletions test/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ASTKind, ASTReader, SourceUnit, compileSol } from 'solc-typed-ast';
import { NodeToProcess } from '../../src/types';

export async function getFileCompiledSource(filePath: string): Promise<SourceUnit> {
const compiledFile = await compileSol(filePath, 'auto');
Expand All @@ -9,3 +10,7 @@ export function expectWarning(warnArray: string[], expectedWarn: string, numberO
expect(warnArray).toContain(expectedWarn);
expect(warnArray.filter((x) => x == expectedWarn).length).toBe(numberOfWarnings);
}

export function findNode(nodes: readonly NodeToProcess[], name: string): any {
return nodes.find((x) => x.name === name);
}
Loading

0 comments on commit 21c0c84

Please sign in to comment.