Tuple expressions.

This commit is contained in:
chriseth
2015-10-15 17:38:42 +02:00
parent 7ba42f4707
commit 7ebd536e79
17 changed files with 294 additions and 18 deletions
+55
View File
@@ -5677,6 +5677,61 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration)
BOOST_CHECK(callContractFunction("f()", encodeArgs()) == encodeArgs(true));
}
BOOST_AUTO_TEST_CASE(tuples)
{
char const* sourceCode = R"(
contract C {
uint[] data;
function g() internal returns (uint a, uint b, uint[] storage c) {
return (1, 2, data);
}
function h() external returns (uint a, uint b) {
return (5, 6);
}
function f() returns (uint) {
data.length = 1;
data[0] = 3;
uint a; uint b;
(a, b) = this.h();
if (a != 1 || b != 2) return 1;
uint[] storage c;
(a, b, c) = g();
if (a != 5 || b != 6 || c[0] != 3) return 2;
(a, b) = (b, a);
if (a != 6 || b != 5) return 3;
(a, , b, ) = (8, 9, 10, 11, 12);
if (a != 8 || b != 10) return 3;
}
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0)));
}
BOOST_AUTO_TEST_CASE(destructuring_assignment_wildcard)
{
char const* sourceCode = R"(
contract C {
function f() returns (uint) {
uint a;
uint b;
uint c;
(a,) = (1,);
if (a != 1) return 1;
(,b) = (2,3,4);
if (b != 4) return 2;
(, c,) = (5,6,7);
if (c != 6) return 3;
(a, b,) = (11, 12, 13);
if (a != 11 || b != 12) return 4;
(, a, b) = (11, 12, 13);
if (a != 12 || b != 13) return 5;
}
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(0)));
}
BOOST_AUTO_TEST_SUITE_END()
}
@@ -2460,6 +2460,33 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_4)
BOOST_CHECK(expectError(text) == Error::Type::TypeError);
}
BOOST_AUTO_TEST_CASE(tuples)
{
char const* text = R"(
contract C {
function f() {
uint a = (1);
var (b,) = (1,);
var (c,d) = (1, 2 + a);
var (e,) = (1, 2, b);
}
}
)";
BOOST_CHECK_NO_THROW(parseAndAnalyse(text));
}
BOOST_AUTO_TEST_CASE(tuples_empty_components)
{
char const* text = R"(
contract C {
function f() {
(1,,2);
}
}
)";
SOLIDITY_CHECK_ERROR_TYPE(parseAndAnalyseReturnError(text), TypeError);
}
BOOST_AUTO_TEST_CASE(multi_variable_declaration_wildcards_fail_5)
{
char const* text = R"(
+16 -1
View File
@@ -983,7 +983,7 @@ BOOST_AUTO_TEST_CASE(local_const_variable)
BOOST_AUTO_TEST_CASE(multi_variable_declaration)
{
char const* text = R"(
library Lib {
contract C {
function f() {
var (a,b,c) = g();
var (d) = 2;
@@ -1000,6 +1000,21 @@ BOOST_AUTO_TEST_CASE(multi_variable_declaration)
BOOST_CHECK(successParse(text));
}
BOOST_AUTO_TEST_CASE(tuples)
{
char const* text = R"(
contract C {
function f() {
uint a = (1);
var (b,) = (1,);
var (c,d) = (1, 2 + a);
var (e,) = (1, 2, b);
}
}
)";
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_SUITE_END()
}