Skip to content

Commit

Permalink
test: validateNatspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Jan 16, 2024
1 parent 5c52b33 commit f97af2d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 36 deletions.
1 change: 0 additions & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export class Processor {
}

validateNatspec(node: NodeToProcess): string[] {
if (!node) return [];
const nodeNatspec = parseNodeNatspec(node);
return this.validator.validate(node, nodeNatspec);
}
Expand Down
93 changes: 58 additions & 35 deletions test/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,12 @@ import * as utils from '../src/utils';
import { Processor } from '../src/processor';
import { Validator } from '../src/validator';
import { getFileCompiledSource } from './utils';
import { mockFunctionDefinition, mockNodeToProcess, mockConfig } from './mocks';
import { mockFunctionDefinition, mockNodeToProcess, mockConfig, mockNatspec } from './mocks';

describe('Processor', () => {
const validator = new Validator(mockConfig({}));
const processor = new Processor(validator);

describe('formatLocation', () => {
const absolutePath = faker.system.filePath();
const contractName = faker.lorem.word();
const nodeName = faker.lorem.word();
const fileContent = faker.lorem.sentence();
const lineNumber = faker.number.int(100);
const src = '${lineNumber}:1:0';
const getLineNumberFromSrcSpy = jest.spyOn(utils, 'getLineNumberFromSrc').mockImplementation(() => lineNumber);

it('should format the location of a node', () => {
const node = mockNodeToProcess({ name: nodeName, src: src });
const formattedLocation = processor.formatLocation(absolutePath, fileContent, contractName, node);

expect(getLineNumberFromSrcSpy).toHaveBeenCalledWith(fileContent, src);
expect(formattedLocation).toEqual(`${absolutePath}:${lineNumber}\n${contractName}:${nodeName}`);
});

it('should format the location of a constructor', () => {
const node = mockFunctionDefinition({ kind: FunctionKind.Constructor, src: src });
const formattedLocation = processor.formatLocation(absolutePath, fileContent, contractName, node);

expect(getLineNumberFromSrcSpy).toHaveBeenCalledWith(fileContent, src);
expect(formattedLocation).toEqual(`${absolutePath}:${lineNumber}\n${contractName}:constructor`);
});

it('should format the location of a function', () => {
const node = mockFunctionDefinition({ name: nodeName, src: src });
const formattedLocation = processor.formatLocation(absolutePath, fileContent, contractName, node);

expect(getLineNumberFromSrcSpy).toHaveBeenCalledWith(fileContent, src);
expect(formattedLocation).toEqual(`${absolutePath}:${lineNumber}\n${contractName}:${nodeName}`);
});
});

describe('selectEligibleNodes', () => {
let contract: ContractDefinition;

Expand Down Expand Up @@ -123,4 +89,61 @@ describe('Processor', () => {
expect(eligibleNodes.filter((node) => node instanceof FunctionDefinition && node.isConstructor).length).toEqual(1);
});
});

describe('validateNatspec', () => {
const node = mockNodeToProcess({});
const nodeNatspec = mockNatspec({});

it('should parse the natspec of the node', () => {
const parseNodeNatspecSpy = jest.spyOn(utils, 'parseNodeNatspec').mockImplementation((_) => nodeNatspec);

processor.validateNatspec(node);

expect(parseNodeNatspecSpy).toHaveBeenCalledWith(node);
});

it('should validate the natspec of the node', () => {
const messages = [faker.lorem.sentence(), faker.lorem.sentence()];
const validateSpy = jest.spyOn(validator, 'validate').mockImplementation((_, __) => messages);

const validatedNatspec = processor.validateNatspec(node);

expect(validatedNatspec).toEqual(messages);
expect(validateSpy).toHaveBeenCalledWith(node, nodeNatspec);
});
});

describe('formatLocation', () => {
const absolutePath = faker.system.filePath();
const contractName = faker.lorem.word();
const nodeName = faker.lorem.word();
const fileContent = faker.lorem.sentence();
const lineNumber = faker.number.int(100);
const src = '${lineNumber}:1:0';
const getLineNumberFromSrcSpy = jest.spyOn(utils, 'getLineNumberFromSrc').mockImplementation(() => lineNumber);

it('should format the location of a node', () => {
const node = mockNodeToProcess({ name: nodeName, src: src });
const formattedLocation = processor.formatLocation(absolutePath, fileContent, contractName, node);

expect(getLineNumberFromSrcSpy).toHaveBeenCalledWith(fileContent, src);
expect(formattedLocation).toEqual(`${absolutePath}:${lineNumber}\n${contractName}:${nodeName}`);
});

it('should format the location of a constructor', () => {
const node = mockFunctionDefinition({ kind: FunctionKind.Constructor, src: src });
const formattedLocation = processor.formatLocation(absolutePath, fileContent, contractName, node);

expect(getLineNumberFromSrcSpy).toHaveBeenCalledWith(fileContent, src);
expect(formattedLocation).toEqual(`${absolutePath}:${lineNumber}\n${contractName}:constructor`);
});

it('should format the location of a function', () => {
const node = mockFunctionDefinition({ name: nodeName, src: src });
const formattedLocation = processor.formatLocation(absolutePath, fileContent, contractName, node);

expect(getLineNumberFromSrcSpy).toHaveBeenCalledWith(fileContent, src);
expect(formattedLocation).toEqual(`${absolutePath}:${lineNumber}\n${contractName}:${nodeName}`);
});
});
});

0 comments on commit f97af2d

Please sign in to comment.