diff options
Diffstat (limited to 'test/libsolidity')
-rw-r--r-- | test/libsolidity/Imports.cpp | 100 | ||||
-rw-r--r-- | test/libsolidity/SolidityExpressionCompiler.cpp | 25 | ||||
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 26 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 42 |
4 files changed, 184 insertions, 9 deletions
diff --git a/test/libsolidity/Imports.cpp b/test/libsolidity/Imports.cpp new file mode 100644 index 00000000..ab8e2257 --- /dev/null +++ b/test/libsolidity/Imports.cpp @@ -0,0 +1,100 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum 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. + + cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. +*/ +/** + * @author Christian <c@ethdev.com> + * @date 2015 + * Tests for high level features like import. + */ + +#include <string> +#include <boost/test/unit_test.hpp> +#include <libsolidity/interface/Exceptions.h> +#include <libsolidity/interface/CompilerStack.h> + +using namespace std; + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +BOOST_AUTO_TEST_SUITE(SolidityImports) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + CompilerStack c; + c.addSource("a", "contract C {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(regular_import) +{ + CompilerStack c; + c.addSource("a", "contract C {}"); + c.addSource("b", "import \"a\"; contract D is C {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(import_does_not_clutter_importee) +{ + CompilerStack c; + c.addSource("a", "contract C { D d; }"); + c.addSource("b", "import \"a\"; contract D is C {}"); + BOOST_CHECK(!c.compile()); +} + +BOOST_AUTO_TEST_CASE(import_is_transitive) +{ + CompilerStack c; + c.addSource("a", "contract C { }"); + c.addSource("b", "import \"a\";"); + c.addSource("c", "import \"b\"; contract D is C {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(circular_import) +{ + CompilerStack c; + c.addSource("a", "import \"b\"; contract C { D d; }"); + c.addSource("b", "import \"a\"; contract D { C c; }"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(relative_import) +{ + CompilerStack c; + c.addSource("a", "import \"./dir/b\"; contract A is B {}"); + c.addSource("dir/b", "contract B {}"); + c.addSource("dir/c", "import \"../a\"; contract C is A {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(relative_import_multiplex) +{ + CompilerStack c; + c.addSource("a", "contract A {}"); + c.addSource("dir/a/b/c", "import \"../../.././a\"; contract B is A {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} // end namespaces diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index a622c738..0e814e56 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -71,18 +71,21 @@ private: }; Declaration const& resolveDeclaration( - vector<string> const& _namespacedName, NameAndTypeResolver const& _resolver) + SourceUnit const& _sourceUnit, + vector<string> const& _namespacedName, + NameAndTypeResolver const& _resolver +) { - Declaration const* declaration = nullptr; + ASTNode const* scope = &_sourceUnit; // bracers are required, cause msvc couldnt handle this macro in for statement for (string const& namePart: _namespacedName) { - auto declarations = _resolver.resolveName(namePart, declaration); + auto declarations = _resolver.resolveName(namePart, scope); BOOST_REQUIRE(!declarations.empty()); - BOOST_REQUIRE(declaration = *declarations.begin()); + BOOST_REQUIRE(scope = *declarations.begin()); } - BOOST_REQUIRE(declaration); - return *declaration; + BOOST_REQUIRE(scope); + return dynamic_cast<Declaration const&>(*scope); } bytes compileFirstExpression( @@ -140,13 +143,17 @@ bytes compileFirstExpression( unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack context.adjustStackOffset(parametersSize); for (vector<string> const& variable: _localVariables) - context.addVariable(dynamic_cast<VariableDeclaration const&>(resolveDeclaration(variable, resolver)), - parametersSize--); + context.addVariable( + dynamic_cast<VariableDeclaration const&>(resolveDeclaration(*sourceUnit, variable, resolver)), + parametersSize-- + ); ExpressionCompiler(context).compile(*extractor.expression()); for (vector<string> const& function: _functions) - context << context.functionEntryLabel(dynamic_cast<FunctionDefinition const&>(resolveDeclaration(function, resolver))); + context << context.functionEntryLabel(dynamic_cast<FunctionDefinition const&>( + resolveDeclaration(*sourceUnit, function, resolver) + )); bytes instructions = context.assembledObject().bytecode; // debug // cout << eth::disassemble(instructions) << endl; diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 73a9b660..1242e801 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2743,6 +2743,32 @@ BOOST_AUTO_TEST_CASE(invalid_args_creating_memory_array) BOOST_CHECK(expectError(text) == Error::Type::TypeError); } +/*BOOST_AUTO_TEST_CASE(inline_array_declaration_and_passing) +{ + char const* text = R"( + contract C { + uint[] a; + function f() returns (uint, uint) { + a = [1,2,3]; + return (a[3], [3,4][0]); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(invalid_types_in_inline_array) +{ + char const* text = R"( + contract C { + function f() { + uint[] x = [45, "foo", true]; + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +}*/ + BOOST_AUTO_TEST_SUITE_END() } diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index fd9076c3..e6c8a8d8 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -1047,6 +1047,48 @@ BOOST_AUTO_TEST_CASE(using_for) BOOST_CHECK(successParse(text)); } +BOOST_AUTO_TEST_CASE(inline_array_declaration) +{ + char const* text = R"( + contract c { + uint[] a; + function f() returns (uint, uint) { + a = [1,2,3]; + return (a[3], [2,3,4][0]); + } + } + )"; + BOOST_CHECK(successParse(text)); +} + + +BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_lvalue) +{ + char const* text = R"( + contract c { + uint[] a; + function f() returns (uint) { + a = [,2,3]; + return (a[0]); + } + } + )"; + BOOST_CHECK(!successParse(text)); +} + +BOOST_AUTO_TEST_CASE(inline_array_empty_cells_check_without_lvalue) +{ + char const* text = R"( + contract c { + uint[] a; + function f() returns (uint, uint) { + return ([3, ,4][0]); + } + } + )"; + BOOST_CHECK(!successParse(text)); +} + BOOST_AUTO_TEST_SUITE_END() } |