mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #856 from chriseth/sol_modifiers
Function modifiers.
This commit is contained in:
@@ -1650,6 +1650,127 @@ BOOST_AUTO_TEST_CASE(constructor_argument_overriding)
|
||||
BOOST_CHECK(callContractFunction("getA()") == encodeArgs(3));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
function getOne() nonFree returns (uint r) { return 1; }
|
||||
modifier nonFree { if (msg.value > 0) _ }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("getOne()") == encodeArgs(0));
|
||||
BOOST_CHECK(callContractFunctionWithValue("getOne()", 1) == encodeArgs(1));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_local_variables)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
modifier mod1 { var a = 1; var b = 2; _ }
|
||||
modifier mod2(bool a) { if (a) return; else _ }
|
||||
function f(bool a) mod1 mod2(a) returns (uint r) { return 3; }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(0));
|
||||
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(3));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_loop)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
modifier repeat(uint count) { for (var i = 0; i < count; ++i) _ }
|
||||
function f() repeat(10) returns (uint r) { r += 1; }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f()") == encodeArgs(10));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_multi_invocation)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
modifier repeat(bool twice) { if (twice) _ _ }
|
||||
function f(bool twice) repeat(twice) returns (uint r) { r += 1; }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
|
||||
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(2));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_multi_with_return)
|
||||
{
|
||||
// Here, the explicit return prevents the second execution
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
modifier repeat(bool twice) { if (twice) _ _ }
|
||||
function f(bool twice) repeat(twice) returns (uint r) { r += 1; return r; }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f(bool)", false) == encodeArgs(1));
|
||||
BOOST_CHECK(callContractFunction("f(bool)", true) == encodeArgs(1));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_overriding)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract A {
|
||||
function f() mod returns (bool r) { return true; }
|
||||
modifier mod { _ }
|
||||
}
|
||||
contract C is A {
|
||||
modifier mod { }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("f()") == encodeArgs(false));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_calling_functions_in_creation_context)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract A {
|
||||
uint data;
|
||||
function A() mod1 { f1(); }
|
||||
function f1() mod2 { data |= 0x1; }
|
||||
function f2() { data |= 0x20; }
|
||||
function f3() { }
|
||||
modifier mod1 { f2(); _ }
|
||||
modifier mod2 { f3(); }
|
||||
function getData() returns (uint r) { return data; }
|
||||
}
|
||||
contract C is A {
|
||||
modifier mod1 { f4(); _ }
|
||||
function f3() { data |= 0x300; }
|
||||
function f4() { data |= 0x4000; }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(0x4300));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(function_modifier_for_constructor)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract A {
|
||||
uint data;
|
||||
function A() mod1 { data |= 2; }
|
||||
modifier mod1 { data |= 1; _ }
|
||||
function getData() returns (uint r) { return data; }
|
||||
}
|
||||
contract C is A {
|
||||
modifier mod1 { data |= 4; _ }
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("getData()") == encodeArgs(4 | 2));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user