Merge pull request #2935 from ethereum/cleanupTestHelpers

Cleanup test helper macros.
This commit is contained in:
Alex Beregszaszi 2017-09-22 16:48:37 +01:00 committed by GitHub
commit 1be33872ba
10 changed files with 22 additions and 96 deletions

View File

@ -15,8 +15,6 @@
along with solidity. If not, see <http://www.gnu.org/licenses/>. along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** @file TestHelper.h /** @file TestHelper.h
* @author Marko Simovic <markobarko@gmail.com>
* @date 2014
*/ */
#pragma once #pragma once
@ -31,78 +29,6 @@ namespace dev
namespace test namespace test
{ {
#if (BOOST_VERSION >= 105900)
#define ETH_BOOST_CHECK_IMPL(_message, _requireOrCheck) BOOST_TEST_TOOL_DIRECT_IMPL( \
false, \
_requireOrCheck, \
_message \
)
#else
#define ETH_BOOST_CHECK_IMPL(_message, _requireOrCheck) BOOST_CHECK_IMPL( \
false, \
_message, \
_requireOrCheck, \
CHECK_MSG \
)
#endif
/// Make sure that no Exception is thrown during testing. If one is thrown show its info and fail the test.
/// Our version of BOOST_REQUIRE_NO_THROW()
/// @param _statement The statement for which to make sure no exceptions are thrown
/// @param _message A message to act as a prefix to the expression's error information
#define ETH_TEST_REQUIRE_NO_THROW(_statement, _message) \
do \
{ \
try \
{ \
BOOST_TEST_PASSPOINT(); \
_statement; \
} \
catch (boost::exception const& _e) \
{ \
auto msg = std::string(_message " due to an exception thrown by " \
BOOST_STRINGIZE(_statement) "\n") + boost::diagnostic_information(_e); \
ETH_BOOST_CHECK_IMPL(msg, REQUIRE); \
} \
catch (...) \
{ \
ETH_BOOST_CHECK_IMPL( \
"Unknown exception thrown by " BOOST_STRINGIZE(_statement), \
REQUIRE \
); \
} \
} \
while (0)
/// Check if an Exception is thrown during testing. If one is thrown show its info and continue the test
/// Our version of BOOST_CHECK_NO_THROW()
/// @param _statement The statement for which to make sure no exceptions are thrown
/// @param _message A message to act as a prefix to the expression's error information
#define ETH_TEST_CHECK_NO_THROW(_statement, _message) \
do \
{ \
try \
{ \
BOOST_TEST_PASSPOINT(); \
_statement; \
} \
catch (boost::exception const& _e) \
{ \
auto msg = std::string(_message " due to an exception thrown by " \
BOOST_STRINGIZE(_statement) "\n") + boost::diagnostic_information(_e); \
ETH_BOOST_CHECK_IMPL(msg, CHECK); \
} \
catch (...) \
{ \
ETH_BOOST_CHECK_IMPL( \
"Unknown exception thrown by " BOOST_STRINGIZE(_statement), \
CHECK \
); \
} \
} \
while (0)
struct Options: boost::noncopyable struct Options: boost::noncopyable
{ {
std::string ipcPath; std::string ipcPath;

View File

@ -223,7 +223,7 @@ protected:
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", registrarCode); m_compiler.addSource("", registrarCode);
m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
s_compiledRegistrar.reset(new bytes(m_compiler.object("GlobalRegistrar").bytecode)); s_compiledRegistrar.reset(new bytes(m_compiler.object("GlobalRegistrar").bytecode));
} }
sendMessage(*s_compiledRegistrar, true); sendMessage(*s_compiledRegistrar, true);

View File

@ -136,7 +136,7 @@ protected:
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", registrarCode); m_compiler.addSource("", registrarCode);
m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode)); s_compiledRegistrar.reset(new bytes(m_compiler.object("FixedFeeRegistrar").bytecode));
} }
sendMessage(*s_compiledRegistrar, true); sendMessage(*s_compiledRegistrar, true);

View File

@ -451,7 +451,7 @@ protected:
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", walletCode); m_compiler.addSource("", walletCode);
m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns); m_compiler.setOptimiserSettings(m_optimize, m_optimizeRuns);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
s_compiledWallet.reset(new bytes(m_compiler.object("Wallet").bytecode)); s_compiledWallet.reset(new bytes(m_compiler.object("Wallet").bytecode));
} }
bytes args = encodeArgs(u256(0x60), _required, _dailyLimit, u256(_owners.size()), _owners); bytes args = encodeArgs(u256(0x60), _required, _dailyLimit, u256(_owners.size()), _owners);

