diff options
author | chriseth <c@ethdev.com> | 2015-11-30 23:06:44 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-11-30 23:06:44 +0800 |
commit | e9c7837c154482a72c8519fbdc9376693ce9a1d5 (patch) | |
tree | 1985b57b4e221fe4e4b5cc14a5049b8c0c46ce6c /test/libsolidity | |
parent | a8736b7b271dac117f15164cf4d2dfabcdd2c6fd (diff) | |
parent | f9e52c9db1ef23000f5721a462aba3fa8d681749 (diff) | |
download | dexon-solidity-e9c7837c154482a72c8519fbdc9376693ce9a1d5.tar.gz dexon-solidity-e9c7837c154482a72c8519fbdc9376693ce9a1d5.tar.zst dexon-solidity-e9c7837c154482a72c8519fbdc9376693ce9a1d5.zip |
Merge pull request #251 from chriseth/bind2
Bind library functions to types.
Diffstat (limited to 'test/libsolidity')
-rw-r--r-- | test/libsolidity/SolidityNameAndTypeResolution.cpp | 160 | ||||
-rw-r--r-- | test/libsolidity/SolidityParser.cpp | 15 |
2 files changed, 175 insertions, 0 deletions
diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 4f26fa4d..73a9b660 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -2529,6 +2529,166 @@ 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(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(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"( + 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"( 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() } |