diff options
Diffstat (limited to 'test/libsolidity')
10 files changed, 205 insertions, 169 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index c80aa8ba..2a24a1e3 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -3745,38 +3745,6 @@ BOOST_AUTO_TEST_CASE(generic_call) BOOST_CHECK_EQUAL(balanceAt(m_contractAddress), 50 - 2); } -BOOST_AUTO_TEST_CASE(generic_callcode) -{ - char const* sourceCode = R"**( - contract Receiver { - uint public received; - function receive(uint256 x) payable { received = x; } - } - contract Sender { - uint public received; - function Sender() payable { } - function doSend(address rec) returns (uint d) - { - bytes4 signature = bytes4(bytes32(keccak256("receive(uint256)"))); - rec.callcode.value(2)(signature, 23); - return Receiver(rec).received(); - } - } - )**"; - compileAndRun(sourceCode, 0, "Receiver"); - u160 const c_receiverAddress = m_contractAddress; - compileAndRun(sourceCode, 50, "Sender"); - u160 const c_senderAddress = m_contractAddress; - ABI_CHECK(callContractFunction("doSend(address)", c_receiverAddress), encodeArgs(0)); - ABI_CHECK(callContractFunction("received()"), encodeArgs(23)); - m_contractAddress = c_receiverAddress; - ABI_CHECK(callContractFunction("received()"), encodeArgs(0)); - BOOST_CHECK(storageEmpty(c_receiverAddress)); - BOOST_CHECK(!storageEmpty(c_senderAddress)); - BOOST_CHECK_EQUAL(balanceAt(c_receiverAddress), 0); - BOOST_CHECK_EQUAL(balanceAt(c_senderAddress), 50); -} - BOOST_AUTO_TEST_CASE(generic_delegatecall) { char const* sourceCode = R"**( @@ -11558,9 +11526,6 @@ BOOST_AUTO_TEST_CASE(bare_call_invalid_address) function f() external returns (bool) { return address(0x4242).call(); } - function g() external returns (bool) { - return address(0x4242).callcode(); - } function h() external returns (bool) { return address(0x4242).delegatecall(); } @@ -11568,7 +11533,6 @@ BOOST_AUTO_TEST_CASE(bare_call_invalid_address) )"; compileAndRun(sourceCode, 0, "C"); ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(1))); - ABI_CHECK(callContractFunction("g()"), encodeArgs(u256(1))); ABI_CHECK(callContractFunction("h()"), encodeArgs(u256(1))); } diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp index 430073a0..1de42300 100644 --- a/test/libsolidity/SyntaxTest.cpp +++ b/test/libsolidity/SyntaxTest.cpp @@ -33,28 +33,10 @@ using namespace std; namespace fs = boost::filesystem; using namespace boost::unit_test; -template<typename IteratorType> -void skipWhitespace(IteratorType& _it, IteratorType _end) +namespace { - while (_it != _end && isspace(*_it)) - ++_it; -} - -template<typename IteratorType> -void skipSlashes(IteratorType& _it, IteratorType _end) -{ - while (_it != _end && *_it == '/') - ++_it; -} - -void expect(string::iterator& _it, string::iterator _end, string::value_type _c) -{ - if (_it == _end || *_it != _c) - throw runtime_error(string("Invalid test expectation. Expected: \"") + _c + "\"."); - ++_it; -} -int parseUnsignedInteger(string::iterator &_it, string::iterator _end) +int parseUnsignedInteger(string::iterator& _it, string::iterator _end) { if (_it == _end || !isdigit(*_it)) throw runtime_error("Invalid test expectation. Source location expected."); @@ -68,6 +50,8 @@ int parseUnsignedInteger(string::iterator &_it, string::iterator _end) return result; } +} + SyntaxTest::SyntaxTest(string const& _filename) { ifstream file(_filename); @@ -120,6 +104,55 @@ bool SyntaxTest::run(ostream& _stream, string const& _linePrefix, bool const _fo return true; } +void SyntaxTest::printSource(ostream& _stream, string const& _linePrefix, bool const _formatted) const +{ + if (_formatted) + { + if (m_source.empty()) + return; + + vector<char const*> sourceFormatting(m_source.length(), formatting::RESET); + for (auto const& error: m_errorList) + if (error.locationStart >= 0 && error.locationEnd >= 0) + { + assert(static_cast<size_t>(error.locationStart) <= m_source.length()); + assert(static_cast<size_t>(error.locationEnd) <= m_source.length()); + bool isWarning = error.type == "Warning"; + for (int i = error.locationStart; i < error.locationEnd; i++) + if (isWarning) + { + if (sourceFormatting[i] == formatting::RESET) + sourceFormatting[i] = formatting::ORANGE_BACKGROUND; + } + else + sourceFormatting[i] = formatting::RED_BACKGROUND; + } + + _stream << _linePrefix << sourceFormatting.front() << m_source.front(); + for (size_t i = 1; i < m_source.length(); i++) + { + if (sourceFormatting[i] != sourceFormatting[i - 1]) + _stream << sourceFormatting[i]; + if (m_source[i] != '\n') + _stream << m_source[i]; + else + { + _stream << formatting::RESET << endl; + if (i + 1 < m_source.length()) + _stream << _linePrefix << sourceFormatting[i]; + } + } + _stream << formatting::RESET; + } + else + { + stringstream stream(m_source); + string line; + while (getline(stream, line)) + _stream << _linePrefix << line << endl; + } +} + void SyntaxTest::printErrorList( ostream& _stream, vector<SyntaxTestError> const& _errorList, @@ -159,19 +192,6 @@ string SyntaxTest::errorMessage(Exception const& _e) return "NONE"; } -string SyntaxTest::parseSource(istream& _stream) -{ - string source; - string line; - string const delimiter("// ----"); - while (getline(_stream, line)) - if (boost::algorithm::starts_with(line, delimiter)) - break; - else - source += line + "\n"; - return source; -} - vector<SyntaxTestError> SyntaxTest::parseExpectations(istream& _stream) { vector<SyntaxTestError> expectations; @@ -220,71 +240,3 @@ vector<SyntaxTestError> SyntaxTest::parseExpectations(istream& _stream) } return expectations; } - -#if BOOST_VERSION < 105900 -test_case *make_test_case( - function<void()> const& _fn, - string const& _name, - string const& /* _filename */, - size_t /* _line */ -) -{ - return make_test_case(_fn, _name); -} -#endif - -bool SyntaxTest::isTestFilename(boost::filesystem::path const& _filename) -{ - return _filename.extension().string() == ".sol" && - !boost::starts_with(_filename.string(), "~") && - !boost::starts_with(_filename.string(), "."); -} - -int SyntaxTest::registerTests( - boost::unit_test::test_suite& _suite, - boost::filesystem::path const& _basepath, - boost::filesystem::path const& _path -) -{ - int numTestsAdded = 0; - fs::path fullpath = _basepath / _path; - if (fs::is_directory(fullpath)) - { - test_suite* sub_suite = BOOST_TEST_SUITE(_path.filename().string()); - for (auto const& entry: boost::iterator_range<fs::directory_iterator>( - fs::directory_iterator(fullpath), - fs::directory_iterator() - )) - if (fs::is_directory(entry.path()) || isTestFilename(entry.path().filename())) - numTestsAdded += registerTests(*sub_suite, _basepath, _path / entry.path().filename()); - _suite.add(sub_suite); - } - else - { - static vector<unique_ptr<string>> filenames; - - filenames.emplace_back(new string(_path.string())); - _suite.add(make_test_case( - [fullpath] - { - BOOST_REQUIRE_NO_THROW({ - try - { - stringstream errorStream; - if (!SyntaxTest(fullpath.string()).run(errorStream)) - BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str()); - } - catch (boost::exception const& _e) - { - BOOST_ERROR("Exception during syntax test: " << boost::diagnostic_information(_e)); - } - }); - }, - _path.stem().string(), - *filenames.back(), - 0 - )); - numTestsAdded = 1; - } - return numTestsAdded; -} diff --git a/test/libsolidity/SyntaxTest.h b/test/libsolidity/SyntaxTest.h index 6159e789..e9e36aa6 100644 --- a/test/libsolidity/SyntaxTest.h +++ b/test/libsolidity/SyntaxTest.h @@ -19,11 +19,9 @@ #include <test/libsolidity/AnalysisFramework.h> #include <test/libsolidity/FormattedScope.h> +#include <test/libsolidity/TestCase.h> #include <libsolidity/interface/Exceptions.h> -#include <boost/noncopyable.hpp> -#include <boost/test/unit_test.hpp> - #include <iosfwd> #include <string> #include <vector> @@ -52,17 +50,24 @@ struct SyntaxTestError }; -class SyntaxTest: AnalysisFramework +class SyntaxTest: AnalysisFramework, public TestCase { public: + static std::unique_ptr<TestCase> create(std::string const& _filename) + { return std::unique_ptr<TestCase>(new SyntaxTest(_filename)); } SyntaxTest(std::string const& _filename); - bool run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false); + virtual bool run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; - std::vector<SyntaxTestError> const& expectations() const { return m_expectations; } - std::string const& source() const { return m_source; } - std::vector<SyntaxTestError> const& errorList() const { return m_errorList; } + virtual void printSource(std::ostream &_stream, std::string const &_linePrefix = "", bool const _formatted = false) const override; + virtual void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override + { + if (!m_errorList.empty()) + printErrorList(_stream, m_errorList, _linePrefix, false); + } + static std::string errorMessage(Exception const& _e); +private: static void printErrorList( std::ostream& _stream, std::vector<SyntaxTestError> const& _errors, @@ -70,15 +75,6 @@ public: bool const _formatted = false ); - static int registerTests( - boost::unit_test::test_suite& _suite, - boost::filesystem::path const& _basepath, - boost::filesystem::path const& _path - ); - static bool isTestFilename(boost::filesystem::path const& _filename); - static std::string errorMessage(Exception const& _e); -private: - static std::string parseSource(std::istream& _stream); static std::vector<SyntaxTestError> parseExpectations(std::istream& _stream); std::string m_source; diff --git a/test/libsolidity/TestCase.cpp b/test/libsolidity/TestCase.cpp new file mode 100644 index 00000000..6c4e0aea --- /dev/null +++ b/test/libsolidity/TestCase.cpp @@ -0,0 +1,55 @@ +/* + 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/TestCase.h> + +#include <boost/algorithm/string.hpp> +#include <boost/algorithm/string/predicate.hpp> + +#include <stdexcept> + +using namespace dev; +using namespace solidity; +using namespace dev::solidity::test; +using namespace std; + +bool TestCase::isTestFilename(boost::filesystem::path const& _filename) +{ + return _filename.extension().string() == ".sol" && + !boost::starts_with(_filename.string(), "~") && + !boost::starts_with(_filename.string(), "."); +} + +string TestCase::parseSource(istream& _stream) +{ + string source; + string line; + string const delimiter("// ----"); + while (getline(_stream, line)) + if (boost::algorithm::starts_with(line, delimiter)) + break; + else + source += line + "\n"; + return source; +} + +void TestCase::expect(string::iterator& _it, string::iterator _end, string::value_type _c) +{ + if (_it == _end || *_it != _c) + throw runtime_error(string("Invalid test expectation. Expected: \"") + _c + "\"."); + ++_it; +} diff --git a/test/libsolidity/TestCase.h b/test/libsolidity/TestCase.h new file mode 100644 index 00000000..3c05ae4e --- /dev/null +++ b/test/libsolidity/TestCase.h @@ -0,0 +1,80 @@ +/* + 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/>. +*/ + +#pragma once + +#include <boost/filesystem.hpp> + +#include <iosfwd> +#include <memory> +#include <string> + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +/** Common superclass of SyntaxTest and SemanticsTest. */ +class TestCase +{ +public: + using TestCaseCreator = std::unique_ptr<TestCase>(*)(std::string const&); + + virtual ~TestCase() {} + + /// Runs the test case. + /// Outputs error messages to @arg _stream. Each line of output is prefixed with @arg _linePrefix. + /// Optionally, color-coding can be enabled (if @arg _formatted is set to true). + /// @returns true, if the test case succeeds, false otherwise + virtual bool run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) = 0; + + /// Outputs the test contract to @arg _stream. + /// 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; + /// 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; + + static bool isTestFilename(boost::filesystem::path const& _filename); + +protected: + static std::string parseSource(std::istream& _file); + static void expect(std::string::iterator& _it, std::string::iterator _end, std::string::value_type _c); + + template<typename IteratorType> + static void skipWhitespace(IteratorType& _it, IteratorType _end) + { + while (_it != _end && isspace(*_it)) + ++_it; + } + + template<typename IteratorType> + static void skipSlashes(IteratorType& _it, IteratorType _end) + { + while (_it != _end && *_it == '/') + ++_it; + } + +}; + +} +} +} diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/349_unused_return_value_callcode.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/349_unused_return_value_callcode.sol deleted file mode 100644 index a5bda4f6..00000000 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/349_unused_return_value_callcode.sol +++ /dev/null @@ -1,8 +0,0 @@ -contract test { - function f() public { - address(0x12).callcode("abc"); - } -} -// ---- -// Warning: (50-79): Return value of low-level calls not used. -// Warning: (50-72): "callcode" has been deprecated in favour of "delegatecall". diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/351_callcode_deprecated.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/351_callcode_deprecated.sol index 422de58c..554f2e11 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/351_callcode_deprecated.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/351_callcode_deprecated.sol @@ -4,4 +4,4 @@ contract test { } } // ---- -// Warning: (55-77): "callcode" has been deprecated in favour of "delegatecall". +// TypeError: (55-77): "callcode" has been deprecated in favour of "delegatecall". diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol index ee4acdcc..d0c3769c 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/413_address_methods.sol @@ -3,13 +3,10 @@ contract C { address addr; uint balance = addr.balance; bool callRet = addr.call(); - bool callcodeRet = addr.callcode(); bool delegatecallRet = addr.delegatecall(); bool sendRet = addr.send(1); addr.transfer(1); - callRet; callcodeRet; delegatecallRet; sendRet; + balance; callRet; delegatecallRet; sendRet; } } // ---- -// Warning: (161-174): "callcode" has been deprecated in favour of "delegatecall". -// Warning: (69-81): Unused local variable. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/545_warn_about_address_members_on_contract_callcode.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/545_warn_about_address_members_on_contract_callcode.sol index 04747e7f..43ee4d88 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/545_warn_about_address_members_on_contract_callcode.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/545_warn_about_address_members_on_contract_callcode.sol @@ -5,4 +5,4 @@ contract C { } // ---- // Warning: (52-65): Using contract member "callcode" inherited from the address type is deprecated. Convert the contract to "address" type to access the member, for example use "address(contract).callcode" instead. -// Warning: (52-65): "callcode" has been deprecated in favour of "delegatecall". +// TypeError: (52-65): "callcode" has been deprecated in favour of "delegatecall". diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/551_warn_about_address_members_on_non_this_contract_callcode.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/551_warn_about_address_members_on_non_this_contract_callcode.sol index 9ab6fb0c..3c1e0280 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/551_warn_about_address_members_on_non_this_contract_callcode.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/551_warn_about_address_members_on_non_this_contract_callcode.sol @@ -6,4 +6,4 @@ contract C { } // ---- // Warning: (65-75): Using contract member "callcode" inherited from the address type is deprecated. Convert the contract to "address" type to access the member, for example use "address(contract).callcode" instead. -// Warning: (65-75): "callcode" has been deprecated in favour of "delegatecall". +// TypeError: (65-75): "callcode" has been deprecated in favour of "delegatecall". |