mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Adds multi-line support for test file parser.
This commit is contained in:
parent
f90c6f57bb
commit
7fa167977b
@ -65,33 +65,27 @@ vector<dev::solidity::test::FunctionCall> TestFileParser::parseFunctionCalls()
|
|||||||
{
|
{
|
||||||
FunctionCall call;
|
FunctionCall call;
|
||||||
|
|
||||||
// f()
|
|
||||||
expect(SoltToken::Newline);
|
expect(SoltToken::Newline);
|
||||||
call.signature = parseFunctionSignature();
|
call.signature = parseFunctionSignature();
|
||||||
|
|
||||||
// f(), 314 ether
|
|
||||||
if (accept(SoltToken::Comma, true))
|
if (accept(SoltToken::Comma, true))
|
||||||
call.value = parseFunctionCallValue();
|
call.value = parseFunctionCallValue();
|
||||||
|
|
||||||
// f(), 314 ether: 1, 1
|
|
||||||
if (accept(SoltToken::Colon, true))
|
if (accept(SoltToken::Colon, true))
|
||||||
call.arguments = parseFunctionCallArguments();
|
call.arguments = parseFunctionCallArguments();
|
||||||
|
|
||||||
string comment = m_scanner.currentLiteral();
|
call.displayMode = parseNewline();
|
||||||
if (accept(SoltToken::Comment, true))
|
call.arguments.comment = parseComment();
|
||||||
call.arguments.comment = comment;
|
|
||||||
|
|
||||||
// -> 1
|
if (accept(SoltToken::Newline, true))
|
||||||
expect(SoltToken::Newline);
|
call.displayMode = FunctionCall::DisplayMode::MultiLine;
|
||||||
expect(SoltToken::Arrow);
|
expect(SoltToken::Arrow);
|
||||||
if (m_scanner.peekToken() != SoltToken::Newline)
|
|
||||||
{
|
|
||||||
call.expectations = parseFunctionCallExpectations();
|
|
||||||
|
|
||||||
string comment = m_scanner.currentLiteral();
|
call.expectations = parseFunctionCallExpectations();
|
||||||
if (accept(SoltToken::Comment, true))
|
|
||||||
call.expectations.comment = comment;
|
if (accept(SoltToken::Newline, false))
|
||||||
}
|
call.displayMode = parseNewline();
|
||||||
|
call.expectations.comment = parseComment();
|
||||||
|
|
||||||
calls.emplace_back(std::move(call));
|
calls.emplace_back(std::move(call));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -127,7 +121,8 @@ bool TestFileParser::accept(SoltToken _token, bool const _expect)
|
|||||||
bool TestFileParser::expect(SoltToken _token, bool const _advance)
|
bool TestFileParser::expect(SoltToken _token, bool const _advance)
|
||||||
{
|
{
|
||||||
if (m_scanner.currentToken() != _token)
|
if (m_scanner.currentToken() != _token)
|
||||||
throw Error(Error::Type::ParserError,
|
throw Error
|
||||||
|
(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) + "\"."
|
||||||
@ -164,10 +159,7 @@ string TestFileParser::parseFunctionSignature()
|
|||||||
|
|
||||||
u256 TestFileParser::parseFunctionCallValue()
|
u256 TestFileParser::parseFunctionCallValue()
|
||||||
{
|
{
|
||||||
u256 value;
|
u256 value = convertNumber(parseNumber());
|
||||||
string literal = m_scanner.currentLiteral();
|
|
||||||
expect(SoltToken::Number);
|
|
||||||
value = convertNumber(literal);
|
|
||||||
expect(SoltToken::Ether);
|
expect(SoltToken::Ether);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@ -176,41 +168,46 @@ FunctionCallArgs TestFileParser::parseFunctionCallArguments()
|
|||||||
{
|
{
|
||||||
FunctionCallArgs arguments;
|
FunctionCallArgs arguments;
|
||||||
|
|
||||||
auto formattedBytes = parseABITypeLiteral();
|
auto param = parseParameter();
|
||||||
arguments.rawBytes += formattedBytes.first;
|
if (param.abiType.type == ABIType::None)
|
||||||
arguments.formats.emplace_back(std::move(formattedBytes.second));
|
throw Error(Error::Type::ParserError, "No argument provided.");
|
||||||
|
arguments.parameters.emplace_back(param);
|
||||||
|
|
||||||
while (accept(SoltToken::Comma, true))
|
while (accept(SoltToken::Comma, true))
|
||||||
{
|
arguments.parameters.emplace_back(parseParameter());
|
||||||
auto formattedBytes = parseABITypeLiteral();
|
|
||||||
arguments.rawBytes += formattedBytes.first;
|
|
||||||
arguments.formats.emplace_back(std::move(formattedBytes.second));
|
|
||||||
}
|
|
||||||
return arguments;
|
return arguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
FunctionCallExpectations TestFileParser::parseFunctionCallExpectations()
|
FunctionCallExpectations TestFileParser::parseFunctionCallExpectations()
|
||||||
{
|
{
|
||||||
FunctionCallExpectations expectations;
|
FunctionCallExpectations expectations;
|
||||||
string token = m_scanner.currentLiteral();
|
|
||||||
|
|
||||||
if (accept(SoltToken::Failure, true))
|
auto param = parseParameter();
|
||||||
expectations.status = false;
|
if (param.abiType.type == ABIType::None)
|
||||||
else
|
return expectations;
|
||||||
{
|
expectations.parameters.emplace_back(param);
|
||||||
auto formattedBytes = parseABITypeLiteral();
|
|
||||||
expectations.rawBytes += formattedBytes.first;
|
|
||||||
expectations.formats.emplace_back(std::move(formattedBytes.second));
|
|
||||||
|
|
||||||
while (accept(SoltToken::Comma, true))
|
while (accept(SoltToken::Comma, true))
|
||||||
{
|
expectations.parameters.emplace_back(parseParameter());
|
||||||
auto formattedBytes = parseABITypeLiteral();
|
|
||||||
expectations.rawBytes += formattedBytes.first;
|
/// We have always one virtual parameter in the parameter list.
|
||||||
expectations.formats.emplace_back(std::move(formattedBytes.second));
|
/// 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;
|
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()
|
pair<bytes, ABIType> TestFileParser::parseABITypeLiteral()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -219,21 +216,23 @@ pair<bytes, ABIType> TestFileParser::parseABITypeLiteral()
|
|||||||
ABIType abiType;
|
ABIType abiType;
|
||||||
if (accept(SoltToken::Sub))
|
if (accept(SoltToken::Sub))
|
||||||
{
|
{
|
||||||
abiType.type = ABIType::Type::SignedDec;
|
abiType = ABIType{ABIType::SignedDec, 32};
|
||||||
abiType.size = 32;
|
|
||||||
|
|
||||||
expect(SoltToken::Sub);
|
expect(SoltToken::Sub);
|
||||||
number = convertNumber(parseNumber()) * -1;
|
number = convertNumber(parseNumber()) * -1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
if (accept(SoltToken::Number))
|
if (accept(SoltToken::Number))
|
||||||
{
|
{
|
||||||
abiType.type = ABIType::Type::UnsignedDec;
|
abiType = ABIType{ABIType::UnsignedDec, 32};
|
||||||
abiType.size = 32;
|
|
||||||
|
|
||||||
number = convertNumber(parseNumber());
|
number = convertNumber(parseNumber());
|
||||||
}
|
}
|
||||||
|
if (accept(SoltToken::Failure, true))
|
||||||
|
{
|
||||||
|
abiType = ABIType{ABIType::Failure, 0};
|
||||||
|
return make_pair(bytes{}, abiType);
|
||||||
|
}
|
||||||
|
}
|
||||||
return make_pair(toBigEndian(number), abiType);
|
return make_pair(toBigEndian(number), abiType);
|
||||||
}
|
}
|
||||||
catch (std::exception const&)
|
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 TestFileParser::parseNumber()
|
||||||
{
|
{
|
||||||
string literal = m_scanner.currentLiteral();
|
string literal = m_scanner.currentLiteral();
|
||||||
@ -283,7 +297,8 @@ void TestFileParser::Scanner::scanNextToken()
|
|||||||
};
|
};
|
||||||
|
|
||||||
TokenDesc token = make_pair(SoltToken::Unknown, "");
|
TokenDesc token = make_pair(SoltToken::Unknown, "");
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
switch(current())
|
switch(current())
|
||||||
{
|
{
|
||||||
case '/':
|
case '/':
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
#include <liblangutil/Exceptions.h>
|
#include <liblangutil/Exceptions.h>
|
||||||
|
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
|
#include <numeric>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -53,7 +54,7 @@ namespace test
|
|||||||
T(Arrow, "->", 0) \
|
T(Arrow, "->", 0) \
|
||||||
T(Newline, "//", 0) \
|
T(Newline, "//", 0) \
|
||||||
/* Literals & identifier */ \
|
/* Literals & identifier */ \
|
||||||
T(Comment, "comment", 0) \
|
T(Comment, "#", 0) \
|
||||||
T(Number, "number", 0) \
|
T(Number, "number", 0) \
|
||||||
T(Identifier, "identifier", 0) \
|
T(Identifier, "identifier", 0) \
|
||||||
/* type keywords */ \
|
/* type keywords */ \
|
||||||
@ -62,7 +63,6 @@ namespace test
|
|||||||
/* special keywords */ \
|
/* special keywords */ \
|
||||||
K(Failure, "FAILURE", 0) \
|
K(Failure, "FAILURE", 0) \
|
||||||
|
|
||||||
|
|
||||||
enum class SoltToken : unsigned int {
|
enum class SoltToken : unsigned int {
|
||||||
#define T(name, string, precedence) name,
|
#define T(name, string, precedence) name,
|
||||||
SOLT_TOKEN_LIST(T, T)
|
SOLT_TOKEN_LIST(T, T)
|
||||||
@ -82,16 +82,47 @@ struct ABIType
|
|||||||
enum Type {
|
enum Type {
|
||||||
UnsignedDec,
|
UnsignedDec,
|
||||||
SignedDec,
|
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) { }
|
ABIType(Type _type, size_t _size): type(_type), size(_size) { }
|
||||||
|
|
||||||
Type type;
|
Type type;
|
||||||
size_t size;
|
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
|
* 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
|
struct FunctionCallExpectations
|
||||||
{
|
{
|
||||||
/// ABI encoded `bytes` of parsed expectations. This `bytes`
|
/// Representation of the comma-separated (or empty) list of expectation parameters given
|
||||||
/// is compared to the actual result of a function call
|
/// to a function call.
|
||||||
/// and is taken into account while validating it.
|
ParameterList parameters;
|
||||||
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;
|
|
||||||
/// Expected status of the transaction. It can be either
|
/// Expected status of the transaction. It can be either
|
||||||
/// a REVERT or a different EVM failure (e.g. out-of-gas).
|
/// 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,
|
/// A Comment that can be attached to the expectations,
|
||||||
/// that is retained and can be displayed.
|
/// that is retained and can be displayed.
|
||||||
std::string comment;
|
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
|
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
|
/// 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.
|
/// detected and retained in order to format them later on.
|
||||||
ABITypeList formats;
|
ParameterList parameters;
|
||||||
/// A Comment that can be attached to the expectations,
|
/// A Comment that can be attached to the expectations,
|
||||||
/// that is retained and can be displayed.
|
/// that is retained and can be displayed.
|
||||||
std::string comment;
|
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
|
/// They are checked against the actual results and their
|
||||||
/// `bytes` representation, as well as the transaction status.
|
/// `bytes` representation, as well as the transaction status.
|
||||||
FunctionCallExpectations expectations;
|
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:
|
public:
|
||||||
/// Constructor that takes an input stream \param _stream to operate on.
|
/// 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); }
|
Scanner(std::istream& _stream) { readStream(_stream); }
|
||||||
|
|
||||||
/// Reads input stream into a single line and resets the current iterator.
|
/// 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.
|
/// Parses the expected result of a function call execution.
|
||||||
FunctionCallExpectations parseFunctionCallExpectations();
|
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
|
/// Parses and converts the current literal to its byte representation and
|
||||||
/// preserves the chosen ABI type. Based on that type information, the driver of
|
/// preserves the chosen ABI type. Based on that type information, the driver of
|
||||||
/// this parser can format arguments, expectations and results. Supported types:
|
/// this parser can format arguments, expectations and results. Supported types:
|
||||||
/// - unsigned and signed decimal number literals
|
/// - unsigned and signed decimal number literals.
|
||||||
/// Throws a ParserError if data is encoded incorrectly or
|
/// 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.
|
/// if data type is not supported.
|
||||||
std::pair<bytes, ABIType> parseABITypeLiteral();
|
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.
|
/// Parses the current number literal.
|
||||||
std::string parseNumber();
|
std::string parseNumber();
|
||||||
|
|
||||||
/// Tries to convert \param _literal to `uint256` and throws if
|
/// Tries to convert \param _literal to `uint256` and throws if
|
||||||
/// if conversion failed.
|
/// conversion fails.
|
||||||
u256 convertNumber(std::string const& _literal);
|
u256 convertNumber(std::string const& _literal);
|
||||||
|
|
||||||
/// A scanner instance
|
/// A scanner instance
|
||||||
|
@ -57,44 +57,93 @@ BOOST_AUTO_TEST_CASE(simple_call_succees)
|
|||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// f(uint256, uint256): 1, 1
|
// f(uint256, uint256): 1, 1
|
||||||
// ->
|
// ->
|
||||||
|
// # This call should not return a value, but still succeed. #
|
||||||
)";
|
)";
|
||||||
|
|
||||||
auto const calls = parse(source);
|
auto const calls = parse(source);
|
||||||
BOOST_CHECK_EQUAL(calls.size(), 1);
|
BOOST_CHECK_EQUAL(calls.size(), 1);
|
||||||
|
|
||||||
auto call = calls.at(0);
|
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)");
|
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)
|
BOOST_AUTO_TEST_CASE(non_existent_call_revert)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// i_am_not_there()
|
// i_am_not_there()
|
||||||
// -> FAILURE
|
// -> FAILURE # This is can be either REVERT or a different EVM failure #
|
||||||
)";
|
)";
|
||||||
auto const calls = parse(source);
|
auto const calls = parse(source);
|
||||||
BOOST_CHECK_EQUAL(calls.size(), 1);
|
BOOST_CHECK_EQUAL(calls.size(), 1);
|
||||||
|
|
||||||
auto const& call = calls.at(0);
|
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.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)
|
BOOST_AUTO_TEST_CASE(call_comments)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// f() # This is a comment #
|
// f() # Parameter comment # -> 1 # Expectation comment #
|
||||||
// -> 1 # This is another comment #
|
// f() # Parameter comment #
|
||||||
|
// -> 1 # Expectation comment #
|
||||||
)";
|
)";
|
||||||
auto const calls = parse(source);
|
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(calls.at(0).displayMode, FunctionCall::DisplayMode::SingleLine);
|
||||||
BOOST_CHECK_EQUAL(call.signature, "f()");
|
BOOST_CHECK_EQUAL(calls.at(1).displayMode, FunctionCall::DisplayMode::MultiLine);
|
||||||
BOOST_CHECK_EQUAL(call.arguments.comment, " This is a comment ");
|
|
||||||
BOOST_CHECK_EQUAL(call.expectations.comment, " This is another comment ");
|
for (auto const& call: calls)
|
||||||
ABI_CHECK(call.expectations.rawBytes, toBigEndian(u256{1}));
|
{
|
||||||
|
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)
|
BOOST_AUTO_TEST_CASE(call_arguments)
|
||||||
@ -107,10 +156,44 @@ BOOST_AUTO_TEST_CASE(call_arguments)
|
|||||||
BOOST_CHECK_EQUAL(calls.size(), 1);
|
BOOST_CHECK_EQUAL(calls.size(), 1);
|
||||||
|
|
||||||
auto const& call = calls.at(0);
|
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.signature, "f(uint256)");
|
||||||
BOOST_CHECK_EQUAL(call.value, u256{314});
|
BOOST_CHECK_EQUAL(call.value, u256{314});
|
||||||
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{5}));
|
BOOST_CHECK_EQUAL(call.expectations.failure, false);
|
||||||
ABI_CHECK(call.expectations.rawBytes, toBigEndian(u256{4}));
|
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)
|
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)
|
BOOST_AUTO_TEST_CASE(call_arguments_invalid)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// f(uint256): abc
|
// f(uint256): abc -> 1
|
||||||
// -> 1
|
|
||||||
)";
|
)";
|
||||||
BOOST_CHECK_THROW(parse(source), langutil::Error);
|
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)
|
BOOST_AUTO_TEST_CASE(call_ether_value_invalid)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// f(uint256), abc : 1
|
// f(uint256), abc : 1 -> 1
|
||||||
// -> 1
|
|
||||||
)";
|
)";
|
||||||
BOOST_CHECK_THROW(parse(source), langutil::Error);
|
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)
|
BOOST_AUTO_TEST_CASE(call_ether_type_invalid)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// f(uint256), 2 btc : 1
|
// f(uint256), 2 btc : 1 -> 1
|
||||||
// -> 1
|
|
||||||
)";
|
)";
|
||||||
BOOST_CHECK_THROW(parse(source), langutil::Error);
|
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)
|
BOOST_AUTO_TEST_CASE(call_arguments_mismatch)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// f(uint256, uint256): 1 # This only throws at runtime #
|
// f(uint256):
|
||||||
|
// 1, 2
|
||||||
|
// # This only throws at runtime #
|
||||||
// -> 1
|
// -> 1
|
||||||
)";
|
)";
|
||||||
auto const calls = parse(source);
|
auto const calls = parse(source);
|
||||||
BOOST_CHECK_EQUAL(calls.size(), 1);
|
BOOST_CHECK_EQUAL(calls.size(), 1);
|
||||||
|
|
||||||
auto const& call = calls.at(0);
|
auto const& call = calls.at(0);
|
||||||
BOOST_CHECK_EQUAL(call.signature, "f(uint256,uint256)");
|
BOOST_CHECK_EQUAL(call.displayMode, FunctionCall::DisplayMode::MultiLine);
|
||||||
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{1}));
|
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)
|
BOOST_AUTO_TEST_CASE(call_multiple_arguments)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// test(uint256, uint256): 1, 2
|
// test(uint256, uint256):
|
||||||
// -> 1, 1
|
// 1,
|
||||||
|
// 2
|
||||||
|
// -> 1,
|
||||||
|
// 1
|
||||||
)";
|
)";
|
||||||
auto const calls = parse(source);
|
auto const calls = parse(source);
|
||||||
BOOST_CHECK_EQUAL(calls.size(), 1);
|
BOOST_CHECK_EQUAL(calls.size(), 1);
|
||||||
|
|
||||||
auto const& call = calls.at(0);
|
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.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)
|
BOOST_AUTO_TEST_CASE(call_multiple_arguments_mixed_format)
|
||||||
{
|
{
|
||||||
char const* source = R"(
|
char const* source = R"(
|
||||||
// test(uint256, uint256),314 ether: 1, -2
|
// test(uint256, uint256), 314 ether:
|
||||||
|
// 1, -2
|
||||||
// -> -1, 2
|
// -> -1, 2
|
||||||
)";
|
)";
|
||||||
auto const calls = parse(source);
|
auto const calls = parse(source);
|
||||||
BOOST_CHECK_EQUAL(calls.size(), 1);
|
BOOST_CHECK_EQUAL(calls.size(), 1);
|
||||||
|
|
||||||
auto const& call = calls.at(0);
|
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.signature, "test(uint256,uint256)");
|
||||||
BOOST_CHECK_EQUAL(call.value, u256{314});
|
BOOST_CHECK_EQUAL(call.value, u256{314});
|
||||||
ABI_CHECK(call.arguments.rawBytes, toBigEndian(u256{1}) + toBigEndian(u256{-2}));
|
ABI_CHECK(call.arguments.rawBytes(), toBigEndian(u256{1}) + toBigEndian(u256{-2}));
|
||||||
ABI_CHECK(call.expectations.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()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user