Merge pull request #9059 from ssi91/refactor-interactive-tests

Refactor interactive tests
This commit is contained in:
chriseth 2020-06-02 16:50:31 +02:00 committed by GitHub
commit 9c92562e28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 45 additions and 200 deletions

View File

@ -18,11 +18,13 @@
#include <test/Common.h> #include <test/Common.h>
#include <test/TestCase.h> #include <test/TestCase.h>
#include <boost/algorithm/string/predicate.hpp> #include <libsolutil/AnsiColorized.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <stdexcept>
#include <iostream> #include <iostream>
#include <stdexcept>
using namespace std; using namespace std;
using namespace solidity; using namespace solidity;
@ -78,6 +80,33 @@ void TestCase::printIndented(ostream& _stream, string const& _output, string con
_stream << _linePrefix << line << endl; _stream << _linePrefix << line << endl;
} }
void TestCase::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void TestCase::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
}
TestCase::TestResult TestCase::checkResult(std::ostream& _stream, const std::string& _linePrefix, bool const _formatted)
{
if (m_expectation != m_obtainedResult)
{
string nextIndentLevel = _linePrefix + " ";
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);
util::AnsiColorized(_stream, _formatted, {util::formatting::BOLD, util::formatting::CYAN})
<< _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
EVMVersionRestrictedTestCase::EVMVersionRestrictedTestCase(string const& _filename): EVMVersionRestrictedTestCase::EVMVersionRestrictedTestCase(string const& _filename):
TestCase(_filename) TestCase(_filename)
{ {

View File

@ -57,14 +57,14 @@ public:
/// Each line of output is prefixed with @arg _linePrefix. /// Each line of output is prefixed with @arg _linePrefix.
/// If @arg _formatted is true, color-coding may be used to indicate /// If @arg _formatted is true, color-coding may be used to indicate
/// error locations in the contract, if applicable. /// error locations in the contract, if applicable.
virtual void printSource(std::ostream &_stream, std::string const &_linePrefix = "", bool const _formatted = false) const = 0; virtual void printSource(std::ostream &_stream, std::string const &_linePrefix = "", bool const _formatted = false) const;
/// Outputs settings. /// Outputs settings.
virtual void printSettings(std::ostream &_stream, std::string const &_linePrefix = "", bool const _formatted = false); virtual void printSettings(std::ostream &_stream, std::string const &_linePrefix = "", bool const _formatted = false);
/// Outputs updated settings /// Outputs updated settings
virtual void printUpdatedSettings(std::ostream& _stream, std::string const& _linePrefix = ""); virtual void printUpdatedSettings(std::ostream& _stream, std::string const& _linePrefix = "");
/// Outputs test expectations to @arg _stream that match the actual results of the test. /// Outputs test expectations to @arg _stream that match the actual results of the test.
/// Each line of output is prefixed with @arg _linePrefix. /// Each line of output is prefixed with @arg _linePrefix.
virtual void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const = 0; virtual void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const;
static bool isTestFilename(boost::filesystem::path const& _filename); static bool isTestFilename(boost::filesystem::path const& _filename);
@ -97,6 +97,11 @@ protected:
} }
void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const; 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;
std::string m_obtainedResult;
std::string m_expectation;
TestCaseReader m_reader; TestCaseReader m_reader;
bool m_shouldRun = true; bool m_shouldRun = true;

View File

@ -64,25 +64,6 @@ TestCase::TestResult ABIJsonTest::run(ostream& _stream, string const& _linePrefi
m_obtainedResult += jsonPrettyPrint(compiler.contractABI(contractName)) + "\n"; m_obtainedResult += jsonPrettyPrint(compiler.contractABI(contractName)) + "\n";
first = false; first = false;
} }
if (m_expectation != m_obtainedResult)
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
return checkResult(_stream, _linePrefix, _formatted);
void ABIJsonTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void ABIJsonTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
} }

View File

@ -36,14 +36,6 @@ public:
ABIJsonTest(std::string const& _filename); ABIJsonTest(std::string const& _filename);
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private:
std::string m_source;
std::string m_expectation;
std::string m_obtainedResult;
}; };
} }

View File

@ -41,7 +41,6 @@ public:
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override; void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private: private:
std::vector<std::pair<std::string, std::string>> m_sources; std::vector<std::pair<std::string, std::string>> m_sources;
std::string m_expectation;
std::string m_expectationLegacy; std::string m_expectationLegacy;
std::string m_astFilename; std::string m_astFilename;
std::string m_legacyAstFilename; std::string m_legacyAstFilename;

View File

@ -47,7 +47,6 @@ private:
bool m_optimise = false; bool m_optimise = false;
bool m_optimiseYul = false; bool m_optimiseYul = false;
size_t m_optimiseRuns = 200; size_t m_optimiseRuns = 200;
std::string m_source;
std::map<std::string, std::map<std::string, std::string>> m_expectations; std::map<std::string, std::map<std::string, std::string>> m_expectations;
}; };

View File

