From d71cd3aa2b235f877b7928b57c94159e2c16865c Mon Sep 17 00:00:00 2001 From: chriseth Date: Sun, 22 Nov 2015 20:39:10 +0100 Subject: Added the `using x for y` directive. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 22 ++++++++++++++++++++++ test/libsolidity/SolidityParser.cpp | 15 +++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 4f26fa4d..f96f7046 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2529,6 +2529,28 @@ BOOST_AUTO_TEST_CASE(member_access_parser_ambiguity) BOOST_CHECK(success(text)); } +BOOST_AUTO_TEST_CASE(using_for_library) +{ + char const* text = R"( + library D { } + contract C { + using D for uint; + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_not_library) +{ + char const* text = R"( + contract D { } + contract C { + using D for uint; + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + BOOST_AUTO_TEST_CASE(create_memory_arrays) { char const* text = R"( diff --git a/test/libsolidity/SolidityParser.cpp b/test/libsolidity/SolidityParser.cpp index 7451397e..fd9076c3 100644 --- a/test/libsolidity/SolidityParser.cpp +++ b/test/libsolidity/SolidityParser.cpp @@ -1032,6 +1032,21 @@ BOOST_AUTO_TEST_CASE(member_access_parser_ambiguity) BOOST_CHECK(successParse(text)); } +BOOST_AUTO_TEST_CASE(using_for) +{ + char const* text = R"( + contract C { + struct s { uint a; } + using LibraryName for uint; + using Library2 for *; + using Lib for s; + function f() { + } + } + )"; + BOOST_CHECK(successParse(text)); +} + BOOST_AUTO_TEST_SUITE_END() } -- cgit v1.2.3 From 93b3237c6ae526d3ff5aa0d23d48319cf705baee Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 25 Nov 2015 14:23:35 +0100 Subject: Add bound functions to types. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index f96f7046..19ee9440 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2551,6 +2551,99 @@ BOOST_AUTO_TEST_CASE(using_for_not_library) BOOST_CHECK(expectError(text) == Error::Type::TypeError); } +BOOST_AUTO_TEST_CASE(using_for_function_exists) +{ + char const* text = R"( + library D { function double(uint self) returns (uint) { return 2*self; } } + contract C { + using D for uint; + function f(uint a) { + a.double; + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_function_on_int) +{ + char const* text = R"( + library D { function double(uint self) returns (uint) { return 2*self; } } + contract C { + using D for uint; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_function_on_struct) +{ + char const* text = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + return x.mul(a); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_overload) +{ + char const* text = R"( + library D { + struct s { uint a; } + function mul(s storage self, uint x) returns (uint) { return self.a *= x; } + function mul(s storage self, bytes32 x) returns (bytes32) { } + } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + return x.mul(a); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(using_for_by_name) +{ + char const* text = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + return x.mul({x: a}); + } + } + )"; + BOOST_CHECK(success(text)); +} + +BOOST_AUTO_TEST_CASE(bound_function_in_var) +{ + char const* text = R"( + library D { struct s { uint a; } function mul(s storage self, uint x) returns (uint) { return self.a *= x; } } + contract C { + using D for D.s; + D.s x; + function f(uint a) returns (uint) { + var g = x.mul; + return g({x: a}); + } + } + )"; + BOOST_CHECK(success(text)); +} + BOOST_AUTO_TEST_CASE(create_memory_arrays) { char const* text = R"( -- cgit v1.2.3 From f9e52c9db1ef23000f5721a462aba3fa8d681749 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 27 Nov 2015 22:24:00 +0100 Subject: Also check the object type for bound functions. --- test/libsolidity/SolidityNameAndTypeResolution.cpp | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 19ee9440..73a9b660 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2628,6 +2628,51 @@ BOOST_AUTO_TEST_CASE(using_for_by_name) BOOST_CHECK(success(text)); } +BOOST_AUTO_TEST_CASE(using_for_mismatch) +{ + char const* text = R"( + library D { function double(bytes32 self) returns (uint) { return 2; } } + contract C { + using D for uint; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(using_for_not_used) +{ + // This is an error because the function is only bound to uint. + // Had it been bound to *, it would have worked. + char const* text = R"( + library D { function double(uint self) returns (uint) { return 2; } } + contract C { + using D for uint; + function f(uint16 a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + +BOOST_AUTO_TEST_CASE(using_for_arbitrary_mismatch) +{ + // Bound to a, but self type does not match. + char const* text = R"( + library D { function double(bytes32 self) returns (uint) { return 2; } } + contract C { + using D for *; + function f(uint a) returns (uint) { + return a.double(); + } + } + )"; + BOOST_CHECK(expectError(text) == Error::Type::TypeError); +} + BOOST_AUTO_TEST_CASE(bound_function_in_var) { char const* text = R"( -- cgit v1.2.3