Skip to content

Commit

Permalink
test: complete parser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Jan 12, 2024
1 parent 4ae7787 commit bbb022e
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 42 deletions.
51 changes: 15 additions & 36 deletions sample-data/ParserTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ interface IParserTest {
*/
function viewFunctionWithParams(uint256 _param1, uint256 _param2) external view returns (uint256);


// @notice Forgot one slash but it's sill natspec
//// @dev Too many slashes are fine too
/// @return Huh?
/// @notice A state variable
/// @return Some value
function someVariable() external view returns (uint256);


/// @notice A struct holding 2 variables of type uint256
/// @member a The first variable
/// @member b The second variable
Expand All @@ -47,15 +44,13 @@ interface IParserTest {
uint256 b;
}

/// @notice Linter fail
/// @return _returned What's being returned
function SOME_CONSTANT() external view returns (uint256 _returned);
/// @notice A constant of type uint256
function SOME_CONSTANT() external view returns (uint256 _returned);
}

/// @notice A contract with correct natspec
contract ParserTest is IParserTest {
/// @inheritdoc IParserTest
/// @dev Providing context
uint256 public someVariable;

/// @inheritdoc IParserTest
Expand Down Expand Up @@ -138,7 +133,6 @@ contract ParserTestFunny is IParserTest {
uint256 public constant SOME_CONSTANT = 123;

/// @inheritdoc IParserTest
/// @dev Why does it have a comment here?
function viewFunctionNoParams() external view returns (uint256){
return 1;
}
Expand All @@ -161,44 +155,29 @@ contract ParserTestFunny is IParserTest {
* She's cool
*/

/// @notice Some private stuff
/// @dev Dev comment for the private function
/// @notice Some private stuff
/// @param _paramName The parameter name
/// @return _returned The returned value
/// @return _returned The returned value
function _viewPrivate(uint256 _paramName) private pure returns (uint256 _returned) {
return 1;
}

/// @notice Some internal stuff
/// @dev Dev comment for the internal function
/// @param _paramName The parameter name
/// @return _returned The returned value
// @notice Forgot one slash and it's not natspec anymore
//// @dev Too many slashes is fine though
//// @return _returned The returned value
function _viewInternal(uint256 _paramName) internal pure returns (uint256 _returned) {
return 1;
}

// Random comment
/// @notice Some internal stuff
/// Separate line
/// Third one
function _viewMultiline() internal pure {
/**** @notice Some text
** */
function _viewBlockLinterFail() internal pure {
}

/// @notice Some internal stuff
/// @notice Separate line
function _viewDuplicateTag() internal pure {
/// @notice Linter fail
/// @dev What have I done
function _viewLinterFail() internal pure {

}

// @notice Forgot one slash but it's sill natspec
//// @dev Too many slashes are fine too
/// @return Huh?
function _internalIncorrectSlashes() internal pure returns (uint256) {
return 0;
}

/// @notice Linter fail
/// @dev Oh god please help us
function _internalLinterFail() internal pure {}
}
// forgefmt: disable-end
134 changes: 128 additions & 6 deletions test/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ describe('Parser', () => {
inheritdoc: {
content: 'IParserTest',
},
tags: [
{
name: 'dev',
content: 'Providing context',
},
],
})
);
});
Expand Down Expand Up @@ -305,4 +299,132 @@ describe('Parser', () => {
);
});
});

describe('Contract with invalid natspec', () => {
beforeAll(async () => {
const compileResult = await getFileCompiledSource('sample-data/ParserTest.sol');
contract = compileResult.vContracts.find(({ name }) => name === 'ParserTestFunny')!;
});

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

expect(result).toEqual(
mockNatspec({
tags: [],
})
);
});

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

expect(result).toEqual(
mockNatspec({
inheritdoc: {
content: 'IParserTest',
},
tags: [
{
name: 'dev',
content: 'Providing context',
},
],
})
);
});

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

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

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

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

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

expect(result).toEqual(
mockNatspec({
tags: [
{
name: 'notice',
content: 'Some private stuff',
}
],
params: [
{
name: '_paramName',
content: 'The parameter name',
}
],
returns: [
{
name: '_returned',
content: 'The returned value',
}
]
})
);
});

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

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

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

expect(result).toEqual(
mockNatspec({
tags: [
{
name: 'notice',
content: 'Some text',
},
],
})
);
});

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

expect(result).toEqual(
mockNatspec({
tags: [
{
name: 'notice',
content: 'Linter fail',
},
{
name: 'dev',
content: 'What have I done'
}
],
})
);
});
});
});

0 comments on commit bbb022e

Please sign in to comment.