aboutsummaryrefslogtreecommitdiffstats
path: root/solidityParser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'solidityParser.cpp')
-rw-r--r--solidityParser.cpp161
1 files changed, 160 insertions, 1 deletions
diff --git a/solidityParser.cpp b/solidityParser.cpp
index 9319a02c..6a97a5d9 100644
--- a/solidityParser.cpp
+++ b/solidityParser.cpp
@@ -37,13 +37,14 @@ namespace test
namespace
{
-ASTPointer<ASTNode> parseText(std::string const& _source)
+ASTPointer<ContractDefinition> parseText(std::string const& _source)
{
Parser parser;
return parser.parse(std::make_shared<Scanner>(CharStream(_source)));
}
}
+
BOOST_AUTO_TEST_SUITE(SolidityParser)
BOOST_AUTO_TEST_CASE(smoke_test)
@@ -91,6 +92,164 @@ BOOST_AUTO_TEST_CASE(single_function_param)
BOOST_CHECK_NO_THROW(parseText(text));
}
+BOOST_AUTO_TEST_CASE(function_natspec_documentation)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " /// This is a test function\n"
+ " function functionName(hash hashin) returns (hash hashout) {}\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(), " This is a test function");
+}
+
+BOOST_AUTO_TEST_CASE(function_normal_comments)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " // We won't see this comment\n"
+ " function functionName(hash hashin) returns (hash hashout) {}\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr,
+ "Should not have gotten a Natspect comment for this function");
+}
+
+BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " /// This is test function 1\n"
+ " function functionName1(hash hashin) returns (hash hashout) {}\n"
+ " /// This is test function 2\n"
+ " function functionName2(hash hashin) returns (hash hashout) {}\n"
+ " // nothing to see here\n"
+ " function functionName3(hash hashin) returns (hash hashout) {}\n"
+ " /// This is test function 4\n"
+ " function functionName4(hash hashin) returns (hash hashout) {}\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(), " This is test function 1");
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(1));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(), " This is test function 2");
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(2));
+ BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr,
+ "Should not have gotten natspec comment for functionName3()");
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(3));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(), " This is test function 4");
+}
+
+BOOST_AUTO_TEST_CASE(multiline_function_documentation)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " /// This is a test function\n"
+ " /// and it has 2 lines\n"
+ " function functionName1(hash hashin) returns (hash hashout) {}\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(),
+ " This is a test function\n"
+ " and it has 2 lines");
+}
+
+BOOST_AUTO_TEST_CASE(natspec_comment_in_function_body)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " /// fun1 description\n"
+ " function fun1(uint256 a) {\n"
+ " var b;\n"
+ " /// I should not interfere with actual natspec comments\n"
+ " uint256 c;\n"
+ " mapping(address=>hash) d;\n"
+ " string name = \"Solidity\";"
+ " }\n"
+ " uint256 stateVar;\n"
+ " /// This is a test function\n"
+ " /// and it has 2 lines\n"
+ " function fun(hash hashin) returns (hash hashout) {}\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(), " fun1 description");
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(1));
+ BOOST_CHECK_EQUAL(*function->getDocumentation(),
+ " This is a test function\n"
+ " and it has 2 lines");
+}
+
+BOOST_AUTO_TEST_CASE(natspec_docstring_between_keyword_and_signature)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function ///I am in the wrong place \n"
+ " fun1(uint256 a) {\n"
+ " var b;\n"
+ " /// I should not interfere with actual natspec comments\n"
+ " uint256 c;\n"
+ " mapping(address=>hash) d;\n"
+ " string name = \"Solidity\";"
+ " }\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_MESSAGE(!function->getDocumentation(),
+ "Shouldn't get natspec docstring for this function");
+}
+
+BOOST_AUTO_TEST_CASE(natspec_docstring_after_signature)
+{
+ ASTPointer<ContractDefinition> contract;
+ ASTPointer<FunctionDefinition> function;
+ char const* text = "contract test {\n"
+ " uint256 stateVar;\n"
+ " function fun1(uint256 a) {\n"
+ " /// I should have been above the function signature\n"
+ " var b;\n"
+ " /// I should not interfere with actual natspec comments\n"
+ " uint256 c;\n"
+ " mapping(address=>hash) d;\n"
+ " string name = \"Solidity\";"
+ " }\n"
+ "}\n";
+ BOOST_REQUIRE_NO_THROW(contract = parseText(text));
+ auto functions = contract->getDefinedFunctions();
+
+ BOOST_REQUIRE_NO_THROW(function = functions.at(0));
+ BOOST_CHECK_MESSAGE(!function->getDocumentation(),
+ "Shouldn't get natspec docstring for this function");
+}
+
BOOST_AUTO_TEST_CASE(struct_definition)
{
char const* text = "contract test {\n"