diff --git a/SolidityEndToEndTest.cpp b/SolidityEndToEndTest.cpp index 259123db6..0325c4c6a 100644 --- a/SolidityEndToEndTest.cpp +++ b/SolidityEndToEndTest.cpp @@ -2499,6 +2499,41 @@ BOOST_AUTO_TEST_CASE(struct_copy_via_local) BOOST_CHECK(callContractFunction("test()") == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(using_enums) +{ + char const* sourceCode = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoStraight, Sit } + function test() + { + choices = ActionChoices.GoStraight; + } + function getChoice() returns (uint d) + { + d = uint256(choices); + } + ActionChoices choices; + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("getChoice()") == encodeArgs(2)); +} + +BOOST_AUTO_TEST_CASE(constructing_enums_from_ints) +{ + char const* sourceCode = R"( + contract c { + enum Truth { False, True } + function test() returns (uint) + { + return uint(Truth(uint8(0x701))); + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("test()") == encodeArgs(1)); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/SolidityInterface.cpp b/SolidityInterface.cpp index 78de2356a..a73c118bb 100644 --- a/SolidityInterface.cpp +++ b/SolidityInterface.cpp @@ -111,24 +111,6 @@ BOOST_AUTO_TEST_CASE(exclude_fallback_function) BOOST_CHECK_EQUAL(getSourcePart(contract), "contract test{}"); } -BOOST_AUTO_TEST_CASE(event) -{ - ContractDefinition const& contract = checkInterface( - "contract test { event Event; }"); - BOOST_REQUIRE_EQUAL(1, contract.getEvents().size()); - BOOST_CHECK_EQUAL(getSourcePart(*contract.getEvents().front()), - "event Event;"); -} - -BOOST_AUTO_TEST_CASE(event_arguments) -{ - ContractDefinition const& contract = checkInterface( - "contract test { event Event(uint a, uint indexed b); }"); - BOOST_REQUIRE_EQUAL(1, contract.getEvents().size()); - BOOST_CHECK_EQUAL(getSourcePart(*contract.getEvents().front()), - "event Event(uint256 a,uint256 indexed b);"); -} - BOOST_AUTO_TEST_CASE(events) { char const* sourceCode = "contract test {\n" @@ -137,10 +119,8 @@ BOOST_AUTO_TEST_CASE(events) " event e2(); \n" "}\n"; ContractDefinition const& contract = checkInterface(sourceCode); - set expectation({"event e1(uint256 b,address indexed c);", "event e2;"}); - BOOST_REQUIRE_EQUAL(2, contract.getEvents().size()); - BOOST_CHECK(expectation == set({getSourcePart(*contract.getEvents().at(0)), - getSourcePart(*contract.getEvents().at(1))})); + // events should not appear in the Solidity Interface + BOOST_REQUIRE_EQUAL(0, contract.getEvents().size()); } BOOST_AUTO_TEST_CASE(inheritance) @@ -155,12 +135,8 @@ BOOST_AUTO_TEST_CASE(inheritance) " event derivedEvent(uint indexed evtArgDerived); \n" " }"; ContractDefinition const& contract = checkInterface(sourceCode); - set expectedEvents({"event derivedEvent(uint256 indexed evtArgDerived);", - "event baseEvent(string32 indexed evtArgBase);"}); set expectedFunctions({"function baseFunction(uint256 p)returns(uint256 i){}", "function derivedFunction(string32 p)returns(string32 i){}"}); - BOOST_CHECK(expectedEvents == set({getSourcePart(*contract.getEvents().at(0)), - getSourcePart(*contract.getEvents().at(1))})); BOOST_REQUIRE_EQUAL(2, contract.getDefinedFunctions().size()); BOOST_CHECK(expectedFunctions == set({getSourcePart(*contract.getDefinedFunctions().at(0)), getSourcePart(*contract.getDefinedFunctions().at(1))})); diff --git a/SolidityNameAndTypeResolution.cpp b/SolidityNameAndTypeResolution.cpp index 3ead6f1c5..d6e4ed516 100644 --- a/SolidityNameAndTypeResolution.cpp +++ b/SolidityNameAndTypeResolution.cpp @@ -992,6 +992,97 @@ BOOST_AUTO_TEST_CASE(exp_operator_exponent_too_big) BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError); } +BOOST_AUTO_TEST_CASE(enum_member_access) +{ + char const* text = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoStraight, Sit } + function test() + { + choices = ActionChoices.GoStraight; + } + ActionChoices choices; + } + )"; + BOOST_CHECK_NO_THROW(parseTextAndResolveNamesWithChecks(text)); +} + +BOOST_AUTO_TEST_CASE(enum_invalid_member_access) +{ + char const* text = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoStraight, Sit } + function test() + { + choices = ActionChoices.RunAroundWavingYourHands; + } + ActionChoices choices; + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(enum_explicit_conversion_is_okay) +{ + char const* text = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoStraight, Sit } + function test() + { + a = uint256(ActionChoices.GoStraight); + b = uint64(ActionChoices.Sit); + } + uint256 a; + uint64 b; + } + )"; + BOOST_CHECK_NO_THROW(parseTextAndResolveNamesWithChecks(text)); +} + +BOOST_AUTO_TEST_CASE(int_to_enum_explicit_conversion_is_okay) +{ + char const* text = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoStraight, Sit } + function test() + { + a = 2; + b = ActionChoices(a); + } + uint256 a; + ActionChoices b; + } + )"; + BOOST_CHECK_NO_THROW(parseTextAndResolveNamesWithChecks(text)); +} + +BOOST_AUTO_TEST_CASE(enum_implicit_conversion_is_not_okay) +{ + char const* text = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoStraight, Sit } + function test() + { + a = ActionChoices.GoStraight; + b = ActionChoices.Sit; + } + uint256 a; + uint64 b; + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); +} + +BOOST_AUTO_TEST_CASE(enum_duplicate_values) +{ + char const* text = R"( + contract test { + enum ActionChoices { GoLeft, GoRight, GoLeft, Sit } + } + )"; + BOOST_CHECK_THROW(parseTextAndResolveNames(text), DeclarationError); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/SolidityParser.cpp b/SolidityParser.cpp index 84f36170f..af82f612a 100644 --- a/SolidityParser.cpp +++ b/SolidityParser.cpp @@ -703,6 +703,38 @@ BOOST_AUTO_TEST_CASE(literal_constants_with_ether_subdenominations_in_expression BOOST_CHECK_NO_THROW(parseTextExplainError(text)); } +BOOST_AUTO_TEST_CASE(enum_valid_declaration) +{ + char const* text = R"( + contract c { + enum validEnum { Value1, Value2, Value3, Value4 } + function c () + { + a = foo.Value3; + } + uint256 a; + })"; + BOOST_CHECK_NO_THROW(parseTextExplainError(text)); +} + +BOOST_AUTO_TEST_CASE(empty_enum_declaration) +{ + char const* text = R"( + contract c { + enum foo { } + })"; + BOOST_CHECK_NO_THROW(parseTextExplainError(text)); +} + +BOOST_AUTO_TEST_CASE(malformed_enum_declaration) +{ + char const* text = R"( + contract c { + enum foo { WARNING,} + })"; + BOOST_CHECK_THROW(parseText(text), ParserError); +} + BOOST_AUTO_TEST_SUITE_END() } diff --git a/webthreestubclient.h b/webthreestubclient.h index 1836f6506..02f5b5e40 100644 --- a/webthreestubclient.h +++ b/webthreestubclient.h @@ -10,7 +10,7 @@ class WebThreeStubClient : public jsonrpc::Client { public: - WebThreeStubClient(jsonrpc::IClientConnector &conn) : jsonrpc::Client(conn) {} + WebThreeStubClient(jsonrpc::IClientConnector &conn, jsonrpc::clientVersion_t type = jsonrpc::JSONRPC_CLIENT_V2) : jsonrpc::Client(conn, type) {} std::string web3_sha3(const std::string& param1) throw (jsonrpc::JsonRpcException) { @@ -52,7 +52,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - bool eth_setListening(const bool& param1) throw (jsonrpc::JsonRpcException) + bool eth_setListening(bool param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -72,7 +72,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - bool eth_setMining(const bool& param1) throw (jsonrpc::JsonRpcException) + bool eth_setMining(bool param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -122,7 +122,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - bool eth_setDefaultBlock(const int& param1) throw (jsonrpc::JsonRpcException) + bool eth_setDefaultBlock(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -233,7 +233,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_blockByNumber(const int& param1) throw (jsonrpc::JsonRpcException) + Json::Value eth_blockByNumber(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -243,7 +243,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_transactionByHash(const std::string& param1, const int& param2) throw (jsonrpc::JsonRpcException) + Json::Value eth_transactionByHash(const std::string& param1, int param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -254,7 +254,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_transactionByNumber(const int& param1, const int& param2) throw (jsonrpc::JsonRpcException) + Json::Value eth_transactionByNumber(int param1, int param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -265,7 +265,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_uncleByHash(const std::string& param1, const int& param2) throw (jsonrpc::JsonRpcException) + Json::Value eth_uncleByHash(const std::string& param1, int param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -276,7 +276,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_uncleByNumber(const int& param1, const int& param2) throw (jsonrpc::JsonRpcException) + Json::Value eth_uncleByNumber(int param1, int param2) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -347,7 +347,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - bool eth_uninstallFilter(const int& param1) throw (jsonrpc::JsonRpcException) + bool eth_uninstallFilter(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -357,7 +357,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_changed(const int& param1) throw (jsonrpc::JsonRpcException) + Json::Value eth_changed(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -367,7 +367,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value eth_filterLogs(const int& param1) throw (jsonrpc::JsonRpcException) + Json::Value eth_filterLogs(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -515,7 +515,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - bool shh_uninstallFilter(const int& param1) throw (jsonrpc::JsonRpcException) + bool shh_uninstallFilter(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -525,7 +525,7 @@ class WebThreeStubClient : public jsonrpc::Client else throw jsonrpc::JsonRpcException(jsonrpc::Errors::ERROR_CLIENT_INVALID_RESPONSE, result.toStyledString()); } - Json::Value shh_changed(const int& param1) throw (jsonrpc::JsonRpcException) + Json::Value shh_changed(int param1) throw (jsonrpc::JsonRpcException) { Json::Value p; p.append(param1); @@ -537,4 +537,4 @@ class WebThreeStubClient : public jsonrpc::Client } }; -#endif //JSONRPC_CPP_WEBTHREESTUBCLIENT_H_ +#endif //JSONRPC_CPP_STUB_WEBTHREESTUBCLIENT_H_