Merge pull request #5937 from ethereum/libsolc-callback-test

Add test cases for libsolc with callbacks
This commit is contained in:
chriseth 2019-02-05 21:11:44 +01:00 committed by GitHub
commit 263eaaa516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,9 +40,28 @@ namespace test
namespace
{
Json::Value compile(string const& _input)
/// TODO: share this between StandardCompiler.cpp
/// Helper to match a specific error type and message
bool containsError(Json::Value const& _compilerResult, string const& _type, string const& _message)
{
string output(solidity_compile(_input.c_str(), nullptr));
if (!_compilerResult.isMember("errors"))
return false;
for (auto const& error: _compilerResult["errors"])
{
BOOST_REQUIRE(error.isObject());
BOOST_REQUIRE(error["type"].isString());
BOOST_REQUIRE(error["message"].isString());
if ((error["type"].asString() == _type) && (error["message"].asString() == _message))
return true;
}
return false;
}
Json::Value compile(string const& _input, CStyleReadFileCallback _callback = nullptr)
{
string output(solidity_compile(_input.c_str(), _callback));
Json::Value ret;
BOOST_REQUIRE(jsonParseStrict(output, ret));
solidity_free();
@ -89,6 +108,63 @@ BOOST_AUTO_TEST_CASE(standard_compilation)
BOOST_CHECK(!result.isMember("contracts"));
}
BOOST_AUTO_TEST_CASE(missing_callback)
{
char const* input = R"(
{
"language": "Solidity",
"sources": {
"fileA": {
"content": "import \"missing.sol\"; contract A { }"
}
}
}
)";
Json::Value result = compile(input);
BOOST_CHECK(result.isObject());
BOOST_CHECK(containsError(result, "ParserError", "Source \"missing.sol\" not found: File not supplied initially."));
}
BOOST_AUTO_TEST_CASE(with_callback)
{
char const* input = R"(
{
"language": "Solidity",
"sources": {
"fileA": {
"content": "import \"found.sol\"; contract A { }"
}
}
}
)";
CStyleReadFileCallback callback{
[](char const* _path, char** o_contents, char** o_error)
{
// Caller frees the pointers.
if (string(_path) == "found.sol")
{
static string content{"import \"missing.sol\"; contract B {}"};
*o_contents = strdup(content.c_str());
*o_error = nullptr;
}
else
{
static string errorMsg{"Missing file."};
*o_error = strdup(errorMsg.c_str());
*o_contents = nullptr;
}
}
};
Json::Value result = compile(input, callback);
BOOST_CHECK(result.isObject());
// This ensures that "found.sol" was properly loaded which triggered the second import statement.
BOOST_CHECK(containsError(result, "ParserError", "Source \"missing.sol\" not found: Missing file."));
}
BOOST_AUTO_TEST_SUITE_END()
}