Merge pull request #6504 from ethereum/soltest-interactive-update

[soltest] Improve interactive update routine
This commit is contained in:
chriseth 2019-05-02 16:41:52 +02:00 committed by GitHub
commit f5f0804c19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 399 additions and 146 deletions

View File

@ -74,18 +74,26 @@ bool SemanticTest::run(ostream& _stream, string const& _linePrefix, bool _format
test.setFailure(!m_transactionSuccessful);
test.setRawBytes(std::move(output));
test.setContractABI(m_compiler.contractABI(m_compiler.lastContractName()));
}
if (!success)
{
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
for (auto const& test: m_tests)
_stream << test.format(_linePrefix, false, _formatted) << endl;
{
ErrorReporter errorReporter;
_stream << test.format(errorReporter, _linePrefix, false, _formatted) << endl;
_stream << errorReporter.format(_linePrefix, _formatted);
}
_stream << endl;
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
for (auto const& test: m_tests)
_stream << test.format(_linePrefix, true, _formatted) << endl;
{
ErrorReporter errorReporter;
_stream << test.format(errorReporter, _linePrefix, true, _formatted) << endl;
_stream << errorReporter.format(_linePrefix, _formatted);
}
AnsiColorized(_stream, _formatted, {BOLD, RED}) << _linePrefix << endl << _linePrefix
<< "Attention: Updates on the test will apply the detected format displayed." << endl;
return false;

View File

@ -1,31 +1,35 @@
contract C {
function f() public returns (uint) {
function f() payable public returns (uint) {
return 2;
}
function g(uint x, uint y) public returns (uint) {
function g() public returns (uint, uint) {
return (2, 3);
}
function h(uint x, uint y) public returns (uint) {
return x - y;
}
function h() public payable returns (uint) {
return f();
}
function i(bytes32 b) public returns (bytes32) {
return b;
}
function j(bool b) public returns (bool) {
return !b;
}
function k(bytes32 b) public returns (bytes32) {
return b;
function k(bytes32 b) public returns (bytes32, bytes32) {
return (b, b);
}
function s() public returns (uint256) {
function l() public returns (uint256) {
return msg.data.length;
}
function m(bytes memory b) public returns (bytes memory) {
return b;
}
}
// ----
// _() -> FAILURE
// f() -> 2
// g(uint256,uint256): 1, -2 -> 3
// h(), 1 ether -> 2
// i() -> FAILURE
// f(), 1 ether -> 2
// g() -> 2, 3
// h(uint256,uint256): 1, -2 -> 3
// j(bool): true -> false
// k(bytes32): 0x31 -> 0x31
// s(): hex"4200ef" -> 7
// k(bytes32): 0x10 -> 0x10, 0x10
// l(): hex"4200ef" -> 7
// m(bytes): 32, 32, 0x20 -> 32, 32, 0x20
// m(bytes): 32, 3, hex"AB33BB" -> 32, 3, left(0xAB33BB)
// m(bytes): 32, 3, hex"AB33FF" -> 32, 3, hex"ab33ff0000000000000000000000000000000000000000000000000000000000"

View File

@ -105,13 +105,13 @@ struct ABIType
{
enum Type
{
None,
Failure,
Boolean,
UnsignedDec,
SignedDec,
Boolean,
Hex,
HexString,
Failure,
None
HexString
};
enum Align
{

View File

@ -13,7 +13,12 @@
*/
#include <test/libsolidity/util/TestFunctionCall.h>
#include <libdevcore/AnsiColorized.h>
#include <boost/algorithm/string/replace.hpp>
#include <regex>
#include <stdexcept>
#include <string>
@ -22,12 +27,52 @@ using namespace solidity;
using namespace dev::solidity::test;
using namespace std;
string TestFunctionCall::format(string const& _linePrefix, bool const _renderResult, bool const _highlight) const
namespace
{
static regex s_boolType{"(bool)"};
static regex s_uintType{"(uint\\d*)"};
static regex s_intType{"(int\\d*)"};
static regex s_bytesType{"(bytes\\d+)"};
static regex s_dynBytesType{"(\\bbytes\\b)"};
/// Translates Solidity's ABI types into the internal type representation of
/// soltest.
auto contractABITypes(string const& _type) -> vector<ABIType>
{
vector<ABIType> abiTypes;
if (regex_match(_type, s_boolType))
abiTypes.push_back(ABIType{ABIType::Boolean, ABIType::AlignRight, 32});
else if (regex_match(_type, s_uintType))
abiTypes.push_back(ABIType{ABIType::UnsignedDec, ABIType::AlignRight, 32});
else if (regex_match(_type, s_intType))
abiTypes.push_back(ABIType{ABIType::SignedDec, ABIType::AlignRight, 32});
else if (regex_match(_type, s_bytesType))
abiTypes.push_back(ABIType{ABIType::Hex, ABIType::AlignRight, 32});
else if (regex_match(_type, s_dynBytesType))
{
abiTypes.push_back(ABIType{ABIType::UnsignedDec, ABIType::AlignRight, 32});
abiTypes.push_back(ABIType{ABIType::UnsignedDec, ABIType::AlignRight, 32});
abiTypes.push_back(ABIType{ABIType::HexString, ABIType::AlignLeft, 32});
}
else
abiTypes.push_back(ABIType{ABIType::None, ABIType::AlignRight, 0});
return abiTypes;
};
}
string TestFunctionCall::format(
ErrorReporter& _errorReporter,
string const& _linePrefix,
bool const _renderResult,
bool const _highlight
) const
{
using namespace soltest;
using Token = soltest::Token;
stringstream _stream;
stringstream stream;
bool highlight = !matchesExpectation() && _highlight;
auto formatOutput = [&](bool const _singleLine)
@ -42,16 +87,16 @@ string TestFunctionCall::format(string const& _linePrefix, bool const _renderRes
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;
stream << _linePrefix << newline << ws << m_call.signature;
if (m_call.value > u256(0))
_stream << comma << ws << m_call.value << ws << ether;
stream << comma << ws << m_call.value << ws << ether;
if (!m_call.arguments.rawBytes().empty())
{
string output = formatRawParameters(m_call.arguments.parameters, _linePrefix);
_stream << colon;
stream << colon;
if (_singleLine)
_stream << ws;
_stream << output;
stream << ws;
stream << output;
}
@ -60,18 +105,18 @@ string TestFunctionCall::format(string const& _linePrefix, bool const _renderRes
if (_singleLine)
{
if (!m_call.arguments.comment.empty())
_stream << ws << comment << m_call.arguments.comment << comment;
_stream << ws << arrow << ws;
stream << ws << comment << m_call.arguments.comment << comment;
stream << ws << arrow << ws;
}
else
{
_stream << endl << _linePrefix << newline << ws;
stream << endl << _linePrefix << newline << ws;
if (!m_call.arguments.comment.empty())
{
_stream << comment << m_call.arguments.comment << comment;
_stream << endl << _linePrefix << newline << ws;
stream << comment << m_call.arguments.comment << comment;
stream << endl << _linePrefix << newline << ws;
}
_stream << arrow << ws;
stream << arrow << ws;
}
/// Format either the expected output or the actual result output
@ -83,6 +128,7 @@ string TestFunctionCall::format(string const& _linePrefix, bool const _renderRes
result = isFailure ?
failure :
formatRawParameters(m_call.expectations.result);
AnsiColorized(stream, highlight, {dev::formatting::RED_BACKGROUND}) << result;
}
else
{
@ -92,113 +138,210 @@ string TestFunctionCall::format(string const& _linePrefix, bool const _renderRes
failure :
matchesExpectation() ?
formatRawParameters(m_call.expectations.result) :
formatBytesParameters(output, m_call.expectations.result);
formatBytesParameters(
_errorReporter,
output,
m_call.signature,
m_call.expectations.result,
highlight
);
if (isFailure)
AnsiColorized(stream, highlight, {dev::formatting::RED_BACKGROUND}) << result;
else
stream << 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;
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;
stream << endl << _linePrefix << newline << ws;
stream << comment << m_call.expectations.comment << comment;
}
}
};
if (m_call.displayMode == FunctionCall::DisplayMode::SingleLine)
formatOutput(true);
else
formatOutput(false);
return _stream.str();
formatOutput(m_call.displayMode == FunctionCall::DisplayMode::SingleLine);
return stream.str();
}
string TestFunctionCall::formatBytesParameters(bytes const& _bytes, dev::solidity::test::ParameterList const& _params) const
string TestFunctionCall::formatBytesParameters(
ErrorReporter& _errorReporter,
bytes const& _bytes,
string const& _signature,
dev::solidity::test::ParameterList const& _params,
bool _highlight
) const
{
stringstream resultStream;
if (_bytes.empty())
return {};
using ParameterList = dev::solidity::test::ParameterList;
stringstream os;
string functionName{_signature.substr(0, _signature.find("("))};
auto sizeFold = [](size_t const _a, Parameter const& _b) { return _a + _b.abiType.size; };
size_t encodingSize = std::accumulate(_params.begin(), _params.end(), size_t{0}, sizeFold);
soltestAssert(
encodingSize == _bytes.size(),
"Encoding does not match byte range: the call returned " +
to_string(_bytes.size()) + " bytes, but " +
to_string(encodingSize) + " bytes were expected."
);
/// Infer type from Contract ABI. Used to generate values for
/// auto-correction during interactive update routine.
ParameterList abiParams;
for (auto const& function: m_contractABI)
if (function["name"] == functionName)
for (auto const& output: function["outputs"])
{
auto types = contractABITypes(output["type"].asString());
for (auto const& type: types)
abiParams.push_back(Parameter{bytes(), "", type, FormatInfo{}});
}
auto it = _bytes.begin();
for (auto const& param: _params)
/// If parameter count does not match, take types defined by ABI, but only
/// if the contract ABI is defined (needed for format tests where the actual
/// result does not matter).
ParameterList preferredParams;
if (m_contractABI && (_params.size() != abiParams.size()))
{
long offset = static_cast<long>(param.abiType.size);
_errorReporter.warning(
"Encoding does not match byte range. The call returned " +
to_string(_bytes.size()) + " bytes, but " +
to_string(encodingSize) + " bytes were expected."
);
preferredParams = abiParams;
}
else
preferredParams = _params;
/// If output is empty, do not format anything.
if (_bytes.empty())
return {};
/// Format output bytes with the given parameters. ABI type takes precedence if:
/// - size of ABI type is greater
/// - given expected type does not match and needs to be overridden in order
/// to generate a valid output of the parameter
auto it = _bytes.begin();
auto abiParam = abiParams.begin();
size_t paramIndex = 1;
for (auto const& param: preferredParams)
{
size_t size = param.abiType.size;
if (m_contractABI)
size = std::max((*abiParam).abiType.size, param.abiType.size);
long offset = static_cast<long>(size);
auto offsetIter = it + offset;
bytes byteRange{it, offsetIter};
switch (param.abiType.type)
{
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.
if (*byteRange.begin() & 0x80)
resultStream << u2s(fromBigEndian<u256>(byteRange));
else
resultStream << fromBigEndian<u256>(byteRange);
break;
case ABIType::SignedDec:
if (*byteRange.begin() & 0x80)
resultStream << u2s(fromBigEndian<u256>(byteRange));
else
resultStream << fromBigEndian<u256>(byteRange);
break;
case ABIType::Boolean:
{
u256 result = fromBigEndian<u256>(byteRange);
if (result == 0)
resultStream << "false";
else if (result == 1)
resultStream << "true";
else
resultStream << result;
break;
}
case ABIType::Hex:
resultStream << toHex(byteRange, HexPrefix::Add);
break;
case ABIType::HexString:
resultStream << "hex\"" << toHex(byteRange) << "\"";
break;
case ABIType::Failure:
break;
case ABIType::None:
break;
}
/// Override type with ABI type if given one does not match.
auto type = param.abiType;
if (m_contractABI)
if ((*abiParam).abiType.type > param.abiType.type)
{
type = (*abiParam).abiType;
_errorReporter.warning(
"Type of parameter " + to_string(paramIndex) +
" does not match the one inferred from ABI."
);
}
/// Prints obtained result if it does not match the expectation
/// and prints the expected result otherwise.
/// Highlights parameter only if it does not match.
if (byteRange != param.rawBytes)
AnsiColorized(
os,
_highlight,
{dev::formatting::RED_BACKGROUND}
) << formatBytesRange(byteRange, type);
else
os << param.rawString;
if (abiParam != abiParams.end())
abiParam++;
it += offset;
if (it != _bytes.end() && !(param.abiType.type == ABIType::None))
resultStream << ", ";
paramIndex++;
if (&param != &preferredParams.back())
os << ", ";
}
return resultStream.str();
return os.str();
}
string TestFunctionCall::formatRawParameters(dev::solidity::test::ParameterList const& _params, std::string const& _linePrefix) const
string TestFunctionCall::formatBytesRange(
bytes const& _bytes,
ABIType const& _abiType
) const
{
stringstream resultStream;
stringstream os;
switch (_abiType.type)
{
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.
if (*_bytes.begin() & 0x80)
os << u2s(fromBigEndian<u256>(_bytes));
else
os << fromBigEndian<u256>(_bytes);
break;
case ABIType::SignedDec:
if (*_bytes.begin() & 0x80)
os << u2s(fromBigEndian<u256>(_bytes));
else
os << fromBigEndian<u256>(_bytes);
break;
case ABIType::Boolean:
{
u256 result = fromBigEndian<u256>(_bytes);
if (result == 0)
os << "false";
else if (result == 1)
os << "true";
else
os << result;
break;
}
case ABIType::Hex:
{
string hex{toHex(_bytes, HexPrefix::Add)};
boost::algorithm::replace_all(hex, "00", "");
os << hex;
break;
}
case ABIType::HexString:
os << "hex\"" << toHex(_bytes) << "\"";
break;
case ABIType::Failure:
break;
case ABIType::None:
break;
}
return os.str();
}
string TestFunctionCall::formatRawParameters(
dev::solidity::test::ParameterList const& _params,
std::string const& _linePrefix
) const
{
stringstream os;
for (auto const& param: _params)
{
if (param.format.newline)
resultStream << endl << _linePrefix << "// ";
resultStream << param.rawString;
os << endl << _linePrefix << "// ";
os << param.rawString;
if (&param != &_params.back())
resultStream << ", ";
os << ", ";
}
return resultStream.str();
return os.str();
}
void TestFunctionCall::reset()

View File

@ -19,8 +19,11 @@
#include <libsolidity/ast/Types.h>
#include <liblangutil/Exceptions.h>
#include <libdevcore/AnsiColorized.h>
#include <libdevcore/CommonData.h>
#include <json/json.h>
#include <iosfwd>
#include <numeric>
#include <stdexcept>
@ -34,6 +37,95 @@ namespace solidity
namespace test
{
/**
* Representation of a notice, warning or error that can occur while
* formatting and therefore updating an interactive function call test.
*/
struct FormatError
{
enum Type
{
Notice,
Warning,
Error
};
explicit FormatError(Type _type, std::string _message):
type(_type),
message(std::move(_message))
{}
Type type;
std::string message;
};
using FormatErrors = std::vector<FormatError>;
/**
* Utility class that collects notices, warnings and errors and is able
* to format them for ANSI colorized output during the interactive update
* process in isoltest.
* Its purpose is to help users of isoltest to automatically
* update test files and always keep track of what is happening.
*/
class ErrorReporter
{
public:
explicit ErrorReporter() {}
/// Adds a new FormatError of type Notice with the given message.
void notice(std::string _notice)
{
m_errors.push_back(FormatError{FormatError::Notice, std::move(_notice)});
}
/// Adds a new FormatError of type Warning with the given message.
void warning(std::string _warning)
{
m_errors.push_back(FormatError{FormatError::Warning, std::move(_warning)});
}
/// Adds a new FormatError of type Error with the given message.
void error(std::string _error)
{
m_errors.push_back(FormatError{FormatError::Error, std::move(_error)});
}
/// Prints all errors depending on their type using ANSI colorized output.
/// It will be used to print notices, warnings and errors during the
/// interactive update process.
std::string format(std::string const& _linePrefix, bool _formatted)
{
std::stringstream os;
for (auto const& error: m_errors)
{
switch (error.type)
{
case FormatError::Notice:
break;
case FormatError::Warning:
AnsiColorized(
os,
_formatted,
{formatting::YELLOW}
) << _linePrefix << "Warning: " << error.message << std::endl;
break;
case FormatError::Error:
AnsiColorized(
os,
_formatted,
{formatting::RED}
) << _linePrefix << "Error: " << error.message << std::endl;
break;
}
}
return os.str();
}
private:
FormatErrors m_errors;
};
/**
* 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
@ -51,7 +143,27 @@ public:
/// 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;
/// If test expectations do not match, the contract ABI is consulted in order to get the
/// right encoding for returned bytes, based on the parsed return types.
/// Reports warnings and errors to the error reporter.
std::string format(
ErrorReporter& _errorReporter,
std::string const& _linePrefix = "",
bool const _renderResult = false,
bool const _highlight = false
) const;
/// Overloaded version that passes an error reporter which is never used outside
/// of this function.
std::string format(
std::string const& _linePrefix = "",
bool const _renderResult = false,
bool const _highlight = false
) const
{
ErrorReporter reporter;
return format(reporter, _linePrefix, _renderResult, _highlight);
}
/// Resets current results in case the function was called and the result
/// stored already (e.g. if test case was updated via isoltest).
@ -60,15 +172,32 @@ public:
FunctionCall const& call() const { return m_call; }
void setFailure(const bool _failure) { m_failure = _failure; }
void setRawBytes(const bytes _rawBytes) { m_rawBytes = _rawBytes; }
void setContractABI(Json::Value _contractABI) { m_contractABI = std::move(_contractABI); }
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;
/// Reports warnings and errors to the error reporter.
std::string formatBytesParameters(
ErrorReporter& _errorReporter,
bytes const& _bytes,
std::string const& _signature,
ParameterList const& _params,
bool highlight = false
) const;
/// Formats a given _bytes applying the _abiType.
std::string formatBytesRange(
bytes const& _bytes,
ABIType const& _abiType
) const;
/// Formats the given parameters using their raw string representation.
std::string formatRawParameters(ParameterList const& _params, std::string const& _linePrefix = "") const;
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.
@ -80,6 +209,9 @@ private:
bytes m_rawBytes = bytes{};
/// Transaction status of the actual call. False in case of a REVERT or any other failure.
bool m_failure = true;
/// JSON object which holds the contract ABI and that is used to set the output formatting
/// in the interactive update routine.
Json::Value m_contractABI;
};
}

View File

@ -135,7 +135,7 @@ BOOST_AUTO_TEST_CASE(format_hex_singleline)
test.setRawBytes(actualBytes);
test.setFailure(false);
BOOST_REQUIRE_EQUAL(test.format("", true), "// f(bytes32): 0x31 -> 0x3200000000000000000000000000000000000000000000000000000000000000");
BOOST_REQUIRE_EQUAL(test.format("", true), "// f(bytes32): 0x31 -> 0x32");
}
BOOST_AUTO_TEST_CASE(format_hex_string_singleline)
@ -237,40 +237,6 @@ BOOST_AUTO_TEST_CASE(format_failure_singleline)
BOOST_REQUIRE_EQUAL(test.format(), "// f(uint8): 1 -> FAILURE");
}
BOOST_AUTO_TEST_CASE(format_parameter_encoding_too_short)
{
bytes expectedBytes = toBigEndian(u256{1}) + toBigEndian(u256{1});
ABIType abiType{ABIType::UnsignedDec, ABIType::AlignRight, 20};
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};
bytes resultBytes = toBigEndian(u256{1}) + toBigEndian(u256{2});
test.setRawBytes(resultBytes);
test.setFailure(false);
BOOST_REQUIRE_THROW(test.format("", true), runtime_error);
}
BOOST_AUTO_TEST_CASE(format_byte_range_too_short)
{
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};
bytes resultBytes{0};
test.setRawBytes(resultBytes);
test.setFailure(false);
BOOST_REQUIRE_THROW(test.format("", true), runtime_error);
}
BOOST_AUTO_TEST_SUITE_END()
}