[isoltest] Add support for call side-effects.

This commit is contained in:
Alexander Arlt
2021-05-22 00:12:07 -05:00
parent 29c8f282e4
commit e9ee571b35
10 changed files with 242 additions and 21 deletions
+110 -5
View File
@@ -23,6 +23,7 @@
#include <string>
#include <tuple>
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <liblangutil/Exceptions.h>
#include <test/ExecutionFramework.h>
@@ -42,12 +43,10 @@ using Mode = FunctionCall::DisplayMode;
namespace
{
vector<FunctionCall> parse(string const& _source)
vector<FunctionCall> parse(string const& _source, std::map<std::string, Builtin> const& _builtins = {})
{
static std::map<std::string, Builtin> const builtins = {};
istringstream stream{_source, ios_base::out};
return TestFileParser{stream, builtins}.parseFunctionCalls(0);
return TestFileParser{stream, _builtins}.parseFunctionCalls(0);
}
void testFunctionCall(
@@ -100,7 +99,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
BOOST_REQUIRE_EQUAL(parse(source).size(), 0);
}
BOOST_AUTO_TEST_CASE(call_success)
BOOST_AUTO_TEST_CASE(call_succees)
{
char const* source = R"(
// success() ->
@@ -959,6 +958,112 @@ BOOST_AUTO_TEST_CASE(library)
);
}
BOOST_AUTO_TEST_CASE(call_effects)
{
std::map<std::string, Builtin> builtins;
builtins["builtin_returning_call_effect"] = [](FunctionCall const&) -> std::optional<bytes>
{
return util::toBigEndian(u256(0x1234));
};
builtins["builtin_returning_call_effect_no_ret"] = [](FunctionCall const&) -> std::optional<bytes>
{
return {};
};
char const* source = R"(
// builtin_returning_call_effect -> 1
// ~ bla
// ~ bla bla
// ~ bla bla bla
)";
vector<FunctionCall> calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 3);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "bla");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[1]), "bla bla");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[2]), "bla bla bla");
source = R"(
// builtin_returning_call_effect -> 1
// ~ bla
// ~ bla bla
// builtin_returning_call_effect -> 2
// ~ bla bla bla
// builtin_returning_call_effect -> 3
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 3);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 2);
BOOST_REQUIRE_EQUAL(calls[1].expectedSideEffects.size(), 1);
BOOST_REQUIRE_EQUAL(calls[2].expectedSideEffects.size(), 0);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "bla");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[1]), "bla bla");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[1].expectedSideEffects[0]), "bla bla bla");
source = R"(
// builtin_returning_call_effect -> 1
// ~ bla
// ~ bla bla bla
// ~ abc ~ def ~ ghi
// ~ ~ ~
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 4);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "bla");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[1]), "bla bla bla");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[2]), "abc ~ def ~ ghi");
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[3]), "~ ~");
source = R"(
// builtin_returning_call_effect_no_ret ->
// ~ hello world
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 1);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "hello world");
source = R"(
// builtin_returning_call_effect -> 1
// ~
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 1);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "");
source = R"(
// builtin_returning_call_effect -> 1 # a comment #
// ~ hello world
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 1);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "hello world");
source = R"(
// builtin_returning_call_effect_no_ret -> # another comment #
// ~ hello world
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 1);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "hello world");
source = R"(
// builtin_returning_call_effect_no_ret # another comment #
// ~ hello world
)";
calls = parse(source, builtins);
BOOST_REQUIRE_EQUAL(calls.size(), 1);
BOOST_REQUIRE_EQUAL(calls[0].expectedSideEffects.size(), 1);
BOOST_REQUIRE_EQUAL(boost::trim_copy(calls[0].expectedSideEffects[0]), "hello world");
source = R"(
// builtin_returning_call_effect_no_ret # another comment #
// ~ hello/world
)";
BOOST_CHECK_THROW(parse(source, builtins), std::exception);
source = R"(
// builtin_returning_call_effect_no_ret # another comment #
// ~ hello//world
)";
BOOST_CHECK_THROW(parse(source, builtins), std::exception);
}
BOOST_AUTO_TEST_SUITE_END()
}