Generalize existing functions for indenting and prefixing

This commit is contained in:
Kamil Śliwak
2023-09-29 23:03:14 +02:00
parent c6b04df78c
commit 996e9cacd0
12 changed files with 89 additions and 73 deletions
+3 -5
View File
@@ -20,6 +20,7 @@
#include <test/Common.h>
#include <test/TestCase.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/StringUtils.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/test/unit_test.hpp>
@@ -144,11 +145,8 @@ void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix,
else
{
if (outputSourceNames)
_stream << _linePrefix << "==== Source: " + name << " ====" << endl;
stringstream stream(source);
string line;
while (getline(stream, line))
_stream << _linePrefix << line << endl;
printPrefixed(_stream, "==== Source: " + name + " ====", _linePrefix);
printPrefixed(_stream, source, _linePrefix);
}
}
+6 -17
View File
@@ -20,8 +20,8 @@
#include <test/TestCase.h>
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/StringUtils.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <iostream>
@@ -31,6 +31,7 @@ using namespace std;
using namespace solidity;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::util;
void TestCase::printSettings(ostream& _stream, const string& _linePrefix, const bool)
{
@@ -69,26 +70,14 @@ void TestCase::expect(string::iterator& _it, string::iterator _end, string::valu
++_it;
}
void TestCase::printIndented(ostream& _stream, string const& _output, string const& _linePrefix) const
{
stringstream output(_output);
string line;
while (getline(output, line))
if (line.empty())
// Avoid trailing spaces.
_stream << boost::trim_right_copy(_linePrefix) << endl;
else
_stream << _linePrefix << line << endl;
}
void TestCase::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
printPrefixed(_stream, m_source, _linePrefix);
}
void TestCase::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
printPrefixed(_stream, m_obtainedResult, _linePrefix);
}
TestCase::TestResult TestCase::checkResult(std::ostream& _stream, const std::string& _linePrefix, bool const _formatted)
@@ -99,10 +88,10 @@ TestCase::TestResult TestCase::checkResult(std::ostream& _stream, const std::str
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Expected result:" << endl;
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
printPrefixed(_stream, m_expectation, nextIndentLevel);
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
printPrefixed(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
-1
View File
@@ -100,7 +100,6 @@ protected:
++_it;
}
void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const;
TestCase::TestResult checkResult(std::ostream& _stream, const std::string& _linePrefix, bool const _formatted);
std::string m_source;
+4 -17
View File
@@ -270,12 +270,7 @@ bool ASTJSONTest::runTest(
"Expected result" <<
(!_variant.name().empty() ? " (" + _variant.name() + "):" : ":") <<
std::endl;
{
std::istringstream stream(_variant.expectation);
std::string line;
while (getline(stream, line))
_stream << nextIndentLevel << line << std::endl;
}
printPrefixed(_stream, _variant.expectation, nextIndentLevel);
_stream << std::endl;
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) <<
@@ -283,12 +278,7 @@ bool ASTJSONTest::runTest(
"Obtained result" <<
(!_variant.name().empty() ? " (" + _variant.name() + "):" : ":") <<
std::endl;
{
std::istringstream stream(_variant.result);
std::string line;
while (getline(stream, line))
_stream << nextIndentLevel << line << std::endl;
}
printPrefixed(_stream, _variant.result, nextIndentLevel);
_stream << std::endl;
return false;
}
@@ -301,11 +291,8 @@ void ASTJSONTest::printSource(std::ostream& _stream, std::string const& _linePre
for (auto const& source: m_sources)
{
if (m_sources.size() > 1 || source.first != "a")
_stream << _linePrefix << sourceDelimiter << source.first << " ====" << std::endl << std::endl;
std::stringstream stream(source.second);
std::string line;
while (getline(stream, line))
_stream << _linePrefix << line << std::endl;
printPrefixed(_stream, sourceDelimiter + source.first + " ====\n", _linePrefix);
printPrefixed(_stream, source.second, _linePrefix);
_stream << std::endl;
}
}
-8
View File
@@ -158,11 +158,3 @@ TestCase::TestResult GasTest::run(std::ostream& _stream, std::string const& _lin
return TestResult::Failure;
}
}
void GasTest::printSource(std::ostream& _stream, std::string const& _linePrefix, bool) const
{
std::string line;
std::istringstream input(m_source);
while (getline(input, line))
_stream << _linePrefix << line << std::endl;
}
-1
View File
@@ -41,7 +41,6 @@ public:
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false) override;
void printSource(std::ostream &_stream, std::string const &_linePrefix = "", bool _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
protected:
+3 -2
View File
@@ -100,8 +100,9 @@ void NatspecJSONTest::printExpectedResult(ostream& _stream, string const& _lineP
if (!m_expectedNatspecJSON.empty())
{
_stream << _linePrefix << "----" << endl;
printIndented(_stream, formatNatspecExpectations(m_expectedNatspecJSON), _linePrefix);
printPrefixed(_stream, formatNatspecExpectations(m_expectedNatspecJSON), _linePrefix);
}
}
void NatspecJSONTest::printObtainedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const
@@ -114,7 +115,7 @@ void NatspecJSONTest::printObtainedResult(ostream& _stream, string const& _lineP
_stream << _linePrefix << "----" << endl;
// TODO: Diff both versions and highlight differences.
// We should have a helper for doing that in newly defined test cases without much effort.
printIndented(_stream, formatNatspecExpectations(natspecJSON), _linePrefix);
printPrefixed(_stream, formatNatspecExpectations(natspecJSON), _linePrefix);
}
}
+2 -5
View File
@@ -647,11 +647,8 @@ void SemanticTest::printSource(std::ostream& _stream, std::string const& _linePr
else
{
if (outputNames)
_stream << _linePrefix << "==== Source: " + name << " ====" << std::endl;
std::stringstream stream(source);
std::string line;
while (getline(stream, line))
_stream << _linePrefix << line << std::endl;
printPrefixed(_stream, "==== Source: " + name + " ====", _linePrefix);
printPrefixed(_stream, source, _linePrefix);
}
}
}
+2 -1
View File
@@ -31,6 +31,7 @@
#include <liblangutil/Scanner.h>
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/StringUtils.h>
#include <fstream>
@@ -86,7 +87,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
{
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Result after the optimiser:" << endl;
printIndented(_stream, printed, _linePrefix + " ");
printPrefixed(_stream, printed, _linePrefix + " ");
return TestResult::FatalError;
}