From 79407c87fb7c54d6924d9262ee3f6a7d6d73cce5 Mon Sep 17 00:00:00 2001 From: ssi91 Date: Mon, 25 May 2020 20:31:24 +0700 Subject: [PATCH] remove extra definition of printIndented add CommonBaseTestCase class and inherit some of TestCase classes from it. Since that, remove print source extra definitions create the base defifnition for printUpdatedExpectations and remove extra definitions of the method make CommonBaseTestCase c-tor explicit include AnsiColorized.h and sort includes implement a common result checker move the common implementations into TastCase --- test/TestCase.cpp | 35 +++++++++++++++++++++++++--- test/TestCase.h | 9 +++++-- test/libsolidity/ABIJsonTest.cpp | 21 +---------------- test/libsolidity/ABIJsonTest.h | 8 ------- test/libsolidity/ASTJSONTest.h | 1 - test/libsolidity/GasTest.h | 1 - test/libyul/EwasmTranslationTest.cpp | 22 +---------------- test/libyul/EwasmTranslationTest.h | 7 ------ test/libyul/FunctionSideEffects.cpp | 34 +-------------------------- test/libyul/FunctionSideEffects.h | 10 -------- test/libyul/ObjectCompilerTest.cpp | 33 +------------------------- test/libyul/ObjectCompilerTest.h | 7 ------ test/libyul/YulInterpreterTest.cpp | 22 +---------------- test/libyul/YulInterpreterTest.h | 7 ------ test/libyul/YulOptimizerTest.cpp | 22 +---------------- test/libyul/YulOptimizerTest.h | 6 ----- 16 files changed, 45 insertions(+), 200 deletions(-) diff --git a/test/TestCase.cpp b/test/TestCase.cpp index 23f23ac73..e6f4b212a 100644 --- a/test/TestCase.cpp +++ b/test/TestCase.cpp @@ -18,11 +18,13 @@ #include #include -#include -#include +#include + +#include +#include -#include #include +#include using namespace std; using namespace solidity; @@ -78,6 +80,33 @@ void TestCase::printIndented(ostream& _stream, string const& _output, string con _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): TestCase(_filename) { diff --git a/test/TestCase.h b/test/TestCase.h index 442b4fd5f..f846b7f87 100644 --- a/test/TestCase.h +++ b/test/TestCase.h @@ -57,14 +57,14 @@ public: /// Each line of output is prefixed with @arg _linePrefix. /// If @arg _formatted is true, color-coding may be used to indicate /// 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. virtual void printSettings(std::ostream &_stream, std::string const &_linePrefix = "", bool const _formatted = false); /// Outputs updated settings virtual void printUpdatedSettings(std::ostream& _stream, std::string const& _linePrefix = ""); /// Outputs test expectations to @arg _stream that match the actual results of the test. /// 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); @@ -97,6 +97,11 @@ protected: } 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; bool m_shouldRun = true; diff --git a/test/libsolidity/ABIJsonTest.cpp b/test/libsolidity/ABIJsonTest.cpp index a46c1d470..aa8153454 100644 --- a/test/libsolidity/ABIJsonTest.cpp +++ b/test/libsolidity/ABIJsonTest.cpp @@ -64,25 +64,6 @@ TestCase::TestResult ABIJsonTest::run(ostream& _stream, string const& _linePrefi m_obtainedResult += jsonPrettyPrint(compiler.contractABI(contractName)) + "\n"; 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; -} - -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); + return checkResult(_stream, _linePrefix, _formatted); } diff --git a/test/libsolidity/ABIJsonTest.h b/test/libsolidity/ABIJsonTest.h index 3a7cd397c..702d36dbf 100644 --- a/test/libsolidity/ABIJsonTest.h +++ b/test/libsolidity/ABIJsonTest.h @@ -36,14 +36,6 @@ public: ABIJsonTest(std::string const& _filename); 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; }; } diff --git a/test/libsolidity/ASTJSONTest.h b/test/libsolidity/ASTJSONTest.h index 92658edd6..45d7a08b9 100644 --- a/test/libsolidity/ASTJSONTest.h +++ b/test/libsolidity/ASTJSONTest.h @@ -41,7 +41,6 @@ public: void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override; private: std::vector> m_sources; - std::string m_expectation; std::string m_expectationLegacy; std::string m_astFilename; std::string m_legacyAstFilename; diff --git a/test/libsolidity/GasTest.h b/test/libsolidity/GasTest.h index 861e47bc9..d8bd0acad 100644 --- a/test/libsolidity/GasTest.h +++ b/test/libsolidity/GasTest.h @@ -47,7 +47,6 @@ private: bool m_optimise = false; bool m_optimiseYul = false; size_t m_optimiseRuns = 200; - std::string m_source; std::map> m_expectations; }; diff --git a/test/libyul/EwasmTranslationTest.cpp b/test/libyul/EwasmTranslationTest.cpp index 46ede4221..8ef5ba0a0 100644 --- a/test/libyul/EwasmTranslationTest.cpp +++ b/test/libyul/EwasmTranslationTest.cpp @@ -71,27 +71,7 @@ TestCase::TestResult EwasmTranslationTest::run(ostream& _stream, string const& _ m_obtainedResult = interpret(); - if (m_expectation != m_obtainedResult) - { - 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); + return checkResult(_stream, _linePrefix, _formatted); } bool EwasmTranslationTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) diff --git a/test/libyul/EwasmTranslationTest.h b/test/libyul/EwasmTranslationTest.h index f9c6e1d3c..e0a90482b 100644 --- a/test/libyul/EwasmTranslationTest.h +++ b/test/libyul/EwasmTranslationTest.h @@ -42,20 +42,13 @@ public: 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: bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); std::string interpret(); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); - std::string m_source; - std::string m_expectation; - std::shared_ptr m_object; - std::string m_obtainedResult; }; } diff --git a/test/libyul/FunctionSideEffects.cpp b/test/libyul/FunctionSideEffects.cpp index 3d3461b0e..aa7d1e503 100644 --- a/test/libyul/FunctionSideEffects.cpp +++ b/test/libyul/FunctionSideEffects.cpp @@ -87,37 +87,5 @@ TestCase::TestResult FunctionSideEffects::run(ostream& _stream, string const& _l for (auto const& fun: functionSideEffectsStr) m_obtainedResult += fun.first + ":" + (fun.second.empty() ? "" : " ") + fun.second + "\n"; - 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; -} - - -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; + return checkResult(_stream, _linePrefix, _formatted); } diff --git a/test/libyul/FunctionSideEffects.h b/test/libyul/FunctionSideEffects.h index b35994b00..23bebebcc 100644 --- a/test/libyul/FunctionSideEffects.h +++ b/test/libyul/FunctionSideEffects.h @@ -36,16 +36,6 @@ public: explicit FunctionSideEffects(std::string const& _filename); 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; }; } diff --git a/test/libyul/ObjectCompilerTest.cpp b/test/libyul/ObjectCompilerTest.cpp index 9dd589748..4b34da7b1 100644 --- a/test/libyul/ObjectCompilerTest.cpp +++ b/test/libyul/ObjectCompilerTest.cpp @@ -78,38 +78,7 @@ TestCase::TestResult ObjectCompilerTest::run(ostream& _stream, string const& _li (obj.sourceMappings->empty() ? "" : " " + *obj.sourceMappings) + "\n"; - 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; -} - -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; + return checkResult(_stream, _linePrefix, _formatted); } void ObjectCompilerTest::printErrors(ostream& _stream, ErrorList const& _errors) diff --git a/test/libyul/ObjectCompilerTest.h b/test/libyul/ObjectCompilerTest.h index dc02d504c..4a1c75486 100644 --- a/test/libyul/ObjectCompilerTest.h +++ b/test/libyul/ObjectCompilerTest.h @@ -47,20 +47,13 @@ public: 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; bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); void disambiguate(); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); - std::string m_source; bool m_optimize = false; - std::string m_expectation; - std::string m_obtainedResult; }; } diff --git a/test/libyul/YulInterpreterTest.cpp b/test/libyul/YulInterpreterTest.cpp index 69e2c8d10..9925b9546 100644 --- a/test/libyul/YulInterpreterTest.cpp +++ b/test/libyul/YulInterpreterTest.cpp @@ -59,27 +59,7 @@ TestCase::TestResult YulInterpreterTest::run(ostream& _stream, string const& _li m_obtainedResult = interpret(); - if (m_expectation != m_obtainedResult) - { - 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); + return checkResult(_stream, _linePrefix, _formatted); } bool YulInterpreterTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) diff --git a/test/libyul/YulInterpreterTest.h b/test/libyul/YulInterpreterTest.h index ec60dbaf4..ee60abe17 100644 --- a/test/libyul/YulInterpreterTest.h +++ b/test/libyul/YulInterpreterTest.h @@ -47,21 +47,14 @@ public: 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: bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); std::string interpret(); static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); - std::string m_source; - std::string m_expectation; - std::shared_ptr m_ast; std::shared_ptr m_analysisInfo; - std::string m_obtainedResult; }; } diff --git a/test/libyul/YulOptimizerTest.cpp b/test/libyul/YulOptimizerTest.cpp index 042d528db..bda7c6514 100644 --- a/test/libyul/YulOptimizerTest.cpp +++ b/test/libyul/YulOptimizerTest.cpp @@ -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"; - if (m_expectation != m_obtainedResult) - { - 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); + return checkResult(_stream, _linePrefix, _formatted); } bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) diff --git a/test/libyul/YulOptimizerTest.h b/test/libyul/YulOptimizerTest.h index 374ea9b19..3d351c3a2 100644 --- a/test/libyul/YulOptimizerTest.h +++ b/test/libyul/YulOptimizerTest.h @@ -56,9 +56,6 @@ public: 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: bool parse(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted); void disambiguate(); @@ -66,9 +63,7 @@ private: static void printErrors(std::ostream& _stream, langutil::ErrorList const& _errors); - std::string m_source; std::string m_optimizerStep; - std::string m_expectation; Dialect const* m_dialect = nullptr; std::set m_reservedIdentifiers; @@ -77,7 +72,6 @@ private: std::shared_ptr m_ast; std::shared_ptr m_analysisInfo; - std::string m_obtainedResult; }; }