Merge pull request #2232 from chriseth/sol_externalConstructorTests

Tests for constructor arguments "from outside".
This commit is contained in:
chriseth 2015-06-19 10:29:59 +02:00
commit 2a870afc7b
2 changed files with 38 additions and 8 deletions

View File

@ -1726,7 +1726,7 @@ BOOST_AUTO_TEST_CASE(fixed_bytes_in_calls)
BOOST_CHECK(callContractFunction("callHelper(bytes2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5))); BOOST_CHECK(callContractFunction("callHelper(bytes2,bool)", string("\0a", 2), true) == encodeArgs(string("\0a\0\0\0", 5)));
} }
BOOST_AUTO_TEST_CASE(constructor_arguments) BOOST_AUTO_TEST_CASE(constructor_arguments_internal)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
contract Helper { contract Helper {
@ -1749,8 +1749,28 @@ BOOST_AUTO_TEST_CASE(constructor_arguments)
function getName() returns (bytes3 ret) { return h.getName(); } function getName() returns (bytes3 ret) { return h.getName(); }
})"; })";
compileAndRun(sourceCode, 0, "Main"); compileAndRun(sourceCode, 0, "Main");
BOOST_REQUIRE(callContractFunction("getFlag()") == encodeArgs(true)); BOOST_CHECK(callContractFunction("getFlag()") == encodeArgs(true));
BOOST_REQUIRE(callContractFunction("getName()") == encodeArgs("abc")); BOOST_CHECK(callContractFunction("getName()") == encodeArgs("abc"));
}
BOOST_AUTO_TEST_CASE(constructor_arguments_external)
{
char const* sourceCode = R"(
contract Main {
bytes3 name;
bool flag;
function Main(bytes3 x, bool f) {
name = x;
flag = f;
}
function getName() returns (bytes3 ret) { return name; }
function getFlag() returns (bool ret) { return flag; }
}
)";
compileAndRun(sourceCode, 0, "Main", encodeArgs("abc", true));
BOOST_CHECK(callContractFunction("getFlag()") == encodeArgs(true));
BOOST_CHECK(callContractFunction("getName()") == encodeArgs("abc"));
} }
BOOST_AUTO_TEST_CASE(functions_called_by_constructor) BOOST_AUTO_TEST_CASE(functions_called_by_constructor)
@ -4166,7 +4186,7 @@ BOOST_AUTO_TEST_CASE(evm_exceptions_in_constructor_out_of_baund)
} }
} }
)"; )";
BOOST_CHECK(compileAndRunWthoutCheck(sourceCode, 0, "A").empty()); BOOST_CHECK(compileAndRunWithoutCheck(sourceCode, 0, "A").empty());
} }
BOOST_AUTO_TEST_CASE(positive_integers_to_signed) BOOST_AUTO_TEST_CASE(positive_integers_to_signed)

View File

@ -42,19 +42,29 @@ class ExecutionFramework
public: public:
ExecutionFramework() { g_logVerbosity = 0; } ExecutionFramework() { g_logVerbosity = 0; }
bytes const& compileAndRunWthoutCheck(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") bytes const& compileAndRunWithoutCheck(
std::string const& _sourceCode,
u256 const& _value = 0,
std::string const& _contractName = "",
bytes const& _arguments = bytes()
)
{ {
m_compiler.reset(false, m_addStandardSources); m_compiler.reset(false, m_addStandardSources);
m_compiler.addSource("", _sourceCode); m_compiler.addSource("", _sourceCode);
ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed"); ETH_TEST_REQUIRE_NO_THROW(m_compiler.compile(m_optimize, m_optimizeRuns), "Compiling contract failed");
bytes code = m_compiler.getBytecode(_contractName); bytes code = m_compiler.getBytecode(_contractName);
sendMessage(code, true, _value); sendMessage(code + _arguments, true, _value);
return m_output; return m_output;
} }
bytes const& compileAndRun(std::string const& _sourceCode, u256 const& _value = 0, std::string const& _contractName = "") bytes const& compileAndRun(
std::string const& _sourceCode,
u256 const& _value = 0,
std::string const& _contractName = "",
bytes const& _arguments = bytes()
)
{ {
compileAndRunWthoutCheck(_sourceCode, _value, _contractName); compileAndRunWithoutCheck(_sourceCode, _value, _contractName, _arguments);
BOOST_REQUIRE(!m_output.empty()); BOOST_REQUIRE(!m_output.empty());
return m_output; return m_output;
} }