From f97af2db3ee276c93bd736e2c9e1abfea6931019 Mon Sep 17 00:00:00 2001 From: Gas One Cent <86567384+gas1cent@users.noreply.github.com> Date: Tue, 16 Jan 2024 14:42:59 +0400 Subject: [PATCH] test: `validateNatspec` tests --- src/processor.ts | 1 - test/processor.test.ts | 93 ++++++++++++++++++++++++++---------------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/processor.ts b/src/processor.ts index 8aad49f..5d91a02 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -54,7 +54,6 @@ export class Processor { } validateNatspec(node: NodeToProcess): string[] { - if (!node) return []; const nodeNatspec = parseNodeNatspec(node); return this.validator.validate(node, nodeNatspec); } diff --git a/test/processor.test.ts b/test/processor.test.ts index 0e3b807..e6ab83e 100644 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -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; @@ -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}`); + }); + }); });