Some more scoping tests.

This commit is contained in:
chriseth 2018-02-26 21:14:20 +01:00
parent 6391a36a6c
commit d64aa0eaad
2 changed files with 102 additions and 1 deletions

View File

@ -298,10 +298,38 @@ BOOST_AUTO_TEST_CASE(C99_scoping_activation)
}
return x;
}
function g() pure public returns (uint x) {
x = 7;
{
x = 3;
uint x;
return x; // This returns the new variable, i.e. 0
}
}
function h() pure public returns (uint x, uint a, uint b) {
x = 7;
{
x = 3;
a = x; // This should read from the outer
uint x = 4;
b = x;
}
}
function i() pure public returns (uint x, uint a) {
x = 7;
{
x = 3;
uint x = x; // This should read from the outer and assign to the inner
a = x;
}
}
}
)";
compileAndRun(sourceCode);
ABI_CHECK(callContractFunction("f()"), encodeArgs(3));
ABI_CHECK(callContractFunction("g()"), encodeArgs(0));
ABI_CHECK(callContractFunction("h()"), encodeArgs(3, 3, 4));
ABI_CHECK(callContractFunction("i()"), encodeArgs(3, 3));
}
BOOST_AUTO_TEST_CASE(recursive_calls)

View File

@ -106,6 +106,67 @@ BOOST_AUTO_TEST_CASE(double_variable_declaration_050)
}));
}
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope)
{
string text = R"(
contract test {
function f() pure public {
{ uint x; }
{ uint x; }
}
}
)";
CHECK_ERROR(text, DeclarationError, "Identifier already declared");
}
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope_050)
{
string text = R"(
pragma experimental "v0.5.0";
contract test {
function f() pure public {
{ uint x; }
{ uint x; }
}
}
)";
CHECK_WARNING_ALLOW_MULTI(text, (vector<string>{
"Experimental features",
"Unused local variable",
"Unused local variable"
}));
}
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope_activation)
{
string text = R"(
contract test {
function f() pure public {
{ uint x; }
uint x;
}
}
)";
CHECK_ERROR(text, DeclarationError, "Identifier already declared");
}
BOOST_AUTO_TEST_CASE(double_variable_declaration_disjoint_scope_activation_050)
{
string text = R"(
pragma experimental "v0.5.0";
contract test {
function f() pure public {
{ uint x; }
uint x;
}
}
)";
CHECK_WARNING_ALLOW_MULTI(text, (vector<string>{
"Experimental features",
"Unused local variable",
"Unused local variable"
}));
}
BOOST_AUTO_TEST_CASE(scoping_old)
{
char const* text = R"(
@ -163,11 +224,23 @@ BOOST_AUTO_TEST_CASE(scoping_activation)
}
BOOST_AUTO_TEST_CASE(scoping_self_use)
{
char const* text = R"(
contract test {
function f() pure public {
uint a = a;
}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
}
BOOST_AUTO_TEST_CASE(scoping_self_use_050)
{
char const* text = R"(
pragma experimental "v0.5.0";
contract test {
function f() public {
function f() pure public {
uint a = a;
}
}