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")
return bytes{false};
else
throw Error(Error::Type::ParserError, "Boolean literal invalid.");
throw TestParserError("Boolean literal invalid.");
}
bytes BytesUtils::convertNumber(string const& _literal)
@ -92,7 +92,7 @@ bytes BytesUtils::convertNumber(string const& _literal)
}
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&)
{
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&)
{
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)
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
* 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/BytesUtils.h>
#include <test/libsolidity/util/SoltestErrors.h>
#include <test/Common.h>
#include <liblangutil/Common.h>
@ -128,9 +129,9 @@ vector<solidity::frontend::test::FunctionCall> TestFileParser::parseFunctionCall
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)
{
if (m_scanner.currentToken() != _token || m_scanner.currentToken() == Token::Invalid)
throw Error(
Error::Type::ParserError,
throw TestParserError(
"Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" +
m_scanner.currentLiteral() + "\". " +
"Expected \"" + formatToken(_token) + "\"."
@ -187,10 +187,10 @@ pair<string, bool> TestFileParser::parseFunctionSignature()
parameters += parseIdentifierOrTuple();
}
if (accept(Token::Arrow, true))
throw Error(Error::Type::ParserError, "Invalid signature detected: " + signature);
throw TestParserError("Invalid signature detected: " + signature);
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
signature += parameters;
@ -207,7 +207,7 @@ FunctionValue TestFileParser::parseFunctionCallValue()
u256 value{ parseDecimalNumber() };
Token token = m_scanner.currentToken();
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();
@ -216,7 +216,7 @@ FunctionValue TestFileParser::parseFunctionCallValue()
}
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();
if (param.abiType.type == ABIType::None)
throw Error(Error::Type::ParserError, "No argument provided.");
throw TestParserError("No argument provided.");
arguments.parameters.emplace_back(param);
while (accept(Token::Comma, true))
@ -290,7 +290,7 @@ Parameter TestFileParser::parseParameter()
if (accept(Token::Boolean))
{
if (isSigned)
throw Error(Error::Type::ParserError, "Invalid boolean literal.");
throw TestParserError("Invalid boolean literal.");
parameter.abiType = ABIType{ABIType::Boolean, ABIType::AlignRight, 32};
string parsed = parseBoolean();
@ -304,7 +304,7 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::HexNumber))
{
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};
string parsed = parseHexNumber();
@ -318,9 +318,9 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::Hex, true))
{
if (isSigned)
throw Error(Error::Type::ParserError, "Invalid hex string literal.");
throw TestParserError("Invalid hex string literal.");
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();
parameter.rawString += "hex\"" + parsed + "\"";
@ -332,9 +332,9 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::String))
{
if (isSigned)
throw Error(Error::Type::ParserError, "Invalid string literal.");
throw TestParserError("Invalid string literal.");
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();
parameter.abiType = ABIType{ABIType::String, ABIType::AlignLeft, parsed.size()};
@ -364,7 +364,7 @@ Parameter TestFileParser::parseParameter()
else if (accept(Token::Failure, true))
{
if (isSigned)
throw Error(Error::Type::ParserError, "Invalid failure literal.");
throw TestParserError("Invalid failure literal.");
parameter.abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0};
parameter.rawBytes = bytes{};
@ -555,10 +555,7 @@ void TestFileParser::Scanner::scanNextToken()
else if (isEndOfLine())
token = make_pair(Token::EOS, "EOS");
else
throw Error(
Error::Type::ParserError,
"Unexpected character: '" + string{current()} + "'"
);
throw TestParserError("Unexpected character: '" + string{current()} + "'");
break;
}
}
@ -651,7 +648,7 @@ string TestFileParser::Scanner::scanString()
str += scanHexPart();
break;
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
@ -673,7 +670,7 @@ char TestFileParser::Scanner::scanHexPart()
else if (tolower(current()) >= 'a' && tolower(current()) <= 'f')
value = tolower(current()) - 'a' + 10;
else
throw Error(Error::Type::ParserError, "\\x used with no following hex digits.");
throw TestParserError("\\x used with no following hex digits.");
advance();
if (current() == '"')

View File

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