Fallback functions.

This commit is contained in:
Christian 2015-01-29 22:50:20 +01:00
parent 5da76a3bbd
commit a55a99a2b0
5 changed files with 101 additions and 5 deletions

View File

@ -273,6 +273,15 @@ BOOST_AUTO_TEST_CASE(const_function)
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_CASE(exclude_fallback_function)
{
char const* sourceCode = "contract test { function() {} }";
char const* interface = "[]";
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(smoke_test)
"}\n";
bytes code = compileContract(sourceCode);
unsigned boilerplateSize = 73;
unsigned boilerplateSize = 69;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x0, // initialize local variable x
byte(Instruction::PUSH1), 0x2,
@ -114,8 +114,8 @@ BOOST_AUTO_TEST_CASE(ifStatement)
" function f() { bool x; if (x) 77; else if (!x) 78; else 79; }"
"}\n";
bytes code = compileContract(sourceCode);
unsigned shift = 60;
unsigned boilerplateSize = 73;
unsigned shift = 56;
unsigned boilerplateSize = 69;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x0,
byte(Instruction::DUP1),
@ -155,8 +155,8 @@ BOOST_AUTO_TEST_CASE(loops)
" function f() { while(true){1;break;2;continue;3;return;4;} }"
"}\n";
bytes code = compileContract(sourceCode);
unsigned shift = 60;
unsigned boilerplateSize = 73;
unsigned shift = 56;
unsigned boilerplateSize = 69;
bytes expectation({byte(Instruction::JUMPDEST),
byte(Instruction::JUMPDEST),
byte(Instruction::PUSH1), 0x1,

View File

@ -1955,6 +1955,37 @@ BOOST_AUTO_TEST_CASE(super_in_constructor)
BOOST_CHECK(callContractFunction("f()") == encodeArgs(1 | 2 | 4 | 8));
}
BOOST_AUTO_TEST_CASE(fallback_function)
{
char const* sourceCode = R"(
contract A {
uint data;
function() returns (uint r) { data = 1; return 2; }
function getData() returns (uint r) { return data; }
}
)";
compileAndRun(sourceCode);
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0));
BOOST_CHECK(callContractFunction("") == encodeArgs(2));
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(1));
}
BOOST_AUTO_TEST_CASE(inherited_fallback_function)
{
char const* sourceCode = R"(
contract A {
uint data;
function() returns (uint r) { data = 1; return 2; }
function getData() returns (uint r) { return data; }
}
contract B is A {}
)";
compileAndRun(sourceCode, 0, "B");
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0));
BOOST_CHECK(callContractFunction("") == encodeArgs(2));
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(1));
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -680,6 +680,54 @@ BOOST_AUTO_TEST_CASE(private_state_variable)
BOOST_CHECK_MESSAGE(function == nullptr, "Accessor function of a private variable should not exist");
}
BOOST_AUTO_TEST_CASE(fallback_function)
{
char const* text = R"(
contract C {
uint x;
function() { x = 2; }
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_CASE(fallback_function_with_arguments)
{
char const* text = R"(
contract C {
uint x;
function(uint a) { x = 2; }
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
}
BOOST_AUTO_TEST_CASE(fallback_function_twice)
{
char const* text = R"(
contract C {
uint x;
function() { x = 2; }
function() { x = 3; }
}
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError);
}
BOOST_AUTO_TEST_CASE(fallback_function_inheritance)
{
char const* text = R"(
contract A {
uint x;
function() { x = 1; }
}
contract C is A {
function() { x = 2; }
}
)";
BOOST_CHECK_NO_THROW(parseTextAndResolveNames(text));
}
BOOST_AUTO_TEST_SUITE_END()
}

View File

@ -586,6 +586,14 @@ BOOST_AUTO_TEST_CASE(modifier_invocation)
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_CASE(fallback_function)
{
char const* text = "contract c {\n"
" function() { }\n"
"}\n";
BOOST_CHECK_NO_THROW(parseText(text));
}
BOOST_AUTO_TEST_SUITE_END()
}