Browse Source

Fix in addStateVariableAccessor and adjustment of parser tests

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
cd677c0921
  1. 9
      libsolidity/Parser.cpp
  2. 33
      test/SolidityParser.cpp

9
libsolidity/Parser.cpp

@ -113,9 +113,10 @@ void Parser::addStateVariableAccessor(ASTPointer<VariableDeclaration> const& _va
vector<ASTPointer<FunctionDefinition>> & _functions) vector<ASTPointer<FunctionDefinition>> & _functions)
{ {
ASTNodeFactory nodeFactory(*this); ASTNodeFactory nodeFactory(*this);
nodeFactory.setLocationEmpty();
ASTPointer<ASTString> emptyDoc; ASTPointer<ASTString> emptyDoc;
ASTPointer<ParameterList> emptyParamList;
nodeFactory.markEndPosition(); vector<ASTPointer<VariableDeclaration>> parameters;
auto expression = nodeFactory.createNode<Identifier>(make_shared<ASTString>(_varDecl->getName())); auto expression = nodeFactory.createNode<Identifier>(make_shared<ASTString>(_varDecl->getName()));
vector<ASTPointer<Statement>> block_statements = {nodeFactory.createNode<Return>(expression)}; vector<ASTPointer<Statement>> block_statements = {nodeFactory.createNode<Return>(expression)};
@ -124,9 +125,9 @@ void Parser::addStateVariableAccessor(ASTPointer<VariableDeclaration> const& _va
true, // isPublic true, // isPublic
false, // not a Constructor false, // not a Constructor
emptyDoc, // no documentation emptyDoc, // no documentation
emptyParamList, // no parameters (temporary, a mapping would need parameters for example) nodeFactory.createNode<ParameterList>(vector<ASTPointer<VariableDeclaration>>()),
true, // is constant true, // is constant
emptyParamList, // no return parameters nodeFactory.createNode<ParameterList>(vector<ASTPointer<VariableDeclaration>>()),
nodeFactory.createNode<Block>(block_statements) nodeFactory.createNode<Block>(block_statements)
) )
); );

33
test/SolidityParser.cpp

