diff options
author | Gav Wood <g@ethdev.com> | 2015-01-27 07:01:25 +0800 |
---|---|---|
committer | Gav Wood <g@ethdev.com> | 2015-01-27 07:01:25 +0800 |
commit | dd87fbccd5a576eb5cf1d25a969328b0de103bfc (patch) | |
tree | 59280eed0ffd55d130ed9cfc4343aae12a58aac0 /SolidityEndToEndTest.cpp | |
parent | f4a92d4caf4db61c3551881e8302909a98006878 (diff) | |
parent | 2b2da431bc52610e8528ce83e5b59d2588fc7f2c (diff) | |
download | dexon-solidity-dd87fbccd5a576eb5cf1d25a969328b0de103bfc.tar.gz dexon-solidity-dd87fbccd5a576eb5cf1d25a969328b0de103bfc.tar.zst dexon-solidity-dd87fbccd5a576eb5cf1d25a969328b0de103bfc.zip |
Merge pull request #856 from chriseth/sol_modifiers
Function modifiers.
Diffstat (limited to 'SolidityEndToEndTest.cpp')
-rw-r--r-- | SolidityEndToEndTest.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 80e9aed4..22db67dd 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -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() } |