View File

@ -50,7 +50,7 @@ public:
m_compiler.reset(false); m_compiler.reset(false);
m_compiler.addSource("", "pragma solidity >=0.0;\n" + _sourceCode); m_compiler.addSource("", "pragma solidity >=0.0;\n" + _sourceCode);
m_compiler.setOptimiserSettings(dev::test::Options::get().optimize); m_compiler.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(m_compiler.compile(), "Compiling contract failed");
AssemblyItems const* items = m_compiler.runtimeAssemblyItems(""); AssemblyItems const* items = m_compiler.runtimeAssemblyItems("");
ASTNode const& sourceUnit = m_compiler.ast(); ASTNode const& sourceUnit = m_compiler.ast();

View File

@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
CompilerStack compilerStack; CompilerStack compilerStack;
compilerStack.addSource("", std::string(sourceCode)); compilerStack.addSource("", std::string(sourceCode));
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize); compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode; bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
std::string const& metadata = compilerStack.metadata("test"); std::string const& metadata = compilerStack.metadata("test");
BOOST_CHECK(dev::test::isValidMetadata(metadata)); BOOST_CHECK(dev::test::isValidMetadata(metadata));
@ -72,7 +72,7 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
CompilerStack compilerStack; CompilerStack compilerStack;
compilerStack.addSource("", std::string(sourceCode)); compilerStack.addSource("", std::string(sourceCode));
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize); compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
bytes const& bytecode = compilerStack.runtimeObject("test").bytecode; bytes const& bytecode = compilerStack.runtimeObject("test").bytecode;
std::string const& metadata = compilerStack.metadata("test"); std::string const& metadata = compilerStack.metadata("test");
BOOST_CHECK(dev::test::isValidMetadata(metadata)); BOOST_CHECK(dev::test::isValidMetadata(metadata));
@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(metadata_relevant_sources)
)"; )";
compilerStack.addSource("B", std::string(sourceCode)); compilerStack.addSource("B", std::string(sourceCode));
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize); compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata("A"); std::string const& serialisedMetadata = compilerStack.metadata("A");
BOOST_CHECK(dev::test::isValidMetadata(serialisedMetadata)); BOOST_CHECK(dev::test::isValidMetadata(serialisedMetadata));
@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE(metadata_relevant_sources_imports)
)"; )";
compilerStack.addSource("C", std::string(sourceCode)); compilerStack.addSource("C", std::string(sourceCode));
compilerStack.setOptimiserSettings(dev::test::Options::get().optimize); compilerStack.setOptimiserSettings(dev::test::Options::get().optimize);
ETH_TEST_REQUIRE_NO_THROW(compilerStack.compile(), "Compiling contract failed"); BOOST_REQUIRE_MESSAGE(compilerStack.compile(), "Compiling contract failed");
std::string const& serialisedMetadata = compilerStack.metadata("C"); std::string const& serialisedMetadata = compilerStack.metadata("C");
BOOST_CHECK(dev::test::isValidMetadata(serialisedMetadata)); BOOST_CHECK(dev::test::isValidMetadata(serialisedMetadata));

View File

@ -44,7 +44,7 @@ public:
{ {
m_compilerStack.reset(false); m_compilerStack.reset(false);
m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code); m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code);
ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parseAndAnalyze(), "Parsing contract failed"); BOOST_REQUIRE_MESSAGE(m_compilerStack.parseAndAnalyze(), "Parsing contract failed");
Json::Value generatedInterface = m_compilerStack.contractABI(""); Json::Value generatedInterface = m_compilerStack.contractABI("");
Json::Value expectedInterface; Json::Value expectedInterface;

View File

@ -125,7 +125,7 @@ bytes compileFirstExpression(
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes()) for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get())) if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{ {
ETH_TEST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract), "Resolving names failed"); BOOST_REQUIRE_MESSAGE(resolver.resolveNamesAndTypes(*contract), "Resolving names failed");
inheritanceHierarchy = vector<ContractDefinition const*>(1, contract); inheritanceHierarchy = vector<ContractDefinition const*>(1, contract);
} }
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes()) for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())

View File

