Adds multi-line support for test file parser.

This commit is contained in:
Erik Kundt 2019-02-01 06:29:34 +01:00
parent f90c6f57bb
commit 7fa167977b
3 changed files with 298 additions and 103 deletions

View File

@ -65,33 +65,27 @@ vector<dev::solidity::test::FunctionCall> TestFileParser::parseFunctionCalls()
{
FunctionCall call;
// f()
expect(SoltToken::Newline);
call.signature = parseFunctionSignature();
// f(), 314 ether
if (accept(SoltToken::Comma, true))
call.value = parseFunctionCallValue();
// f(), 314 ether: 1, 1
if (accept(SoltToken::Colon, true))
call.arguments = parseFunctionCallArguments();
string comment = m_scanner.currentLiteral();
if (accept(SoltToken::Comment, true))
call.arguments.comment = comment;
call.displayMode = parseNewline();
call.arguments.comment = parseComment();
// -> 1
expect(SoltToken::Newline);
if (accept(SoltToken::Newline, true))
call.displayMode = FunctionCall::DisplayMode::MultiLine;
expect(SoltToken::Arrow);
if (m_scanner.peekToken() != SoltToken::Newline)
{
call.expectations = parseFunctionCallExpectations();
string comment = m_scanner.currentLiteral();
if (accept(SoltToken::Comment, true))
call.expectations.comment = comment;
}
call.expectations = parseFunctionCallExpectations();
if (accept(SoltToken::Newline, false))
call.displayMode = parseNewline();
call.expectations.comment = parseComment();
calls.emplace_back(std::move(call));
}
else
@ -127,7 +121,8 @@ bool TestFileParser::accept(SoltToken _token, bool const _expect)
bool TestFileParser::expect(SoltToken _token, bool const _advance)
{
if (m_scanner.currentToken() != _token)
throw Error(Error::Type::ParserError,
throw Error
(Error::Type::ParserError,
"Unexpected " + formatToken(m_scanner.currentToken()) + ": \"" +
m_scanner.currentLiteral() + "\". " +
"Expected \"" + formatToken(_token) + "\"."
@ -164,10 +159,7 @@ string TestFileParser::parseFunctionSignature()
u256 TestFileParser::parseFunctionCallValue()
{
u256 value;
string literal = m_scanner.currentLiteral();
expect(SoltToken::Number);
value = convertNumber(literal);
u256 value = convertNumber(parseNumber());
expect(SoltToken::Ether);
return value;
}
@ -176,41 +168,46 @@ FunctionCallArgs TestFileParser::parseFunctionCallArguments()
{
FunctionCallArgs arguments;
auto formattedBytes = parseABITypeLiteral();
arguments.rawBytes += formattedBytes.first;
arguments.formats.emplace_back(std::move(formattedBytes.second));
auto param = parseParameter();
if (param.abiType.type == ABIType::None)
throw Error(Error::Type::ParserError, "No argument provided.");
arguments.parameters.emplace_back(param);
while (accept(SoltToken::Comma, true))
{
auto formattedBytes = parseABITypeLiteral();
arguments.rawBytes += formattedBytes.first;
arguments.formats.emplace_back(std::move(formattedBytes.second));
}
arguments.parameters.emplace_back(parseParameter());
return arguments;
}
FunctionCallExpectations TestFileParser::parseFunctionCallExpectations()
{
FunctionCallExpectations expectations;
string token = m_scanner.currentLiteral();
if (accept(SoltToken::Failure, true))
expectations.status = false;
else
{
auto formattedBytes = parseABITypeLiteral();
expectations.rawBytes += formattedBytes.first;
expectations.formats.emplace_back(std::move(formattedBytes.second));
auto param = parseParameter();
if (param.abiType.type == ABIType::None)
return expectations;
expectations.parameters.emplace_back(param);
while (accept(SoltToken::Comma, true))
{
auto formattedBytes = parseABITypeLiteral();
expectations.rawBytes += formattedBytes.first;
expectations.formats.emplace_back(std::move(formattedBytes.second));
}
}
while (accept(SoltToken::Comma, true))
expectations.parameters.emplace_back(parseParameter());
/// We have always one virtual parameter in the parameter list.
/// If its type is FAILURE, the expected result is also a REVERT etc.
if (expectations.parameters.at(0).abiType.type != ABIType::Failure)
expectations.failure = false;
return expectations;
}
Parameter TestFileParser::parseParameter()
{
Parameter parameter;
if (accept(SoltToken::Newline, true))
parameter.format.newline = true;
auto literal = parseABITypeLiteral();
parameter.rawBytes = literal.first;
parameter.abiType = literal.second;
return parameter;
}
pair<bytes, ABIType> TestFileParser::parseABITypeLiteral()
{
try
@ -219,21 +216,23 @@ pair<bytes, ABIType> TestFileParser::parseABITypeLiteral()
ABIType abiType;
if (accept(SoltToken::Sub))
{
abiType.type = ABIType::Type::SignedDec;
abiType.size = 32;
abiType = ABIType{ABIType::SignedDec, 32};
expect(SoltToken::Sub);
number = convertNumber(parseNumber()) * -1;
}
else
{
if (accept(SoltToken::Number))
{
abiType.type = ABIType::Type::UnsignedDec;
abiType.size = 32;
abiType = ABIType{ABIType::UnsignedDec, 32};
number = convertNumber(parseNumber());
}
if (accept(SoltToken::Failure, true))
{
abiType = ABIType{ABIType::Failure, 0};
return make_pair(bytes{}, abiType);
}
}
return make_pair(toBigEndian(number), abiType);
}
catch (std::exception const&)
@ -242,6 +241,21 @@ pair<bytes, ABIType> TestFileParser::parseABITypeLiteral()
}
}
solidity::test::FunctionCall::DisplayMode TestFileParser::parseNewline()
{
if (accept(SoltToken::Newline, true))
return FunctionCall::DisplayMode::MultiLine;
return FunctionCall::DisplayMode::SingleLine;
}
string TestFileParser::parseComment()
{
string comment = m_scanner.currentLiteral();
if (accept(SoltToken::Comment, true))
return comment;
return string{};
}
string TestFileParser::parseNumber()
{
string literal = m_scanner.currentLiteral();
@ -283,7 +297,8 @@ void TestFileParser::Scanner::scanNextToken()
};
TokenDesc token = make_pair(SoltToken::Unknown, "");
do {
do
{
switch(current())
{
case '/':

View File

@ -19,6 +19,7 @@
#include <liblangutil/Exceptions.h>
#include <iosfwd>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>
@ -53,7 +54,7 @@ namespace test
T(Arrow, "->", 0) \
T(Newline, "//", 0) \
/* Literals & identifier */ \
T(Comment, "comment", 0) \
T(Comment, "#", 0) \
T(Number, "number", 0) \
T(Identifier, "identifier", 0) \
/* type keywords */ \
@ -62,7 +63,6 @@ namespace test
/* special keywords */ \
K(Failure, "FAILURE", 0) \
enum class SoltToken : unsigned int {
#define T(name, string, precedence) name,
SOLT_TOKEN_LIST(T, T)
@ -82,16 +82,47 @@ struct ABIType
enum Type {
UnsignedDec,
SignedDec,
Invalid
Failure,
None
};
ABIType(): type(ABIType::Invalid), size(0) { }
ABIType(): type(ABIType::None), size(0) { }
ABIType(Type _type, size_t _size): type(_type), size(_size) { }
Type type;
size_t size;
};
using ABITypeList = std::vector<ABIType>;
/**
* Helper that can hold format information retrieved
* while scanning through a parameter list in sol_t.
*/
struct FormatInfo
{
bool newline;
};
/**
* Parameter abstraction used for the encoding and decoding
* function parameter lists and expectation lists.
* A parameter list is usually a comma-separated list of literals.
* It should not be possible to call create a parameter holding
* an identifier, but if so, the ABI type would be invalid.
*/
struct Parameter
{
/// ABI encoded `bytes` of parsed expectations. This `bytes`
/// is compared to the actual result of a function call
/// and is taken into account while validating it.
bytes rawBytes;
/// Types that were used to encode `rawBytes`. Expectations
/// are usually comma separated literals. Their type is auto-
/// detected and retained in order to format them later on.
ABIType abiType;
/// Format info attached to the parameter. It handles newlines given
/// in the declaration of it.
FormatInfo format;
};
using ParameterList = std::vector<Parameter>;
/**
* Represents the expected result of a function call after it has been executed. This may be a single
@ -102,20 +133,24 @@ using ABITypeList = std::vector<ABIType>;
*/
struct FunctionCallExpectations
{
/// ABI encoded `bytes` of parsed expectations. This `bytes`
/// is compared to the actual result of a function call
/// and is taken into account while validating it.
bytes rawBytes;
/// Types that were used to encode `rawBytes`. Expectations
/// are usually comma seperated literals. Their type is auto-
/// detected and retained in order to format them later on.
ABITypeList formats;
/// Representation of the comma-separated (or empty) list of expectation parameters given
/// to a function call.
ParameterList parameters;
/// Expected status of the transaction. It can be either
/// a REVERT or a different EVM failure (e.g. out-of-gas).
bool status = true;
bool failure = true;
/// A Comment that can be attached to the expectations,
/// that is retained and can be displayed.
std::string comment;
/// ABI encoded `bytes` of parsed parameters. This `bytes`
/// passed to the function call.
bytes rawBytes() const
{
bytes raw;
for (auto const& param: parameters)
raw += param.rawBytes;
return raw;
}
};
/**
@ -126,16 +161,22 @@ struct FunctionCallExpectations
*/
struct FunctionCallArgs
{
/// ABI encoded `bytes` of parsed parameters. This `bytes`
/// passed to the function call.
bytes rawBytes;
/// Types that were used to encode `rawBytes`. Parameters
/// are usually comma seperated literals. Their type is auto-
/// are usually comma separated literals. Their type is auto-
/// detected and retained in order to format them later on.
ABITypeList formats;
ParameterList parameters;
/// A Comment that can be attached to the expectations,
/// that is retained and can be displayed.
std::string comment;
/// ABI encoded `bytes` of parsed parameters. This `bytes`
/// passed to the function call.
bytes rawBytes() const
{
bytes raw;
for (auto const& param: parameters)
raw += param.rawBytes;
return raw;
}
};
/**
@ -156,6 +197,16 @@ struct FunctionCall
/// They are checked against the actual results and their
/// `bytes` representation, as well as the transaction status.
FunctionCallExpectations expectations;
/// single / multi-line mode will be detected as follows:
/// every newline (//) in source results in a function call
/// that has its display mode set to multi-mode. Function and
/// result parameter lists are an exception: a single parameter
/// stores a format information that contains a newline definition.
enum DisplayMode {
SingleLine,
MultiLine
};
DisplayMode displayMode = DisplayMode::SingleLine;
};
/**
@ -196,6 +247,7 @@ private:
{
public:
/// Constructor that takes an input stream \param _stream to operate on.
/// It reads all lines into one single line, keeping the newlines.
Scanner(std::istream& _stream) { readStream(_stream); }
/// Reads input stream into a single line and resets the current iterator.
@ -253,19 +305,34 @@ private:
/// Parses the expected result of a function call execution.
FunctionCallExpectations parseFunctionCallExpectations();
/// Parses the next parameter in a comma separated list.
/// Takes a newly parsed, and type-annotated `bytes` argument,
/// appends it to the internal `bytes` buffer of the parameter. It can also
/// store newlines found in the source, that are needed to
/// format input and output of the interactive update.
Parameter parseParameter();
/// Parses and converts the current literal to its byte representation and
/// preserves the chosen ABI type. Based on that type information, the driver of
/// this parser can format arguments, expectations and results. Supported types:
/// - unsigned and signed decimal number literals
/// Throws a ParserError if data is encoded incorrectly or
/// - unsigned and signed decimal number literals.
/// Returns invalid ABI type for empty literal. This is needed in order
/// to detect empty expectations. Throws a ParserError if data is encoded incorrectly or
/// if data type is not supported.
std::pair<bytes, ABIType> parseABITypeLiteral();
/// Accepts a newline `//` and returns DisplayMode::MultiLine
/// if found, DisplayMode::SingleLine otherwise.
FunctionCall::DisplayMode parseNewline();
/// Parses a comment
std::string parseComment();
/// Parses the current number literal.
std::string parseNumber();
/// Tries to convert \param _literal to `uint256` and throws if
/// if conversion failed.
/// conversion fails.
u256 convertNumber(std::string const& _literal);
/// A scanner instance

View File

@ -57,44 +57,93 @@ BOOST_AUTO_TEST_CASE(simple_call_succees)
char const* source = R"(
// f(uint256, uint256): 1, 1
// ->
// # This call should not return a value, but still succeed. #
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto call = calls.at(0);
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{1}) + toBigEndian(u256{1}));
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "f(uint256,uint256)");
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}) + toBigEndian(u256{1}));
ABI_CHECK(call.expectations.rawBytes(), bytes{});
}
BOOST_AUTO_TEST_CASE(simple_single_line_call_success)
{
char const* source = R"(
// f(uint256): 1 -> # Does not expect a value. #
// f(uint256): 1 -> 1 # Expect return value. #
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 2);
auto call = calls.at(0);
BOOST_CHECK_EQUAL(call.signature, "f(uint256)");
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::SingleLine);
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}));
ABI_CHECK(call.expectations.rawBytes(), bytes{});
call = calls.at(1);
BOOST_CHECK_EQUAL(call.signature, "f(uint256)");
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::SingleLine);
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}));
ABI_CHECK(call.expectations.rawBytes(), toBigEndian(u256{1}));
}
BOOST_AUTO_TEST_CASE(non_existent_call_revert_single_line)
{
char const* source = R"(
// i_am_not_there() -> FAILURE
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::SingleLine);
BOOST_CHECK_EQUAL(call.signature, "i_am_not_there()");
BOOST_CHECK_EQUAL(call.expectations.failure, true);
BOOST_CHECK_EQUAL(call.expectations.parameters.at(0).abiType.type, ABIType::Failure);
ABI_CHECK(call.expectations.rawBytes(), bytes{});
}
BOOST_AUTO_TEST_CASE(non_existent_call_revert)
{
char const* source = R"(
// i_am_not_there()
// -> FAILURE
// -> FAILURE # This is can be either REVERT or a different EVM failure #
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "i_am_not_there()");
BOOST_CHECK_EQUAL(call.expectations.status, false);
BOOST_CHECK_EQUAL(call.expectations.parameters.at(0).abiType.type, ABIType::Failure);
ABI_CHECK(call.expectations.rawBytes(), bytes{});
BOOST_CHECK_EQUAL(call.expectations.failure, true);
}
BOOST_AUTO_TEST_CASE(call_comments)
{
char const* source = R"(
// f() # This is a comment #
// -> 1 # This is another comment #
// f() # Parameter comment # -> 1 # Expectation comment #
// f() # Parameter comment #
// -> 1 # Expectation comment #
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
BOOST_CHECK_EQUAL(calls.size(), 2);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.signature, "f()");
BOOST_CHECK_EQUAL(call.arguments.comment, " This is a comment ");
BOOST_CHECK_EQUAL(call.expectations.comment, " This is another comment ");
ABI_CHECK(call.expectations.rawBytes, toBigEndian(u256{1}));
BOOST_CHECK_EQUAL(calls.at(0).displayMode, FunctionCall::DisplayMode::SingleLine);
BOOST_CHECK_EQUAL(calls.at(1).displayMode, FunctionCall::DisplayMode::MultiLine);
for (auto const& call: calls)
{
BOOST_CHECK_EQUAL(call.signature, "f()");
BOOST_CHECK_EQUAL(call.arguments.comment, " Parameter comment ");
BOOST_CHECK_EQUAL(call.expectations.comment, " Expectation comment ");
ABI_CHECK(call.expectations.rawBytes(), toBigEndian(u256{1}));
}
}
BOOST_AUTO_TEST_CASE(call_arguments)
@ -107,10 +156,44 @@ BOOST_AUTO_TEST_CASE(call_arguments)
BOOST_CHECK_EQUAL(calls.size(), 1);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "f(uint256)");
BOOST_CHECK_EQUAL(call.value, u256{314});
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{5}));
ABI_CHECK(call.expectations.rawBytes, toBigEndian(u256{4}));
BOOST_CHECK_EQUAL(call.expectations.failure, false);
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{5}));
ABI_CHECK(call.expectations.rawBytes(), toBigEndian(u256{4}));
}
BOOST_AUTO_TEST_CASE(call_expectations_empty_single_line)
{
char const* source = R"(
// _exp_() ->
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::SingleLine);
BOOST_CHECK_EQUAL(call.signature, "_exp_()");
ABI_CHECK(call.arguments.rawBytes(), bytes{});
ABI_CHECK(call.expectations.rawBytes(), bytes{});
}
BOOST_AUTO_TEST_CASE(call_expectations_empty_multiline)
{
char const* source = R"(
// _exp_()
// ->
// # This call should not return a value, but still succeed. #
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "_exp_()");
ABI_CHECK(call.arguments.rawBytes(), bytes{});
ABI_CHECK(call.expectations.rawBytes(), bytes{});
}
BOOST_AUTO_TEST_CASE(call_expectations_missing)
@ -130,8 +213,7 @@ BOOST_AUTO_TEST_CASE(call_ether_value_expectations_missing)
BOOST_AUTO_TEST_CASE(call_arguments_invalid)
{
char const* source = R"(
// f(uint256): abc
// -> 1
// f(uint256): abc -> 1
)";
BOOST_CHECK_THROW(parse(source), langutil::Error);
}
@ -139,8 +221,7 @@ BOOST_AUTO_TEST_CASE(call_arguments_invalid)
BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
{
char const* source = R"(
// f(uint256), abc : 1
// -> 1
// f(uint256), abc : 1 -> 1
)";
BOOST_CHECK_THROW(parse(source), langutil::Error);
}
@ -148,8 +229,7 @@ BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
{
char const* source = R"(
// f(uint256), 2 btc : 1
// -> 1
// f(uint256), 2 btc : 1 -> 1
)";
BOOST_CHECK_THROW(parse(source), langutil::Error);
}
@ -157,47 +237,80 @@ BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
BOOST_AUTO_TEST_CASE(call_arguments_mismatch)
{
char const* source = R"(
// f(uint256, uint256): 1 # This only throws at runtime #
// f(uint256):
// 1, 2
// # This only throws at runtime #
// -> 1
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.signature, "f(uint256,uint256)");
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{1}));
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "f(uint256)");
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}) + toBigEndian(u256{2}));
BOOST_CHECK_EQUAL(call.expectations.failure, false);
}
BOOST_AUTO_TEST_CASE(call_multiple_arguments)
{
char const* source = R"(
// test(uint256, uint256): 1, 2
// -> 1, 1
// test(uint256, uint256):
// 1,
// 2
// -> 1,
// 1
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "test(uint256,uint256)");
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{1}) + toBigEndian(u256{2}));
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}) + toBigEndian(u256{2}));
BOOST_CHECK_EQUAL(call.expectations.failure, false);
}
BOOST_AUTO_TEST_CASE(call_multiple_arguments_mixed_format)
{
char const* source = R"(
// test(uint256, uint256),314 ether: 1, -2
// test(uint256, uint256), 314 ether:
// 1, -2
// -> -1, 2
)";
auto const calls = parse(source);
BOOST_CHECK_EQUAL(calls.size(), 1);
auto const& call = calls.at(0);
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
BOOST_CHECK_EQUAL(call.signature, "test(uint256,uint256)");
BOOST_CHECK_EQUAL(call.value, u256{314});
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{1}) + toBigEndian(u256{-2}));
ABI_CHECK(call.expectations.rawBytes, toBigEndian(u256{-1}) + toBigEndian(u256{2}));
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}) + toBigEndian(u256{-2}));
BOOST_CHECK_EQUAL(call.expectations.failure, false);
ABI_CHECK(call.expectations.rawBytes(), toBigEndian(u256{-1}) + toBigEndian(u256{2}));
}
BOOST_AUTO_TEST_CASE(call_arguments_colon)
{
char const* source = R"(
// h256():
// -> 1
)";
BOOST_CHECK_THROW(parse(source), langutil::Error);
}
BOOST_AUTO_TEST_CASE(call_arguments_newline_colon)
{
char const* source = R"(
// h256()
// :
// -> 1
)";
BOOST_CHECK_THROW(parse(source), langutil::Error);
}
BOOST_AUTO_TEST_SUITE_END()
}