diff --git a/test/libjulia/Common.cpp b/test/libjulia/Common.cpp new file mode 100644 index 000000000..30bb22ea7 --- /dev/null +++ b/test/libjulia/Common.cpp @@ -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 . +*/ +/** + * @date 2017 + * Common functions the iulia tests. + */ + +#include + +#include + +#include + +#include +#include +#include + +#include +#include + +#include + +using namespace std; +using namespace dev::julia; +using namespace dev::solidity; + +void dev::julia::test::printErrors(ErrorList const& _errors, Scanner const& _scanner) +{ + for (auto const& error: _errors) + SourceReferenceFormatter::printExceptionInformation( + cout, + *error, + (error->type() == Error::Type::Warning) ? "Warning" : "Error", + [&](std::string const&) -> Scanner const& { return _scanner; } + ); +} + + +pair, shared_ptr> dev::julia::test::parse(string const& _source, bool _julia) +{ + ErrorList errors; + ErrorReporter errorReporter(errors); + auto scanner = make_shared(CharStream(_source), ""); + auto parserResult = assembly::Parser(errorReporter, true).parse(scanner); + if (parserResult) + { + BOOST_REQUIRE(errorReporter.errors().empty()); + auto analysisInfo = make_shared(); + assembly::AsmAnalyzer analyzer(*analysisInfo, errorReporter, true); + if (analyzer.analyze(*parserResult)) + { + BOOST_REQUIRE(errorReporter.errors().empty()); + return make_pair(parserResult, analysisInfo); + } + } + printErrors(errors, *scanner); + BOOST_FAIL("Invalid source."); + + // Unreachable. + return {}; +} + +shared_ptr dev::julia::test::disambiguate(string const& _source) +{ + auto result = parse(_source); + Disambiguator disambiguator(*result.first, *result.second); + return disambiguator.run(); +} + +string dev::julia::test::format(string const& _source) +{ + return assembly::AsmPrinter(_julia)(*parse(_source, _julia).first); +} diff --git a/test/libjulia/Common.h b/test/libjulia/Common.h new file mode 100644 index 000000000..55fa03da1 --- /dev/null +++ b/test/libjulia/Common.h @@ -0,0 +1,54 @@ +/* + 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 . +*/ +/** + * @date 2017 + * Common functions the iulia tests. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace dev +{ +namespace solidity +{ +class Scanner; +class Error; +using ErrorList = std::vector>; +namespace assembly +{ +struct AsmAnalysisInfo; +} +} +namespace julia +{ +namespace test +{ + +void printErrors(solidity::ErrorList const& _errors, solidity::Scanner const& _scanner); +std::pair, std::shared_ptr> parse(std::string const& _source); +std::shared_ptr disambiguate(std::string const& _source); +std::string format(std::string const& _source); + +} +} +} diff --git a/test/libjulia/Disambiguator.cpp b/test/libjulia/Disambiguator.cpp index baee46415..f5f883bac 100644 --- a/test/libjulia/Disambiguator.cpp +++ b/test/libjulia/Disambiguator.cpp @@ -19,90 +19,23 @@ * Unit tests for the iulia name disambiguator. */ -#include +#include -#include -#include #include -#include -#include - -#include - #include -#include -#include - using namespace std; -using namespace dev::solidity::assembly; - -namespace dev -{ -namespace solidity -{ -namespace test -{ - -namespace -{ - -void printErrors(ErrorList const& _errors, Scanner const& _scanner) -{ - for (auto const& error: _errors) - SourceReferenceFormatter::printExceptionInformation( - cout, - *error, - (error->type() == Error::Type::Warning) ? "Warning" : "Error", - [&](std::string const&) -> Scanner const& { return _scanner; } - ); -} - - -pair, shared_ptr> parse(string const& _source) -{ - ErrorList errors; - ErrorReporter errorReporter(errors); - auto scanner = make_shared(CharStream(_source), ""); - auto parserResult = assembly::Parser(errorReporter, true).parse(scanner); - if (parserResult) - { - BOOST_REQUIRE(errorReporter.errors().empty()); - auto analysisInfo = make_shared(); - assembly::AsmAnalyzer analyzer(*analysisInfo, errorReporter, true); - if (analyzer.analyze(*parserResult)) - { - BOOST_REQUIRE(errorReporter.errors().empty()); - return make_pair(parserResult, analysisInfo); - } - } - printErrors(errors, *scanner); - BOOST_FAIL("Invalid source."); - - // Unreachable. - return {}; -} - -string disambiguate(string const& _source) -{ - auto result = parse(_source); - julia::Disambiguator disambiguator(*result.first, *result.second); - return assembly::AsmPrinter(true)(*disambiguator.run()); -} - -string format(string const& _source) -{ - return assembly::AsmPrinter(true)(*parse(_source).first); -} - -} +using namespace dev::julia::test; +using namespace dev::solidity; #define CHECK(_original, _expectation)\ do\ {\ - BOOST_CHECK_EQUAL(disambiguate(_original), format(_expectation));\ - BOOST_CHECK_EQUAL(disambiguate(_original), disambiguate(disambiguate(_original)));\ + assembly::AsmPrinter p(true);\ + string result = p(*disambiguate(_original));\ + BOOST_CHECK_EQUAL(result, format(_expectation));\ + BOOST_CHECK_EQUAL(result, p(*disambiguate(result)));\ }\ while(false) @@ -170,8 +103,3 @@ BOOST_AUTO_TEST_CASE(if_statement) } BOOST_AUTO_TEST_SUITE_END() - - -} -} -} // end namespaces