2014-11-12 10:39:42 +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 Marek Kotewicz <marek@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Unit tests for the solidity compiler JSON Interface output.
|
|
|
|
*/
|
|
|
|
|
2014-11-12 12:00:11 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2014-11-12 10:39:42 +00:00
|
|
|
#include <libsolidity/CompilerStack.h>
|
2014-12-08 23:58:02 +00:00
|
|
|
#include <jsoncpp/json/json.h>
|
2014-12-02 09:41:18 +00:00
|
|
|
#include <libdevcore/Exceptions.h>
|
2014-11-12 10:39:42 +00:00
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
2014-11-12 12:00:11 +00:00
|
|
|
class InterfaceChecker
|
|
|
|
{
|
2014-11-12 10:39:42 +00:00
|
|
|
public:
|
2014-12-01 17:01:42 +00:00
|
|
|
void checkInterface(std::string const& _code, std::string const& _expectedInterfaceString)
|
2014-11-12 10:39:42 +00:00
|
|
|
{
|
2014-12-02 09:41:18 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
m_compilerStack.parse(_code);
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
std::string const* extra = boost::get_error_info<errinfo_comment>(e);
|
|
|
|
std::string msg = std::string("Parsing contract failed with: ") +
|
|
|
|
e.what() + std::string("\n");
|
|
|
|
if (extra)
|
|
|
|
msg += *extra;
|
|
|
|
BOOST_FAIL(msg);
|
|
|
|
}
|
2014-12-08 11:07:17 +00:00
|
|
|
std::string generatedInterfaceString = m_compilerStack.getJsonDocumentation("", DocumentationType::ABI_INTERFACE);
|
2014-11-12 10:39:42 +00:00
|
|
|
Json::Value generatedInterface;
|
|
|
|
m_reader.parse(generatedInterfaceString, generatedInterface);
|
|
|
|
Json::Value expectedInterface;
|
|
|
|
m_reader.parse(_expectedInterfaceString, expectedInterface);
|
2014-12-01 17:01:42 +00:00
|
|
|
BOOST_CHECK_MESSAGE(expectedInterface == generatedInterface,
|
|
|
|
"Expected " << _expectedInterfaceString <<
|
|
|
|
"\n but got:\n" << generatedInterfaceString);
|
2014-11-12 10:39:42 +00:00
|
|
|
}
|
2014-12-01 17:01:42 +00:00
|
|
|
|
2014-11-12 10:39:42 +00:00
|
|
|
private:
|
|
|
|
CompilerStack m_compilerStack;
|
|
|
|
Json::Reader m_reader;
|
|
|
|
};
|
|
|
|
|
2014-12-02 09:41:18 +00:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(SolidityABIJSON, InterfaceChecker)
|
2014-11-12 10:39:42 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(basic_test)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint a) returns(uint d) { return a * 7; }\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
char const* interface = R"([
|
|
|
|
{
|
|
|
|
"name": "f",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-11-12 10:39:42 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "d",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
])";
|
|
|
|
|
2014-12-01 17:01:42 +00:00
|
|
|
checkInterface(sourceCode, interface);
|
2014-11-12 10:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(empty_contract)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
char const* interface = "[]";
|
|
|
|
|
2014-12-01 17:01:42 +00:00
|
|
|
checkInterface(sourceCode, interface);
|
2014-11-12 10:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multiple_methods)
|
|
|
|
{
|
|
|
|
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";
|
2014-12-01 17:01:42 +00:00
|
|
|
|
2014-11-12 10:39:42 +00:00
|
|
|
char const* interface = R"([
|
|
|
|
{
|
|
|
|
"name": "f",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-11-12 10:39:42 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "d",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "g",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-11-12 10:39:42 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "b",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "e",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
])";
|
|
|
|
|
2014-12-01 17:01:42 +00:00
|
|
|
checkInterface(sourceCode, interface);
|
2014-11-12 10:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multiple_params)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint a, uint b) returns(uint d) { return a + b; }\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
char const* interface = R"([
|
|
|
|
{
|
|
|
|
"name": "f",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-11-12 10:39:42 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "b",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "d",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
])";
|
|
|
|
|
2014-12-01 17:01:42 +00:00
|
|
|
checkInterface(sourceCode, interface);
|
2014-11-12 10:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multiple_methods_order)
|
|
|
|
{
|
|
|
|
// methods are expected to be in alpabetical order
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function f(uint a) returns(uint d) { return a * 7; }\n"
|
|
|
|
" function c(uint b) returns(uint e) { return b * 8; }\n"
|
|
|
|
"}\n";
|
2014-12-01 17:01:42 +00:00
|
|
|
|
2014-11-12 10:39:42 +00:00
|
|
|
char const* interface = R"([
|
|
|
|
{
|
|
|
|
"name": "c",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-11-12 10:39:42 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "b",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "e",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "f",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-11-12 10:39:42 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "d",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
])";
|
2014-12-01 17:01:42 +00:00
|
|
|
|
|
|
|
checkInterface(sourceCode, interface);
|
2014-11-12 10:39:42 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 09:48:59 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(const_function)
|
|
|
|
{
|
|
|
|
char const* sourceCode = "contract test {\n"
|
|
|
|
" function foo(uint a, uint b) returns(uint d) { return a + b; }\n"
|
2014-12-22 02:09:11 +00:00
|
|
|
" function boo(uint32 a) constant returns(uint b) { return a * 4; }\n"
|
2014-12-19 09:48:59 +00:00
|
|
|
"}\n";
|
|
|
|
|
|
|
|
char const* interface = R"([
|
|
|
|
{
|
|
|
|
"name": "boo",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": true,
|
2014-12-19 09:48:59 +00:00
|
|
|
"inputs": [{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint32"
|
|
|
|
}],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "b",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "foo",
|
2014-12-21 15:28:46 +00:00
|
|
|
"constant": false,
|
2014-12-19 09:48:59 +00:00
|
|
|
"inputs": [
|
|
|
|
{
|
|
|
|
"name": "a",
|
|
|
|
"type": "uint256"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "b",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"name": "d",
|
|
|
|
"type": "uint256"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
])";
|
|
|
|
|
|
|
|
checkInterface(sourceCode, interface);
|
|
|
|
}
|
|
|
|
|
2014-11-12 10:39:42 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|