2015-01-31 23:46:38 +00:00
|
|
|
/*
|
|
|
|
This file is part of cpp-ethereum.
|
|
|
|
|
|
|
|
cpp-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
cpp-ethereum is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Unit tests for generating source interfaces for Solidity contracts.
|
|
|
|
*/
|
|
|
|
|
2015-04-20 20:48:53 +00:00
|
|
|
#include "../TestHelper.h"
|
2015-01-31 23:46:38 +00:00
|
|
|
#include <libsolidity/CompilerStack.h>
|
|
|
|
#include <libsolidity/AST.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
class SolidityInterfaceChecker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SolidityInterfaceChecker(): m_compilerStack(false) {}
|
|
|
|
|
|
|
|
/// Compiles the given code, generates the interface and parses that again.
|
|
|
|
ContractDefinition const& checkInterface(string const& _code, string const& _contractName = "")
|
|
|
|
{
|
|
|
|
m_code = _code;
|
2015-03-06 11:06:12 +00:00
|
|
|
ETH_TEST_REQUIRE_NO_THROW(m_compilerStack.parse(_code), "Parsing failed");
|
2015-08-31 16:44:29 +00:00
|
|
|
m_interface = m_compilerStack.metadata("", DocumentationType::ABISolidityInterface);
|
2015-03-06 11:06:12 +00:00
|
|
|
ETH_TEST_REQUIRE_NO_THROW(m_reCompiler.parse(m_interface), "Interface parsing failed");
|
2015-08-31 16:44:29 +00:00
|
|
|
return m_reCompiler.contractDefinition(_contractName);
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 09:19:02 +00:00
|
|
|
string sourcePart(ASTNode const& _node) const
|
2015-01-31 23:46:38 +00:00
|
|
|
{
|
2015-08-31 16:44:29 +00:00
|
|
|
SourceLocation location = _node.location();
|
2015-01-31 23:46:38 +00:00
|
|
|
BOOST_REQUIRE(!location.isEmpty());
|
|
|
|
return m_interface.substr(location.start, location.end - location.start);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
string m_code;
|
|
|
|
string m_interface;
|
|
|
|
CompilerStack m_compilerStack;
|
|
|
|
CompilerStack m_reCompiler;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(SolidityInterface, SolidityInterfaceChecker)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(empty_contract)
|
|
|
|
{
|
|
|
|
ContractDefinition const& contract = checkInterface("contract test {}");
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_CHECK_EQUAL(sourcePart(contract), "contract test{}");
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(single_function)
|
|
|
|
{
|
|
|
|
ContractDefinition const& contract = checkInterface(
|
|
|
|
"contract test {\n"
|
|
|
|
" function f(uint a) returns(uint d) { return a * 7; }\n"
|
|
|
|
"}\n");
|
2015-08-31 16:44:29 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size());
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_CHECK_EQUAL(sourcePart(*contract.definedFunctions().front()),
|
2015-04-23 12:45:50 +00:00
|
|
|
"function f(uint256 a)returns(uint256 d);");
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(single_constant_function)
|
|
|
|
{
|
|
|
|
ContractDefinition const& contract = checkInterface(
|
2015-03-11 10:38:11 +00:00
|
|
|
"contract test { function f(uint a) constant returns(bytes1 x) { 1==2; } }");
|
2015-08-31 16:44:29 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(1, contract.definedFunctions().size());
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_CHECK_EQUAL(sourcePart(*contract.definedFunctions().front()),
|
2015-04-23 12:45:50 +00:00
|
|
|
"function f(uint256 a)constant returns(bytes1 x);");
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multiple_functions)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint a) returns(uint d) { return a * 7; }\n"
|
|
|
|
" function g(uint b) returns(uint e) { return b * 8; }\n"
|
|
|
|
"}\n";
|
|
|
|
ContractDefinition const& contract = checkInterface(sourceCode);
|
2015-04-23 12:45:50 +00:00
|
|
|
set<string> expectation({"function f(uint256 a)returns(uint256 d);",
|
|
|
|
"function g(uint256 b)returns(uint256 e);"});
|
2015-08-31 16:44:29 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(2, contract.definedFunctions().size());
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_CHECK(expectation == set<string>({sourcePart(*contract.definedFunctions().at(0)),
|
|
|
|
sourcePart(*contract.definedFunctions().at(1))}));
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(exclude_fallback_function)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test { function() {} }";
|
|
|
|
ContractDefinition const& contract = checkInterface(sourceCode);
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_CHECK_EQUAL(sourcePart(contract), "contract test{}");
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(events)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint a) returns(uint d) { return a * 7; }\n"
|
|
|
|
" event e1(uint b, address indexed c); \n"
|
|
|
|
" event e2(); \n"
|
|
|
|
"}\n";
|
|
|
|
ContractDefinition const& contract = checkInterface(sourceCode);
|
2015-02-16 16:15:04 +00:00
|
|
|
// events should not appear in the Solidity Interface
|
2015-08-31 16:44:29 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(0, contract.events().size());
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(inheritance)
|
|
|
|
{
|
|
|
|
char const* sourceCode =
|
|
|
|
" contract Base { \n"
|
|
|
|
" function baseFunction(uint p) returns (uint i) { return p; } \n"
|
2015-03-11 10:38:11 +00:00
|
|
|
" event baseEvent(bytes32 indexed evtArgBase); \n"
|
2015-01-31 23:46:38 +00:00
|
|
|
" } \n"
|
|
|
|
" contract Derived is Base { \n"
|
2015-03-11 10:38:11 +00:00
|
|
|
" function derivedFunction(bytes32 p) returns (bytes32 i) { return p; } \n"
|
2015-01-31 23:46:38 +00:00
|
|
|
" event derivedEvent(uint indexed evtArgDerived); \n"
|
|
|
|
" }";
|
|
|
|
ContractDefinition const& contract = checkInterface(sourceCode);
|
2015-04-23 12:45:50 +00:00
|
|
|
set<string> expectedFunctions({"function baseFunction(uint256 p)returns(uint256 i);",
|
|
|
|
"function derivedFunction(bytes32 p)returns(bytes32 i);"});
|
2015-08-31 16:44:29 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(2, contract.definedFunctions().size());
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_CHECK(expectedFunctions == set<string>({sourcePart(*contract.definedFunctions().at(0)),
|
|
|
|
sourcePart(*contract.definedFunctions().at(1))}));
|
2015-01-31 23:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|