Throw TestParserError instead of Error in tests

This commit is contained in:
a3d4 2020-05-28 01:17:53 +02:00
parent 011f8a462d
commit be83c54d79
4 changed files with 57 additions and 50 deletions

View File

@ -81,7 +81,7 @@ bytes BytesUtils::convertBoolean(string const& _literal)
else if (_literal == "false") else if (_literal == "false")
return bytes{false}; return bytes{false};
else else
throw Error(Error::Type::ParserError, "Boolean literal invalid."); throw TestParserError("Boolean literal invalid.");
} }
bytes BytesUtils::convertNumber(string const& _literal) bytes BytesUtils::convertNumber(string const& _literal)
@ -92,7 +92,7 @@ bytes BytesUtils::convertNumber(string const& _literal)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "Number encoding invalid."); throw TestParserError("Number encoding invalid.");
} }
} }
@ -104,7 +104,7 @@ bytes BytesUtils::convertHexNumber(string const& _literal)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "Hex number encoding invalid."); throw TestParserError("Hex number encoding invalid.");
} }
} }
@ -116,7 +116,7 @@ bytes BytesUtils::convertString(string const& _literal)
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "String encoding invalid."); throw TestParserError("String encoding invalid.");
} }
} }

View File

@ -30,6 +30,15 @@ namespace solidity::frontend::test
while (false) while (false)
class TestParserError: virtual public util::Exception
{
public:
explicit TestParserError(std::string const& _description)
{
*this << util::errinfo_comment(_description);
}
};
/** /**
* Representation of a notice, warning or error that can occur while * Representation of a notice, warning or error that can occur while
* formatting and therefore updating an interactive function call test. * formatting and therefore updating an interactive function call test.

View File

@ -18,6 +18,7 @@
#include <test/libsolidity/util/TestFileParser.h> #include <test/libsolidity/util/TestFileParser.h>
#include <test/libsolidity/util/BytesUtils.h> #include <test/libsolidity/util/BytesUtils.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/Common.h> #include <test/Common.h>
#include <liblangutil/Common.h> #include <liblangutil/Common.h>
@ -128,9 +129,9 @@ vector<solidity::frontend::test::FunctionCall> TestFileParser::parseFunctionCall
calls.emplace_back(std::move(call)); calls.emplace_back(std::move(call));
} }
catch (Error const& _e) catch (TestParserError const& _e)
{ {
throw Error{_e.type(), "Line " + to_string(_lineOffset + m_lineNumber) + ": " + _e.what()}; throw TestParserError("Line " + to_string(_lineOffset + m_lineNumber) + ": " + _e.what());
} }
} }
} }
@ -150,8 +151,7 @@ bool TestFileParser::accept(soltest::Token _token, bool const _expect)
bool TestFileParser::expect(soltest::Token _token, bool const _advance) bool TestFileParser::expect(soltest::Token _token, bool const _advance)
{ {
if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid) if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid)
throw Error( throw TestParserError(
Error::Type::ParserError,
"Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" + "Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" +
m_scanner.currentLiteral() + "\". " + m_scanner.currentLiteral() + "\". " +
"Expected \"" + formatToken(_token) + "\"." "Expected \"" + formatToken(_token) + "\"."
@ -187,10 +187,10 @@ pair<string, bool> TestFileParser::parseFunctionSignature()
parameters += parseIdentifierOrTuple(); parameters += parseIdentifierOrTuple();
} }
if (accept(Token::Arrow, true)) if (accept(Token::Arrow, true))
throw Error(Error::Type::ParserError, "Invalid signature detected: " + signature); throw TestParserError("Invalid signature detected: " + signature);
if (!hasName && !parameters.empty()) if (!hasName && !parameters.empty())
throw Error(Error::Type::ParserError, "Signatures without a name cannot have parameters: " + signature); throw TestParserError("Signatures without a name cannot have parameters: " + signature);
else else
signature += parameters; signature += parameters;
@ -207,7 +207,7 @@ FunctionValue TestFileParser::parseFunctionCallValue()
u256 value{ parseDecimalNumber() }; u256 value{ parseDecimalNumber() };
Token token = m_scanner.currentToken(); Token token = m_scanner.currentToken();
if (token != Token::Ether && token != Token::Wei) if (token != Token::Ether && token != Token::Wei)
throw Error(Error::Type::ParserError, "Invalid value unit provided. Coins can be wei or ether."); throw TestParserError("Invalid value unit provided. Coins can be wei or ether.");
m_scanner.scanNextToken(); m_scanner.scanNextToken();
@ -216,7 +216,7 @@ FunctionValue TestFileParser::parseFunctionCallValue()
} }
catch (std::exception const&) catch (std::exception const&)
{ {
throw Error(Error::Type::ParserError, "Ether value encoding invalid."); throw TestParserError("Ether value encoding invalid.");
} }
} }
@ -226,7 +226,7 @@ FunctionCallArgs TestFileParser::parseFunctionCallArguments()
auto param = parseParameter(); auto param = parseParameter();
if (param.abiType.type == ABIType::None) if (param.abiType.type == ABIType::None)
throw Error(Error::Type::ParserError, "No argument provided."); throw TestParserError("No argument provided.");
arguments.parameters.emplace_back(param); arguments.parameters.emplace_back(param);
while (accept(Token::Comma, true)) while (accept(Token::Comma, true))
@ -290,7 +290,7 @@ Parameter TestFileParser::parseParameter()
if (accept(Token::Boolean)) if (accept(Token::Boolean))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid boolean literal."); throw TestParserError("Invalid boolean literal.");
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32}; parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
string parsed = parseBoolean(); string parsed = parseBoolean();
@ -304,7 +304,7 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::HexNumber)) else if (accept(Token::HexNumber))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid hex number literal."); throw TestParserError("Invalid hex number literal.");
parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32}; parameter.abiType = ABIType{ABIType::Hex, ABIType::AlignRight, 32};
string parsed = parseHexNumber(); string parsed = parseHexNumber();
@ -318,9 +318,9 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::Hex, true)) else if (accept(Token::Hex, true))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid hex string literal."); throw TestParserError("Invalid hex string literal.");
if (parameter.alignment != Parameter::Alignment::None) if (parameter.alignment != Parameter::Alignment::None)
throw Error(Error::Type::ParserError, "Hex string literals cannot be aligned or padded."); throw TestParserError("Hex string literals cannot be aligned or padded.");
string parsed = parseString(); string parsed = parseString();
parameter.rawString += "hex\"" + parsed + "\""; parameter.rawString += "hex\"" + parsed + "\"";
@ -332,9 +332,9 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::String)) else if (accept(Token::String))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid string literal."); throw TestParserError("Invalid string literal.");
if (parameter.alignment != Parameter::Alignment::None) if (parameter.alignment != Parameter::Alignment::None)
throw Error(Error::Type::ParserError, "String literals cannot be aligned or padded."); throw TestParserError("String literals cannot be aligned or padded.");
string parsed = parseString(); string parsed = parseString();
parameter.abiType = ABIType{ABIType::String, ABIType::AlignLeft, parsed.size()}; parameter.abiType = ABIType{ABIType::String, ABIType::AlignLeft, parsed.size()};
@ -364,7 +364,7 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::Failure, true)) else if (accept(Token::Failure, true))
{ {
if (isSigned) if (isSigned)
throw Error(Error::Type::ParserError, "Invalid failure literal."); throw TestParserError("Invalid failure literal.");
parameter.abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0}; parameter.abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0};
parameter.rawBytes = bytes{}; parameter.rawBytes = bytes{};
@ -555,10 +555,7 @@ void TestFileParser::Scanner::scanNextToken()
else if (isEndOfLine()) else if (isEndOfLine())
token = make_pair(Token::EOS, "EOS"); token = make_pair(Token::EOS, "EOS");
else else
throw Error( throw TestParserError("Unexpected character: '" + string{current()} + "'");
Error::Type::ParserError,
"Unexpected character: '" + string{current()} + "'"
);
break; break;
} }
} }
@ -651,7 +648,7 @@ string TestFileParser::Scanner::scanString()
str += scanHexPart(); str += scanHexPart();
break; break;
default: default:
throw Error(Error::Type::ParserError, "Invalid or escape sequence found in string literal."); throw TestParserError("Invalid or escape sequence found in string literal.");
} }
} }
else else
@ -673,7 +670,7 @@ char TestFileParser::Scanner::scanHexPart()
else if (tolower(current()) >= 'a' && tolower(current()) <= 'f') else if (tolower(current()) >= 'a' && tolower(current()) <= 'f')
value = tolower(current()) - 'a' + 10; value = tolower(current()) - 'a' + 10;
else else
throw Error(Error::Type::ParserError, "\\x used with no following hex digits."); throw TestParserError("\\x used with no following hex digits.");
advance(); advance();
if (current() == '"') if (current() == '"')

View File

@ -25,6 +25,7 @@
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
#include <test/ExecutionFramework.h> #include <test/ExecutionFramework.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/libsolidity/util/TestFileParser.h> #include <test/libsolidity/util/TestFileParser.h>
using namespace std; using namespace std;
@ -365,7 +366,7 @@ BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid1)
char const* source = R"( char const* source = R"(
// f(uint256): "\x" -> // f(uint256): "\x" ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid2) BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid2)
@ -383,7 +384,7 @@ BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid3)
char const* source = R"( char const* source = R"(
// f(uint256): "\xZ" -> // f(uint256): "\xZ" ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid4) BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid4)
@ -391,7 +392,7 @@ BOOST_AUTO_TEST_CASE(scanner_hex_values_invalid4)
char const* source = R"( char const* source = R"(
// f(uint256): "\xZZ" -> // f(uint256): "\xZZ" ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_hex_string) BOOST_AUTO_TEST_CASE(call_arguments_hex_string)
@ -741,7 +742,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_hex_string_left_align)
char const* source = R"( char const* source = R"(
// f(bytes): left(hex"4200ef") -> // f(bytes): left(hex"4200ef") ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_hex_string_right_align) BOOST_AUTO_TEST_CASE(call_arguments_hex_string_right_align)
@ -749,7 +750,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_hex_string_right_align)
char const* source = R"( char const* source = R"(
// f(bytes): right(hex"4200ef") -> // f(bytes): right(hex"4200ef") ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_newline_invalid) BOOST_AUTO_TEST_CASE(call_newline_invalid)
@ -757,7 +758,7 @@ BOOST_AUTO_TEST_CASE(call_newline_invalid)
char const* source = R"( char const* source = R"(
/ /
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_invalid) BOOST_AUTO_TEST_CASE(call_invalid)
@ -765,7 +766,7 @@ BOOST_AUTO_TEST_CASE(call_invalid)
char const* source = R"( char const* source = R"(
/ f() -> / f() ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signature_invalid) BOOST_AUTO_TEST_CASE(call_signature_invalid)
@ -773,7 +774,7 @@ BOOST_AUTO_TEST_CASE(call_signature_invalid)
char const* source = R"( char const* source = R"(
// f(uint8,) -> FAILURE // f(uint8,) -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid) BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid)
@ -781,7 +782,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid)
char const* source = R"( char const* source = R"(
// f((uint8,) -> FAILURE // f((uint8,) -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_empty) BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_empty)
@ -789,7 +790,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_empty)
char const* source = R"( char const* source = R"(
// f(uint8, ()) -> FAILURE // f(uint8, ()) -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_parantheses) BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_parantheses)
@ -797,14 +798,14 @@ BOOST_AUTO_TEST_CASE(call_arguments_tuple_invalid_parantheses)
char const* source = R"( char const* source = R"(
// f((uint8,() -> FAILURE // f((uint8,() -> FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_value_expectations_missing) BOOST_AUTO_TEST_CASE(call_ether_value_expectations_missing)
{ {
char const* source = R"( char const* source = R"(
// f(), 0)"; // f(), 0)";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_invalid) BOOST_AUTO_TEST_CASE(call_arguments_invalid)
@ -812,7 +813,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_invalid)
char const* source = R"( char const* source = R"(
// f(uint256): abc -> 1 // f(uint256): abc -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_invalid_decimal) BOOST_AUTO_TEST_CASE(call_arguments_invalid_decimal)
@ -820,7 +821,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_invalid_decimal)
char const* source = R"( char const* source = R"(
// sig(): 0.h3 -> // sig(): 0.h3 ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_value_invalid) BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
@ -828,7 +829,7 @@ BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
char const* source = R"( char const* source = R"(
// f(uint256), abc : 1 -> 1 // f(uint256), abc : 1 -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_value_invalid_decimal) BOOST_AUTO_TEST_CASE(call_ether_value_invalid_decimal)
@ -836,7 +837,7 @@ BOOST_AUTO_TEST_CASE(call_ether_value_invalid_decimal)
char const* source = R"( char const* source = R"(
// sig(): 0.1hd ether -> // sig(): 0.1hd ether ->
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_ether_type_invalid) BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
@ -844,7 +845,7 @@ BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
char const* source = R"( char const* source = R"(
// f(uint256), 2 btc : 1 -> 1 // f(uint256), 2 btc : 1 -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signed_bool_invalid) BOOST_AUTO_TEST_CASE(call_signed_bool_invalid)
@ -852,7 +853,7 @@ BOOST_AUTO_TEST_CASE(call_signed_bool_invalid)
char const* source = R"( char const* source = R"(
// f() -> -true // f() -> -true
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signed_failure_invalid) BOOST_AUTO_TEST_CASE(call_signed_failure_invalid)
@ -860,7 +861,7 @@ BOOST_AUTO_TEST_CASE(call_signed_failure_invalid)
char const* source = R"( char const* source = R"(
// f() -> -FAILURE // f() -> -FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_signed_hex_number_invalid) BOOST_AUTO_TEST_CASE(call_signed_hex_number_invalid)
@ -868,7 +869,7 @@ BOOST_AUTO_TEST_CASE(call_signed_hex_number_invalid)
char const* source = R"( char const* source = R"(
// f() -> -0x42 // f() -> -0x42
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_colon) BOOST_AUTO_TEST_CASE(call_arguments_colon)
@ -877,7 +878,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_colon)
// h256(): // h256():
// -> 1 // -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arguments_newline_colon) BOOST_AUTO_TEST_CASE(call_arguments_newline_colon)
@ -887,7 +888,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_newline_colon)
// : // :
// -> 1 // -> 1
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_arrow_missing) BOOST_AUTO_TEST_CASE(call_arrow_missing)
@ -895,7 +896,7 @@ BOOST_AUTO_TEST_CASE(call_arrow_missing)
char const* source = R"( char const* source = R"(
// h256() FAILURE // h256() FAILURE
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(call_unexpected_character) BOOST_AUTO_TEST_CASE(call_unexpected_character)
@ -903,7 +904,7 @@ BOOST_AUTO_TEST_CASE(call_unexpected_character)
char const* source = R"( char const* source = R"(
// f() -> ?? // f() -> ??
)"; )";
BOOST_REQUIRE_THROW(parse(source), langutil::Error); BOOST_REQUIRE_THROW(parse(source), TestParserError);
} }
BOOST_AUTO_TEST_CASE(constructor) BOOST_AUTO_TEST_CASE(constructor)