Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08effa0

Browse files
committed
More verbose error messages.
1 parent 9d8edb4 commit 08effa0

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

test/libsolidity/AnalysisFramework.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
#include <libsolidity/ast/AST.h>
2727

28+
#include <libsolidity/parsing/Scanner.h>
29+
2830
#include <libdevcore/SHA3.h>
2931

3032
#include <boost/test/unit_test.hpp>
@@ -46,8 +48,7 @@ AnalysisFramework::parseAnalyseAndReturnError(
4648
m_compiler.addSource("", _insertVersionPragma ? "pragma solidity >=0.0;\n" + _source : _source);
4749
if (!m_compiler.parse())
4850
{
49-
printErrors();
50-
BOOST_ERROR("Parsing contract failed in analysis test suite.");
51+
BOOST_ERROR("Parsing contract failed in analysis test suite:" + formatErrors());
5152
}
5253

5354
m_compiler.analyze();
@@ -73,8 +74,7 @@ AnalysisFramework::parseAnalyseAndReturnError(
7374
{
7475
if (firstError && !_allowMultipleErrors)
7576
{
76-
printErrors();
77-
BOOST_FAIL("Multiple errors found.");
77+
BOOST_FAIL("Multiple errors found: " + formatErrors());
7878
}
7979
if (!firstError)
8080
firstError = currentError;
@@ -88,7 +88,10 @@ SourceUnit const* AnalysisFramework::parseAndAnalyse(string const& _source)
8888
{
8989
auto sourceAndError = parseAnalyseAndReturnError(_source);
9090
BOOST_REQUIRE(!!sourceAndError.first);
91-
BOOST_REQUIRE(!sourceAndError.second);
91+
string message;
92+
if (sourceAndError.second)
93+
message = "Unexpected error: " + formatError(*sourceAndError.second);
94+
BOOST_REQUIRE_MESSAGE(!sourceAndError.second, message);
9295
return sourceAndError.first;
9396
}
9497

@@ -101,17 +104,23 @@ Error AnalysisFramework::expectError(std::string const& _source, bool _warning,
101104
{
102105
auto sourceAndError = parseAnalyseAndReturnError(_source, _warning, true, _allowMultiple);
103106
BOOST_REQUIRE(!!sourceAndError.second);
104-
BOOST_REQUIRE(!!sourceAndError.first);
107+
BOOST_REQUIRE_MESSAGE(!!sourceAndError.first, "Expected error, but no error happened.");
105108
return *sourceAndError.second;
106109
}
107110

108-
void AnalysisFramework::printErrors()
111+
string AnalysisFramework::formatErrors()
109112
{
113+
string message;
110114
for (auto const& error: m_compiler.errors())
111-
SourceReferenceFormatter::printExceptionInformation(
112-
std::cerr,
113-
*error,
114-
(error->type() == Error::Type::Warning) ? "Warning" : "Error",
115+
message += formatError(*error);
116+
return message;
117+
}
118+
119+
string AnalysisFramework::formatError(Error const& _error)
120+
{
121+
return SourceReferenceFormatter::formatExceptionInformation(
122+
_error,
123+
(_error.type() == Error::Type::Warning) ? "Warning" : "Error",
115124
[&](std::string const& _sourceName) -> solidity::Scanner const& { return m_compiler.scanner(_sourceName); }
116125
);
117126
}

test/libsolidity/AnalysisFramework.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class AnalysisFramework
5757
bool success(std::string const& _source);
5858
Error expectError(std::string const& _source, bool _warning = false, bool _allowMultiple = false);
5959

60-
void printErrors();
60+
std::string formatErrors();
61+
std::string formatError(Error const& _error);
6162

6263
static ContractDefinition const* retrieveContractByName(SourceUnit const& _source, std::string const& _name);
6364
static FunctionTypePointer retrieveFunctionBySignature(
@@ -105,7 +106,10 @@ CHECK_ERROR_OR_WARNING(text, Warning, substring, true, true)
105106
do \
106107
{ \
107108
auto sourceAndError = parseAnalyseAndReturnError((text), true); \
108-
BOOST_CHECK(sourceAndError.second == nullptr); \
109+
std::string message; \
110+
if (sourceAndError.second) \
111+
message = formatError(*sourceAndError.second); \
112+
BOOST_CHECK_MESSAGE(!sourceAndError.second, message); \
109113
} \
110114
while(0)
111115

0 commit comments

Comments
 (0)