mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
- added more tests to check constant specifier implementation
- deny use of const for local variables - deny unitialized const variables - only int, fixed strings, and enums can be declaired as const
This commit is contained in:
parent
8e6b2c10c3
commit
1d2e579eff
@ -3196,26 +3196,28 @@ BOOST_AUTO_TEST_CASE(pass_dynamic_arguments_to_the_base_base_with_gap)
|
||||
BOOST_CHECK(callContractFunction("m_i()") == encodeArgs(4));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(constant_functions)
|
||||
BOOST_AUTO_TEST_CASE(simple_constant_variables_test)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract Foo {
|
||||
function getX() constant returns (uint r) { r = x; }
|
||||
uint x = 56;
|
||||
function getX() returns (uint r) { return x; }
|
||||
uint constant x = 56;
|
||||
})";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("getX()") == encodeArgs(56));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(const_state_variable)
|
||||
BOOST_AUTO_TEST_CASE(constant_variables)
|
||||
{
|
||||
//for now constant specifier is valid only for uint bytesXX and enums
|
||||
char const* sourceCode = R"(
|
||||
contract Foo {
|
||||
function getX() constant returns (uint r) { return x; }
|
||||
uint constant x = 56;
|
||||
enum ActionChoices { GoLeft, GoRight, GoStraight, Sit }
|
||||
ActionChoices constant choices = ActionChoices.GoLeft;
|
||||
bytes32 constant st = "abc\x00\xff__";
|
||||
})";
|
||||
compileAndRun(sourceCode);
|
||||
BOOST_CHECK(callContractFunction("getX()") == encodeArgs(56));
|
||||
}
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
@ -1404,7 +1404,7 @@ BOOST_AUTO_TEST_CASE(test_byte_is_alias_of_byte1)
|
||||
ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(text), "Type resolving failed");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(const_state_variable)
|
||||
BOOST_AUTO_TEST_CASE(assigning_value_to_const_variable)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract Foo {
|
||||
@ -1414,6 +1414,38 @@ BOOST_AUTO_TEST_CASE(const_state_variable)
|
||||
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(complex_const_variable)
|
||||
{
|
||||
//for now constant specifier is valid only for uint bytesXX and enums
|
||||
char const* text = R"(
|
||||
contract Foo {
|
||||
mapping(uint => bool) constant mapVar;
|
||||
})";
|
||||
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(uninitialized_const_variable)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract Foo {
|
||||
uint constant y;
|
||||
})";
|
||||
BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(local_const_variable)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract Foo {
|
||||
function localConst() returns (uint ret)
|
||||
{
|
||||
uint constant local = 4;
|
||||
return local;
|
||||
}
|
||||
})";
|
||||
BOOST_CHECK_THROW(parseTextAndResolveNames(text), ParserError);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -822,6 +822,15 @@ BOOST_AUTO_TEST_CASE(multi_arrays)
|
||||
ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(constant_is_keyword)
|
||||
{
|
||||
char const* text = R"(
|
||||
contract Foo {
|
||||
uint constant = 4;
|
||||
})";
|
||||
BOOST_CHECK_THROW(parseText(text), ParserError);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user