@ -47,7 +47,7 @@ public:
{ {
m_compilerStack.reset(false); m_compilerStack.reset(false);
m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code); m_compilerStack.addSource("", "pragma solidity >=0.0;\n" + _code);
ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parseAndAnalyze(), "Parsing contract failed"); BOOST_REQUIRE_MESSAGE(m_compilerStack.parseAndAnalyze(), "Parsing contract failed");
Json::Value generatedDocumentation; Json::Value generatedDocumentation;
if (_userDocumentation) if (_userDocumentation)

View File

@ -250,7 +250,7 @@ BOOST_AUTO_TEST_CASE(function_natspec_documentation)
FunctionDefinition const* function = nullptr; FunctionDefinition const* function = nullptr;
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
checkFunctionNatspec(function, "This is a test function"); checkFunctionNatspec(function, "This is a test function");
} }
@ -268,7 +268,7 @@ BOOST_AUTO_TEST_CASE(function_normal_comments)
ErrorList errors; ErrorList errors;
ASTPointer<ContractDefinition> contract = parseText(text, errors); ASTPointer<ContractDefinition> contract = parseText(text, errors);
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
BOOST_CHECK_MESSAGE(function->documentation() == nullptr, BOOST_CHECK_MESSAGE(function->documentation() == nullptr,
"Should not have gotten a Natspecc comment for this function"); "Should not have gotten a Natspecc comment for this function");
} }
@ -294,17 +294,17 @@ BOOST_AUTO_TEST_CASE(multiple_functions_natspec_documentation)
ASTPointer<ContractDefinition> contract = parseText(text, errors); ASTPointer<ContractDefinition> contract = parseText(text, errors);
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
checkFunctionNatspec(function, "This is test function 1"); checkFunctionNatspec(function, "This is test function 1");
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(1), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(1), "Failed to retrieve function");
checkFunctionNatspec(function, "This is test function 2"); checkFunctionNatspec(function, "This is test function 2");
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(2), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(2), "Failed to retrieve function");
BOOST_CHECK_MESSAGE(function->documentation() == nullptr, BOOST_CHECK_MESSAGE(function->documentation() == nullptr,
"Should not have gotten natspec comment for functionName3()"); "Should not have gotten natspec comment for functionName3()");
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(3), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(3), "Failed to retrieve function");
checkFunctionNatspec(function, "This is test function 4"); checkFunctionNatspec(function, "This is test function 4");
} }
@ -323,7 +323,7 @@ BOOST_AUTO_TEST_CASE(multiline_function_documentation)
ErrorList errors; ErrorList errors;
ASTPointer<ContractDefinition> contract = parseText(text, errors); ASTPointer<ContractDefinition> contract = parseText(text, errors);
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
checkFunctionNatspec(function, "This is a test function\n" checkFunctionNatspec(function, "This is a test function\n"
" and it has 2 lines"); " and it has 2 lines");
} }
@ -351,10 +351,10 @@ BOOST_AUTO_TEST_CASE(natspec_comment_in_function_body)
ASTPointer<ContractDefinition> contract = parseText(text, errors); ASTPointer<ContractDefinition> contract = parseText(text, errors);
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
checkFunctionNatspec(function, "fun1 description"); checkFunctionNatspec(function, "fun1 description");
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(1), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(1), "Failed to retrieve function");
checkFunctionNatspec(function, "This is a test function\n" checkFunctionNatspec(function, "This is a test function\n"
" and it has 2 lines"); " and it has 2 lines");
} }
@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE(natspec_docstring_between_keyword_and_signature)
ASTPointer<ContractDefinition> contract = parseText(text, errors); ASTPointer<ContractDefinition> contract = parseText(text, errors);
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
BOOST_CHECK_MESSAGE(!function->documentation(), BOOST_CHECK_MESSAGE(!function->documentation(),
"Shouldn't get natspec docstring for this function"); "Shouldn't get natspec docstring for this function");
} }
@ -406,7 +406,7 @@ BOOST_AUTO_TEST_CASE(natspec_docstring_after_signature)
ASTPointer<ContractDefinition> contract = parseText(text, errors); ASTPointer<ContractDefinition> contract = parseText(text, errors);
auto functions = contract->definedFunctions(); auto functions = contract->definedFunctions();
ETH_TEST_REQUIRE_NO_THROW(function = functions.at(0), "Failed to retrieve function"); BOOST_REQUIRE_MESSAGE(function = functions.at(0), "Failed to retrieve function");
BOOST_CHECK_MESSAGE(!function->documentation(), BOOST_CHECK_MESSAGE(!function->documentation(),
"Shouldn't get natspec docstring for this function"); "Shouldn't get natspec docstring for this function");
} }