Moves soltest function call to separate class.

This commit is contained in:
Erik Kundt
2019-02-21 16:39:31 +01:00
parent e7a8fed01b
commit d1ad431887
8 changed files with 421 additions and 207 deletions
+4 -4
View File
@@ -195,12 +195,12 @@ tuple<bytes, ABIType, string> TestFileParser::parseABITypeLiteral()
try
{
u256 number{0};
ABIType abiType{ABIType::None, 0};
ABIType abiType{ABIType::None, ABIType::AlignRight, 0};
string rawString;
if (accept(Token::Sub))
{
abiType = ABIType{ABIType::SignedDec, 32};
abiType = ABIType{ABIType::SignedDec, ABIType::AlignRight, 32};
expect(Token::Sub);
rawString += formatToken(Token::Sub);
string parsed = parseNumber();
@@ -211,14 +211,14 @@ tuple<bytes, ABIType, string> TestFileParser::parseABITypeLiteral()
{
if (accept(Token::Number))
{
abiType = ABIType{ABIType::UnsignedDec, 32};
abiType = ABIType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
string parsed = parseNumber();
rawString += parsed;
number = convertNumber(parsed);
}
else if (accept(Token::Failure, true))
{
abiType = ABIType{ABIType::Failure, 0};
abiType = ABIType{ABIType::Failure, ABIType::AlignRight, 0};
return make_tuple(bytes{}, abiType, rawString);
}
}
+10 -1
View File
@@ -98,13 +98,22 @@ namespace soltest
*/
struct ABIType
{
enum Type {
enum Type
{
UnsignedDec,
SignedDec,
Failure,
None
};
enum Align
{
AlignLeft,
AlignRight
};
Type type = ABIType::None;
Align align = Align::AlignRight;
size_t size = 0;
};
+183
View File
@@ -0,0 +1,183 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <test/libsolidity/util/TestFunctionCall.h>
#include <libdevcore/AnsiColorized.h>
#include <stdexcept>
#include <string>
using namespace dev;
using namespace solidity;
using namespace dev::solidity::test;
using namespace std;
string TestFunctionCall::format(string const& _linePrefix, bool const _renderResult, bool const _highlight) const
{
using namespace soltest;
using Token = soltest::Token;
stringstream _stream;
bool highlight = !matchesExpectation() && _highlight;
auto formatOutput = [&](bool const _singleLine)
{
string ws = " ";
string arrow = formatToken(Token::Arrow);
string colon = formatToken(Token::Colon);
string comma = formatToken(Token::Comma);
string comment = formatToken(Token::Comment);
string ether = formatToken(Token::Ether);
string newline = formatToken(Token::Newline);
string failure = formatToken(Token::Failure);
/// Formats the function signature. This is the same independent from the display-mode.
_stream << _linePrefix << newline << ws << m_call.signature;
if (m_call.value > u256(0))
_stream << comma << ws << m_call.value << ws << ether;
if (!m_call.arguments.rawBytes().empty())
{
string output = formatRawParameters(m_call.arguments.parameters, _linePrefix);
_stream << colon << output;
}
/// Formats comments on the function parameters and the arrow taking
/// the display-mode into account.
if (_singleLine)
{
if (!m_call.arguments.comment.empty())
_stream << ws << comment << m_call.arguments.comment << comment;
_stream << ws << arrow << ws;
}
else
{
_stream << endl << _linePrefix << newline << ws;
if (!m_call.arguments.comment.empty())
{
_stream << comment << m_call.arguments.comment << comment;
_stream << endl << _linePrefix << newline << ws;
}
_stream << arrow << ws;
}
/// Format either the expected output or the actual result output
string result;
if (!_renderResult)
{
bytes output = m_call.expectations.rawBytes();
bool const isFailure = m_call.expectations.failure;
result = isFailure ? failure : formatBytesParameters(output, m_call.expectations.result);
}
else
{
bytes output = m_rawBytes;
bool const isFailure = m_failure;
result = isFailure ? failure : formatBytesParameters(output, m_call.expectations.result);
}
AnsiColorized(_stream, highlight, {dev::formatting::RED_BACKGROUND}) << result;
/// Format comments on expectations taking the display-mode into account.
if (_singleLine)
{
if (!m_call.expectations.comment.empty())
_stream << ws << comment << m_call.expectations.comment << comment;
}
else
{
if (!m_call.expectations.comment.empty())
{
_stream << endl << _linePrefix << newline << ws;
_stream << comment << m_call.expectations.comment << comment;
}
}
};
if (m_call.displayMode == FunctionCall::DisplayMode::SingleLine)
formatOutput(true);
else
formatOutput(false);
// _stream << endl;
return _stream.str();
}
string TestFunctionCall::formatBytesParameters(bytes const& _bytes, ParameterList const& _params) const
{
stringstream resultStream;
if (_bytes.empty())
return {};
auto it = _bytes.begin();
for (auto const& param: _params)
{
long offset = static_cast<long>(param.abiType.size);
auto offsetIter = it + offset;
soltestAssert(offsetIter <= _bytes.end(), "Byte range can not be extended past the end of given bytes.");
bytes byteRange{it, offsetIter};
switch (param.abiType.type)
{
case ABIType::SignedDec:
soltestAssert(param.abiType.align == ABIType::AlignRight, "Signed decimals must be right-aligned.");
if (*byteRange.begin() & 0x80)
resultStream << u2s(fromBigEndian<u256>(byteRange));
else
resultStream << fromBigEndian<u256>(byteRange);
break;
case ABIType::UnsignedDec:
// Check if the detected type was wrong and if this could
// be signed. If an unsigned was detected in the expectations,
// but the actual result returned a signed, it would be formatted
// incorrectly.
soltestAssert(param.abiType.align == ABIType::AlignRight, "Unsigned decimals must be right-aligned.");
if (*byteRange.begin() & 0x80)
resultStream << u2s(fromBigEndian<u256>(byteRange));
else
resultStream << fromBigEndian<u256>(byteRange);
break;
case ABIType::Failure:
break;
case ABIType::None:
break;
}
it += offset;
if (it != _bytes.end() && !(param.abiType.type == ABIType::None))
resultStream << ", ";
}
soltestAssert(it == _bytes.end(), "Parameter encoding too short for the given byte range.");
return resultStream.str();
}
string TestFunctionCall::formatRawParameters(ParameterList const& _params, std::string const& _linePrefix) const
{
stringstream resultStream;
for (auto const& param: _params)
{
if (param.format.newline)
resultStream << endl << _linePrefix << "//";
resultStream << " " << param.rawString;
if (&param != &_params.back())
resultStream << ",";
}
return resultStream.str();
}
void TestFunctionCall::reset()
{
m_rawBytes = bytes{};
m_failure = true;
}
bool TestFunctionCall::matchesExpectation() const
{
return m_failure == m_call.expectations.failure && m_rawBytes == m_call.expectations.rawBytes();
}
+87
View File
@@ -0,0 +1,87 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <test/libsolidity/util/TestFileParser.h>
#include <test/TestCase.h>
#include <libsolidity/ast/Types.h>
#include <liblangutil/Exceptions.h>
#include <libdevcore/CommonData.h>
#include <iosfwd>
#include <numeric>
#include <stdexcept>
#include <string>
#include <vector>
namespace dev
{
namespace solidity
{
namespace test
{
/**
* Represents a function call and the result it returned. It stores the call
* representation itself, the actual byte result (if any) and a string representation
* used for the interactive update routine provided by isoltest. It also provides
* functionality to compare the actual result with the expectations attached to the
* call object, as well as a way to reset the result if executed multiple times.
*/
class TestFunctionCall
{
public:
TestFunctionCall(FunctionCall _call): m_call(std::move(_call)) {}
/// Formats this function call test and applies the format that was detected during parsing.
/// If _renderResult is false, the expected result of the call will is used, if it's false
/// the actual result is used.
/// If _highlight is false, it's formatted without colorized highlighting. If it's true, AnsiColorized is
/// used to apply a colorized highlighting.
std::string format(std::string const& _linePrefix = "", bool const _renderResult = false, bool const _highlight = false) const;
/// Resets current results in case the function was called and the result
/// stored already (e.g. if test case was updated via isoltest).
void reset();
FunctionCall const& call() const { return m_call; }
void setFailure(const bool _failure) { m_failure = _failure; }
void setRawBytes(const bytes _rawBytes) { m_rawBytes = _rawBytes; }
private:
/// Tries to format the given `bytes`, applying the detected ABI types that have be set for each parameter.
/// Throws if there's a mismatch in the size of `bytes` and the desired formats that are specified
/// in the ABI type.
std::string formatBytesParameters(bytes const& _bytes, ParameterList const& _params) const;
/// Formats the given parameters using their raw string representation.
std::string formatRawParameters(ParameterList const& _params, std::string const& _linePrefix = "") const;
/// Compares raw expectations (which are converted to a byte representation before),
/// and also the expected transaction status of the function call to the actual test results.
bool matchesExpectation() const;
/// Function call that has been parsed and which holds all parameters / expectations.
FunctionCall m_call;
/// Result of the actual call been made.
bytes m_rawBytes = bytes{};
/// Transaction status of the actual call. False in case of a REVERT or any other failure.
bool m_failure = true;
};
}
}
}
@@ -0,0 +1,121 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#include <functional>
#include <string>
#include <tuple>
#include <boost/test/unit_test.hpp>
#include <liblangutil/Exceptions.h>
#include <test/ExecutionFramework.h>
#include <test/libsolidity/util/TestFunctionCall.h>
using namespace std;
using namespace dev::test;
namespace dev
{
namespace solidity
{
namespace test
{
BOOST_AUTO_TEST_SUITE(TestFunctionCallTest)
BOOST_AUTO_TEST_CASE(format_unsigned_singleline)
{
bytes expectedBytes = toBigEndian(u256{1});
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
Parameter param{expectedBytes, "1", abiType, FormatInfo{}};
FunctionCallExpectations expectations{vector<Parameter>{param}, false, string{}};
FunctionCallArgs arguments{vector<Parameter>{param}, string{}};
FunctionCall call{"f(uint8)", 0, arguments, expectations};
TestFunctionCall test{call};
BOOST_REQUIRE_EQUAL(test.format(), "// f(uint8): 1 -> 1");
}
BOOST_AUTO_TEST_CASE(format_multiple_unsigned_singleline)
{
bytes expectedBytes = toBigEndian(u256{1});
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
Parameter param{expectedBytes, "1", abiType, FormatInfo{}};
FunctionCallExpectations expectations{vector<Parameter>{param, param}, false, string{}};
FunctionCallArgs arguments{vector<Parameter>{param, param}, string{}};
FunctionCall call{"f(uint8, uint8)", 0, arguments, expectations};
TestFunctionCall test{call};
BOOST_REQUIRE_EQUAL(test.format(), "// f(uint8, uint8): 1, 1 -> 1, 1");
}
BOOST_AUTO_TEST_CASE(format_signed_singleline)
{
bytes expectedBytes = toBigEndian(u256{-1});
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
Parameter param{expectedBytes, "-1", abiType, FormatInfo{}};
FunctionCallExpectations expectations{vector<Parameter>{param}, false, string{}};
FunctionCallArgs arguments{vector<Parameter>{param}, string{}};
FunctionCall call{"f(int8)", 0, arguments, expectations};
TestFunctionCall test{call};
BOOST_REQUIRE_EQUAL(test.format(), "// f(int8): -1 -> -1");
}
BOOST_AUTO_TEST_CASE(format_empty_byte_range)
{
bytes expectedBytes;
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
Parameter param{expectedBytes, "1", abiType, FormatInfo{}};
FunctionCallExpectations expectations{vector<Parameter>{param}, false, string{}};
FunctionCallArgs arguments{vector<Parameter>{}, string{}};
FunctionCall call{"f()", 0, arguments, expectations};
TestFunctionCall test{call};
BOOST_REQUIRE_EQUAL(test.format(), "// f() -> ");
}
BOOST_AUTO_TEST_CASE(format_parameter_encoding_too_short)
{
bytes expectedBytes = toBigEndian(u256{1}) + toBigEndian(u256{1});
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
Parameter param{expectedBytes, "1", abiType, FormatInfo{}};
FunctionCallExpectations expectations{vector<Parameter>{param, param}, false, string{}};
FunctionCallArgs arguments{vector<Parameter>{param, param}, string{}};
FunctionCall call{"f(uint8, uint8)", 0, arguments, expectations};
TestFunctionCall test{call};
BOOST_REQUIRE_THROW(test.format(), runtime_error);
}
BOOST_AUTO_TEST_CASE(format_byte_range_too_short)
{
bytes expectedBytes{0};
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 32};
Parameter param{expectedBytes, "1", abiType, FormatInfo{}};
FunctionCallExpectations expectations{vector<Parameter>{param, param}, false, string{}};
FunctionCallArgs arguments{vector<Parameter>{param, param}, string{}};
FunctionCall call{"f(uint8, uint8)", 0, arguments, expectations};
TestFunctionCall test{call};
BOOST_REQUIRE_THROW(test.format(), runtime_error);
}
BOOST_AUTO_TEST_SUITE_END()
}
}
}