@ -66,6 +66,14 @@ ASTPointer<ContractDefinition> parseTextExplainError(std::string const& _source)
} }
} }
static void checkFunctionNatspec(ASTPointer<FunctionDefinition> _function,
std::string const& _expectedDoc)
{
auto doc = _function->getDocumentation();
BOOST_CHECK_MESSAGE(doc != nullptr, "Function does not have Natspec Doc as expected");
BOOST_CHECK_EQUAL(*doc, _expectedDoc);
}
} }
@ -121,14 +129,16 @@ BOOST_AUTO_TEST_CASE(function_natspec_documentation)
ASTPointer<ContractDefinition> contract; ASTPointer<ContractDefinition> contract;
ASTPointer<FunctionDefinition> function; ASTPointer<FunctionDefinition> function;
char const* text = "contract test {\n" char const* text = "contract test {\n"
" private:\n"
" uint256 stateVar;\n" " uint256 stateVar;\n"
" public:\n"
" /// This is a test function\n" " /// This is a test function\n"
" function functionName(hash hashin) returns (hash hashout) {}\n" " function functionName(hash hashin) returns (hash hashout) {}\n"
"}\n"; "}\n";
BOOST_REQUIRE_NO_THROW(contract = parseText(text)); BOOST_REQUIRE_NO_THROW(contract = parseText(text));
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
BOOST_REQUIRE_NO_THROW(function = functions.at(0)); BOOST_REQUIRE_NO_THROW(function = functions.at(0));
BOOST_CHECK_EQUAL(*function->getDocumentation(), "This is a test function"); checkFunctionNatspec(function, "This is a test function");
} }
BOOST_AUTO_TEST_CASE(function_normal_comments) BOOST_AUTO_TEST_CASE(function_normal_comments)
@ -144,7 +154,7 @@ BOOST_AUTO_TEST_CASE(function_normal_comments)
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
BOOST_REQUIRE_NO_THROW(function = functions.at(0)); BOOST_REQUIRE_NO_THROW(function = functions.at(0));
BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr, BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr,
"Should not have gotten a Natspect comment for this function"); "Should not have gotten a Natspecc comment for this function");
} }
BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation) BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation)
@ -152,7 +162,9 @@ BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation)
ASTPointer<ContractDefinition> contract; ASTPointer<ContractDefinition> contract;
ASTPointer<FunctionDefinition> function; ASTPointer<FunctionDefinition> function;
char const* text = "contract test {\n" char const* text = "contract test {\n"
" private:\n"
" uint256 stateVar;\n" " uint256 stateVar;\n"
" public:\n"
" /// This is test function 1\n" " /// This is test function 1\n"
" function functionName1(hash hashin) returns (hash hashout) {}\n" " function functionName1(hash hashin) returns (hash hashout) {}\n"
" /// This is test function 2\n" " /// This is test function 2\n"
@ -166,17 +178,17 @@ BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation)
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
BOOST_REQUIRE_NO_THROW(function = functions.at(0)); BOOST_REQUIRE_NO_THROW(function = functions.at(0));
BOOST_CHECK_EQUAL(*function->getDocumentation(), "This is test function 1"); checkFunctionNatspec(function, "This is test function 1");
BOOST_REQUIRE_NO_THROW(function = functions.at(1)); BOOST_REQUIRE_NO_THROW(function = functions.at(1));
BOOST_CHECK_EQUAL(*function->getDocumentation(), "This is test function 2"); checkFunctionNatspec(function, "This is test function 2");
BOOST_REQUIRE_NO_THROW(function = functions.at(2)); BOOST_REQUIRE_NO_THROW(function = functions.at(2));
BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr, BOOST_CHECK_MESSAGE(function->getDocumentation() == nullptr,
"Should not have gotten natspec comment for functionName3()"); "Should not have gotten natspec comment for functionName3()");
BOOST_REQUIRE_NO_THROW(function = functions.at(3)); BOOST_REQUIRE_NO_THROW(function = functions.at(3));
BOOST_CHECK_EQUAL(*function->getDocumentation(), "This is test function 4"); checkFunctionNatspec(function, "This is test function 4");
} }
BOOST_AUTO_TEST_CASE(multiline_function_documentation) BOOST_AUTO_TEST_CASE(multiline_function_documentation)
@ -192,9 +204,8 @@ BOOST_AUTO_TEST_CASE(multiline_function_documentation)
BOOST_REQUIRE_NO_THROW(contract = parseText(text)); BOOST_REQUIRE_NO_THROW(contract = parseText(text));
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
BOOST_REQUIRE_NO_THROW(function = functions.at(0)); BOOST_REQUIRE_NO_THROW(function = functions.at(1)); // 1 since, 0 is the index of stateVar accessor
BOOST_CHECK_EQUAL(*function->getDocumentation(), checkFunctionNatspec(function, "This is a test function\n"
"This is a test function\n"
" and it has 2 lines"); " and it has 2 lines");
} }
@ -211,7 +222,6 @@ BOOST_AUTO_TEST_CASE(natspec_comment_in_function_body)
" mapping(address=>hash) d;\n" " mapping(address=>hash) d;\n"
" string name = \"Solidity\";" " string name = \"Solidity\";"
" }\n" " }\n"
" uint256 stateVar;\n"
" /// This is a test function\n" " /// This is a test function\n"
" /// and it has 2 lines\n" " /// and it has 2 lines\n"
" function fun(hash hashin) returns (hash hashout) {}\n" " function fun(hash hashin) returns (hash hashout) {}\n"
@ -220,11 +230,10 @@ BOOST_AUTO_TEST_CASE(natspec_comment_in_function_body)
auto functions = contract->getDefinedFunctions(); auto functions = contract->getDefinedFunctions();
BOOST_REQUIRE_NO_THROW(function = functions.at(0)); BOOST_REQUIRE_NO_THROW(function = functions.at(0));
BOOST_CHECK_EQUAL(*function->getDocumentation(), "fun1 description"); checkFunctionNatspec(function, "fun1 description");
BOOST_REQUIRE_NO_THROW(function = functions.at(1)); BOOST_REQUIRE_NO_THROW(function = functions.at(1));
BOOST_CHECK_EQUAL(*function->getDocumentation(), checkFunctionNatspec(function, "This is a test function\n"
"This is a test function\n"
" and it has 2 lines"); " and it has 2 lines");
} }

Loading…
Cancel
Save