@ -71,27 +71,7 @@ TestCase::TestResult EwasmTranslationTest::run(ostream& _stream, string const& _
m_obtainedResult = interpret(); m_obtainedResult = interpret();
if (m_expectation != m_obtainedResult) return checkResult(_stream, _linePrefix, _formatted);
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
void EwasmTranslationTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void EwasmTranslationTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
} }
bool EwasmTranslationTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) bool EwasmTranslationTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)

View File

@ -42,20 +42,13 @@ public:
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const &_linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private: private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
std::string interpret(); std::string interpret();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_source;
std::string m_expectation;
std::shared_ptr<Object> m_object; std::shared_ptr<Object> m_object;
std::string m_obtainedResult;
}; };
} }

View File

@ -87,37 +87,5 @@ TestCase::TestResult FunctionSideEffects::run(ostream& _stream, string const& _l
for (auto const& fun: functionSideEffectsStr) for (auto const& fun: functionSideEffectsStr)
m_obtainedResult += fun.first + ":" + (fun.second.empty() ? "" : " ") + fun.second + "\n"; m_obtainedResult += fun.first + ":" + (fun.second.empty() ? "" : " ") + fun.second + "\n";
if (m_expectation != m_obtainedResult) return checkResult(_stream, _linePrefix, _formatted);
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
void FunctionSideEffects::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void FunctionSideEffects::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
}
void FunctionSideEffects::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;
} }

View File

@ -36,16 +36,6 @@ public:
explicit FunctionSideEffects(std::string const& _filename); explicit FunctionSideEffects(std::string const& _filename);
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private:
void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const;
std::string m_source;
std::string m_expectation;
std::string m_obtainedResult;
}; };
} }

View File

@ -78,38 +78,7 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li
(obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) + (obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) +
"\n"; "\n";
if (m_expectation != m_obtainedResult) return checkResult(_stream, _linePrefix, _formatted);
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
void ObjectCompilerTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void ObjectCompilerTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
}
void ObjectCompilerTest::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 ObjectCompilerTest::printErrors(ostream& _stream, ErrorList const& _errors) void ObjectCompilerTest::printErrors(ostream& _stream, ErrorList const& _errors)

View File

@ -47,20 +47,13 @@ public:
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const &_linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private: private:
void printIndented(std::ostream& _stream, std::string const& _output, std::string const& _linePrefix = "") const;
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
void disambiguate(); void disambiguate();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_source;
bool m_optimize = false; bool m_optimize = false;
std::string m_expectation;
std::string m_obtainedResult;
}; };
} }

View File

@ -59,27 +59,7 @@ TestCase::TestResult YulInterpreterTest::run(ostream& _stream, string const& _li
m_obtainedResult = interpret(); m_obtainedResult = interpret();
if (m_expectation != m_obtainedResult) return checkResult(_stream, _linePrefix, _formatted);
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
void YulInterpreterTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void YulInterpreterTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
} }
bool YulInterpreterTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) bool YulInterpreterTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)

View File

@ -47,21 +47,14 @@ public:
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const &_linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private: private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
std::string interpret(); std::string interpret();
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_source;
std::string m_expectation;
std::shared_ptr<Block> m_ast; std::shared_ptr<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo; std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
std::string m_obtainedResult;
}; };
} }

View File

@ -354,27 +354,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line
m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + AsmPrinter{ *m_dialect }(*m_ast) + "\n"; m_obtainedResult = "step: " + m_optimizerStep + "\n\n" + AsmPrinter{ *m_dialect }(*m_ast) + "\n";
if (m_expectation != m_obtainedResult) return checkResult(_stream, _linePrefix, _formatted);
{
string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Expected result:" << endl;
// TODO could compute a simple diff with highlighted lines
printIndented(_stream, m_expectation, nextIndentLevel);
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::CYAN}) << _linePrefix << "Obtained result:" << endl;
printIndented(_stream, m_obtainedResult, nextIndentLevel);
return TestResult::Failure;
}
return TestResult::Success;
}
void YulOptimizerTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
{
printIndented(_stream, m_source, _linePrefix);
}
void YulOptimizerTest::printUpdatedExpectations(ostream& _stream, string const& _linePrefix) const
{
printIndented(_stream, m_obtainedResult, _linePrefix);
} }
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)

View File

@ -56,9 +56,6 @@ public:
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
void printSource(std::ostream& _stream, std::string const &_linePrefix = "", bool const _formatted = false) const override;
void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override;
private: private:
bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted);
void disambiguate(); void disambiguate();
@ -66,9 +63,7 @@ private:
static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors);
std::string m_source;
std::string m_optimizerStep; std::string m_optimizerStep;
std::string m_expectation;
Dialect const* m_dialect = nullptr; Dialect const* m_dialect = nullptr;
std::set<YulString> m_reservedIdentifiers; std::set<YulString> m_reservedIdentifiers;
@ -77,7 +72,6 @@ private:
std::shared_ptr<Block> m_ast; std::shared_ptr<Block> m_ast;
std::shared_ptr<AsmAnalysisInfo> m_analysisInfo; std::shared_ptr<AsmAnalysisInfo> m_analysisInfo;
std::string m_obtainedResult;
}; };
} }