Merge pull request #6060 from ethereum/soltest-formatting

[soltest] Create dedicated formatting
This commit is contained in:
chriseth 2019-02-21 16:43:50 +01:00 committed by GitHub
commit 98012135c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 421 additions and 207 deletions

View File

@ -35,166 +35,6 @@ using namespace boost::algorithm;
using namespace boost::unit_test;
namespace fs = boost::filesystem;
namespace
{
using FunctionCallTest = SemanticTest::FunctionCallTest;
using FunctionCall = dev::solidity::test::FunctionCall;
using ParamList = dev::solidity::test::ParameterList;
string formatBytes(bytes const& _bytes, ParamList const& _params)
{
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:
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.
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 formatRawArguments(ParamList const& _params, string const& _linePrefix = "")
{
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();
}
string formatFunctionCallTest(
FunctionCallTest const& _test,
string const& _linePrefix = "",
bool const _renderResult = false,
bool const _highlight = false
)
{
using namespace soltest;
using Token = soltest::Token;
stringstream _stream;
FunctionCall call = _test.call;
bool highlight = !_test.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);
/// Prints the function signature. This is the same independent from the display-mode.
_stream << _linePrefix << newline << ws << call.signature;
if (call.value > u256(0))
_stream << comma << ws << call.value << ws << ether;
if (!call.arguments.rawBytes().empty())
{
string output = formatRawArguments(call.arguments.parameters, _linePrefix);
_stream << colon << output;
}
/// Prints comments on the function parameters and the arrow taking
/// the display-mode into account.
if (_singleLine)
{
if (!call.arguments.comment.empty())
_stream << ws << comment << call.arguments.comment << comment;
_stream << ws << arrow << ws;
}
else
{
_stream << endl << _linePrefix << newline << ws;
if (!call.arguments.comment.empty())
{
_stream << comment << call.arguments.comment << comment;
_stream << endl << _linePrefix << newline << ws;
}
_stream << arrow << ws;
}
/// Print either the expected output or the actual result output
string result;
if (!_renderResult)
{
bytes output = call.expectations.rawBytes();
bool const isFailure = call.expectations.failure;
result = isFailure ? failure : formatBytes(output, call.expectations.result);
}
else
{
bytes output = _test.rawBytes;
bool const isFailure = _test.failure;
result = isFailure ? failure : formatBytes(output, call.expectations.result);
}
AnsiColorized(_stream, highlight, {RED_BACKGROUND}) << result;
/// Print comments on expectations taking the display-mode into account.
if (_singleLine)
{
if (!call.expectations.comment.empty())
_stream << ws << comment << call.expectations.comment << comment;
}
else
{
if (!call.expectations.comment.empty())
{
_stream << endl << _linePrefix << newline << ws;
_stream << comment << call.expectations.comment << comment;
}
}
};
if (call.displayMode == FunctionCall::DisplayMode::SingleLine)
formatOutput(true);
else
formatOutput(false);
_stream << endl;
return _stream.str();
}
}
SemanticTest::SemanticTest(string const& _filename, string const& _ipcPath):
SolidityExecutionFramework(_ipcPath)
@ -218,27 +58,27 @@ bool SemanticTest::run(ostream& _stream, string const& _linePrefix, bool const _
for (auto& test: m_tests)
{
bytes output = callContractFunctionWithValueNoEncoding(
test.call.signature,
test.call.value,
test.call.arguments.rawBytes()
test.call().signature,
test.call().value,
test.call().arguments.rawBytes()
);
if ((m_transactionSuccessful == test.call.expectations.failure) || (output != test.call.expectations.rawBytes()))
if ((m_transactionSuccessful == test.call().expectations.failure) || (output != test.call().expectations.rawBytes()))
success = false;
test.failure = !m_transactionSuccessful;
test.rawBytes = std::move(output);
test.setFailure(!m_transactionSuccessful);
test.setRawBytes(std::move(output));
}
if (!success)
{
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
for (auto const& test: m_tests)
_stream << formatFunctionCallTest(test, _linePrefix, false, true & _formatted);
_stream << test.format(_linePrefix, false, _formatted) << endl;
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
for (auto const& test: m_tests)
_stream << formatFunctionCallTest(test, _linePrefix, true, true & _formatted);
_stream << test.format(_linePrefix, true, _formatted) << endl;
AnsiColorized(_stream, _formatted, {BOLD, RED}) << _linePrefix
<< "Attention: Updates on the test will apply the detected format displayed." << endl;
@ -258,14 +98,14 @@ void SemanticTest::printSource(ostream& _stream, string const& _linePrefix, bool
void SemanticTest::printUpdatedExpectations(ostream& _stream, string const&) const
{
for (auto const& test: m_tests)
_stream << formatFunctionCallTest(test, "", true, false);
_stream << test.format("", true, false) << endl;
}
void SemanticTest::parseExpectations(istream& _stream)
{
TestFileParser parser{_stream};
for (auto const& call: parser.parseFunctionCalls())
m_tests.emplace_back(FunctionCallTest{call, bytes{}, string{}});
auto functionCalls = parser.parseFunctionCalls();
move(functionCalls.begin(), functionCalls.end(), back_inserter(m_tests));
}
bool SemanticTest::deploy(string const& _contractName, u256 const& _value, bytes const& _arguments)

View File

@ -15,6 +15,7 @@
#pragma once
#include <test/libsolidity/util/TestFileParser.h>
#include <test/libsolidity/util/TestFunctionCall.h>
#include <test/libsolidity/SolidityExecutionFramework.h>
#include <test/libsolidity/AnalysisFramework.h>
#include <test/TestCase.h>
@ -42,35 +43,6 @@ namespace test
class SemanticTest: public SolidityExecutionFramework, public TestCase
{
public:
/**
* 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.
*/
struct FunctionCallTest
{
FunctionCall call;
bytes rawBytes;
std::string output;
bool failure = true;
/// 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
{
return failure == call.expectations.failure && rawBytes == call.expectations.rawBytes();
}
/// 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()
{
failure = true;
rawBytes = bytes{};
output = std::string{};
}
};
static std::unique_ptr<TestCase> create(Config const& _options)
{ return std::make_unique<SemanticTest>(_options.filename, _options.ipcPath); }
@ -90,8 +62,9 @@ public:
/// Returns true if deployment was successful, false otherwise.
bool deploy(std::string const& _contractName, u256 const& _value, bytes const& _arguments);
private:
std::string m_source;
std::vector<FunctionCallTest> m_tests;
std::vector<TestFunctionCall> m_tests;
};
}

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);
}
}

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;
};

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();
}

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;
};
}
}
}

View File

@ -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()
}
}
}

View File

@ -15,8 +15,9 @@ add_executable(isoltest
../Common.cpp
../TestCase.cpp
../libsolidity/util/TestFileParser.cpp
../libsolidity/util/TestFunctionCall.cpp
../libsolidity/SyntaxTest.cpp
../libsolidity/SemanticTest.cpp
../libsolidity/SemanticTest.cpp
../libsolidity/AnalysisFramework.cpp
../libsolidity/SolidityExecutionFramework.cpp
../ExecutionFramework.cpp