2019-02-21 00:50:41 +00:00
|
|
|
/*
|
|
|
|
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>
|
2019-04-11 18:25:20 +00:00
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
#include <test/libsolidity/util/BytesUtils.h>
|
|
|
|
#include <test/libsolidity/util/ContractABIUtils.h>
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
#include <libdevcore/AnsiColorized.h>
|
2019-04-11 18:25:20 +00:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace dev::solidity::test;
|
|
|
|
using namespace std;
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
string TestFunctionCall::format(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
string const& _linePrefix,
|
|
|
|
bool const _renderResult,
|
|
|
|
bool const _highlight
|
|
|
|
) const
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
|
|
|
using namespace soltest;
|
|
|
|
using Token = soltest::Token;
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
stringstream stream;
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
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.
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << _linePrefix << newline << ws << m_call.signature;
|
2019-02-21 00:50:41 +00:00
|
|
|
if (m_call.value > u256(0))
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << comma << ws << m_call.value << ws << ether;
|
2019-02-21 00:50:41 +00:00
|
|
|
if (!m_call.arguments.rawBytes().empty())
|
|
|
|
{
|
|
|
|
string output = formatRawParameters(m_call.arguments.parameters, _linePrefix);
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << colon;
|
2019-02-28 13:16:16 +00:00
|
|
|
if (_singleLine)
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << ws;
|
|
|
|
stream << output;
|
2019-02-28 13:16:16 +00:00
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Formats comments on the function parameters and the arrow taking
|
|
|
|
/// the display-mode into account.
|
|
|
|
if (_singleLine)
|
|
|
|
{
|
|
|
|
if (!m_call.arguments.comment.empty())
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << ws << comment << m_call.arguments.comment << comment;
|
|
|
|
stream << ws << arrow << ws;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << endl << _linePrefix << newline << ws;
|
2019-02-21 00:50:41 +00:00
|
|
|
if (!m_call.arguments.comment.empty())
|
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << comment << m_call.arguments.comment << comment;
|
|
|
|
stream << endl << _linePrefix << newline << ws;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << arrow << ws;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
2019-02-28 13:16:16 +00:00
|
|
|
result = isFailure ?
|
|
|
|
failure :
|
|
|
|
formatRawParameters(m_call.expectations.result);
|
2019-04-11 18:25:20 +00:00
|
|
|
AnsiColorized(stream, highlight, {dev::formatting::RED_BACKGROUND}) << result;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes output = m_rawBytes;
|
|
|
|
bool const isFailure = m_failure;
|
2019-02-28 13:16:16 +00:00
|
|
|
result = isFailure ?
|
|
|
|
failure :
|
|
|
|
matchesExpectation() ?
|
|
|
|
formatRawParameters(m_call.expectations.result) :
|
2019-04-11 18:25:20 +00:00
|
|
|
formatBytesParameters(
|
|
|
|
_errorReporter,
|
|
|
|
output,
|
|
|
|
m_call.signature,
|
|
|
|
m_call.expectations.result,
|
|
|
|
highlight
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isFailure)
|
|
|
|
AnsiColorized(stream, highlight, {dev::formatting::RED_BACKGROUND}) << result;
|
|
|
|
else
|
|
|
|
stream << result;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Format comments on expectations taking the display-mode into account.
|
|
|
|
if (_singleLine)
|
|
|
|
{
|
|
|
|
if (!m_call.expectations.comment.empty())
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << ws << comment << m_call.expectations.comment << comment;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_call.expectations.comment.empty())
|
|
|
|
{
|
2019-04-11 13:42:48 +00:00
|
|
|
stream << endl << _linePrefix << newline << ws;
|
|
|
|
stream << comment << m_call.expectations.comment << comment;
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
formatOutput(m_call.displayMode == FunctionCall::DisplayMode::SingleLine);
|
|
|
|
return stream.str();
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
string TestFunctionCall::formatBytesParameters(
|
|
|
|
ErrorReporter& _errorReporter,
|
|
|
|
bytes const& _bytes,
|
2019-04-11 18:25:20 +00:00
|
|
|
string const& _signature,
|
|
|
|
dev::solidity::test::ParameterList const& _params,
|
|
|
|
bool _highlight
|
2019-04-11 13:42:48 +00:00
|
|
|
) const
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
2019-04-11 18:25:20 +00:00
|
|
|
using ParameterList = dev::solidity::test::ParameterList;
|
|
|
|
|
|
|
|
stringstream os;
|
|
|
|
string functionName{_signature.substr(0, _signature.find("("))};
|
2019-04-11 13:42:48 +00:00
|
|
|
|
2019-07-03 19:37:55 +00:00
|
|
|
/// Create parameters from Contract ABI. Used to generate values for
|
2019-04-11 18:25:20 +00:00
|
|
|
/// auto-correction during interactive update routine.
|
2019-07-03 19:37:55 +00:00
|
|
|
ParameterList abiParams = ContractABIUtils().parametersFromJson(m_contractABI, functionName);
|
2019-04-11 18:25:20 +00:00
|
|
|
|
|
|
|
/// 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()))
|
|
|
|
{
|
2019-07-03 19:37:55 +00:00
|
|
|
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);
|
|
|
|
|
2019-04-11 13:42:48 +00:00
|
|
|
_errorReporter.warning(
|
|
|
|
"Encoding does not match byte range. The call returned " +
|
|
|
|
to_string(_bytes.size()) + " bytes, but " +
|
|
|
|
to_string(encodingSize) + " bytes were expected."
|
|
|
|
);
|
2019-04-11 18:25:20 +00:00
|
|
|
preferredParams = abiParams;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
preferredParams = _params;
|
2019-03-13 13:55:53 +00:00
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
/// 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
|
2019-02-21 00:50:41 +00:00
|
|
|
auto it = _bytes.begin();
|
2019-04-11 18:25:20 +00:00
|
|
|
auto abiParam = abiParams.begin();
|
|
|
|
size_t paramIndex = 1;
|
|
|
|
for (auto const& param: preferredParams)
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
2019-04-11 18:25:20 +00:00
|
|
|
size_t size = param.abiType.size;
|
|
|
|
if (m_contractABI)
|
|
|
|
size = std::max((*abiParam).abiType.size, param.abiType.size);
|
|
|
|
|
|
|
|
long offset = static_cast<long>(size);
|
2019-02-21 00:50:41 +00:00
|
|
|
auto offsetIter = it + offset;
|
|
|
|
bytes byteRange{it, offsetIter};
|
2019-04-11 18:25:20 +00:00
|
|
|
|
|
|
|
/// 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++;
|
|
|
|
|
2019-02-21 00:50:41 +00:00
|
|
|
it += offset;
|
2019-04-11 18:25:20 +00:00
|
|
|
paramIndex++;
|
|
|
|
if (¶m != &preferredParams.back())
|
|
|
|
os << ", ";
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestFunctionCall::formatBytesRange(
|
|
|
|
bytes const& _bytes,
|
|
|
|
ABIType const& _abiType
|
|
|
|
) const
|
|
|
|
{
|
|
|
|
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.
|
2019-07-03 19:37:55 +00:00
|
|
|
os << BytesUtils().formatUnsigned(_bytes);
|
2019-04-11 18:25:20 +00:00
|
|
|
break;
|
|
|
|
case ABIType::SignedDec:
|
2019-07-03 19:37:55 +00:00
|
|
|
os << BytesUtils().formatSigned(_bytes);
|
2019-04-11 18:25:20 +00:00
|
|
|
break;
|
|
|
|
case ABIType::Boolean:
|
2019-07-03 19:37:55 +00:00
|
|
|
os << BytesUtils().formatBoolean(_bytes);
|
2019-04-11 18:25:20 +00:00
|
|
|
break;
|
|
|
|
case ABIType::Hex:
|
2019-07-03 19:37:55 +00:00
|
|
|
os << BytesUtils().formatHex(_bytes);
|
2019-04-11 18:25:20 +00:00
|
|
|
break;
|
|
|
|
case ABIType::HexString:
|
2019-07-03 19:37:55 +00:00
|
|
|
os << BytesUtils().formatHexString(_bytes);
|
2019-04-11 18:25:20 +00:00
|
|
|
break;
|
2019-05-06 08:08:10 +00:00
|
|
|
case ABIType::String:
|
2019-07-03 19:37:55 +00:00
|
|
|
os << BytesUtils().formatString(_bytes);
|
2019-05-06 08:08:10 +00:00
|
|
|
break;
|
2019-04-11 18:25:20 +00:00
|
|
|
case ABIType::Failure:
|
|
|
|
break;
|
|
|
|
case ABIType::None:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return os.str();
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 18:25:20 +00:00
|
|
|
string TestFunctionCall::formatRawParameters(
|
|
|
|
dev::solidity::test::ParameterList const& _params,
|
|
|
|
std::string const& _linePrefix
|
|
|
|
) const
|
2019-02-21 00:50:41 +00:00
|
|
|
{
|
2019-04-11 18:25:20 +00:00
|
|
|
stringstream os;
|
2019-02-21 00:50:41 +00:00
|
|
|
for (auto const& param: _params)
|
|
|
|
{
|
|
|
|
if (param.format.newline)
|
2019-04-11 18:25:20 +00:00
|
|
|
os << endl << _linePrefix << "// ";
|
|
|
|
os << param.rawString;
|
2019-02-21 00:50:41 +00:00
|
|
|
if (¶m != &_params.back())
|
2019-04-11 18:25:20 +00:00
|
|
|
os << ", ";
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
2019-04-11 18:25:20 +00:00
|
|
|
return os.str();
|
2019-02-21 00:50:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|