Browse Source

Using jsoncpp for exporting ABI interface from solidity

- Also changing the interface JSON test to have a shorter name
  plus to provide meaningful error message in case of failure
cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
eed32824c3
  1. 50
      libsolidity/CompilerStack.cpp
  2. 18
      test/solidityJSONInterfaceTest.cpp

50
libsolidity/CompilerStack.cpp

@ -85,54 +85,52 @@ void CompilerStack::streamAssembly(ostream& _outStream)
string const& CompilerStack::getInterface()
{
Json::StyledWriter writer;
if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
if (m_interface.empty())
{
stringstream interface;
interface << '[';
Json::Value methods(Json::arrayValue);
vector<FunctionDefinition const*> exportedFunctions = m_contractASTNode->getInterfaceFunctions();
unsigned functionsCount = exportedFunctions.size();
for (FunctionDefinition const* f: exportedFunctions)
{
auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars)
Json::Value method;
Json::Value inputs(Json::arrayValue);
Json::Value outputs(Json::arrayValue);
auto streamVariables = [&](vector<ASTPointer<VariableDeclaration>> const& _vars,
Json::Value &json)
{
unsigned varCount = _vars.size();
for (ASTPointer<VariableDeclaration> const& var: _vars)
{
interface << "{"
<< "\"name\":" << escaped(var->getName(), false) << ","
<< "\"type\":" << escaped(var->getType()->toString(), false)
<< "}";
if (--varCount > 0)
interface << ",";
Json::Value input;
input["name"] = var->getName();
input["type"] = var->getType()->toString();
json.append(input);
}
};
interface << '{'
<< "\"name\":" << escaped(f->getName(), false) << ","
<< "\"inputs\":[";
streamVariables(f->getParameters());
interface << "],"
<< "\"outputs\":[";
streamVariables(f->getReturnParameters());
interface << "]"
<< "}";
if (--functionsCount > 0)
interface << ",";
}
interface << ']';
m_interface = interface.str();
method["name"] = f->getName();
streamVariables(f->getParameters(), inputs);
method["inputs"] = inputs;
streamVariables(f->getReturnParameters(), outputs);
method["outputs"] = outputs;
methods.append(method);
}
m_interface = writer.write(methods);
}
return m_interface;
}
string const& CompilerStack::getDocumentation()
{
Json::StyledWriter writer;
if (!m_parseSuccessful)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Parsing was not successful."));
if (m_documentation.empty())
{
Json::Value doc;

18
test/solidityJSONInterfaceTest.cpp

@ -34,7 +34,7 @@ namespace test
class InterfaceChecker
{
public:
bool checkInterface(std::string const& _code, std::string const& _expectedInterfaceString)
void checkInterface(std::string const& _code, std::string const& _expectedInterfaceString)
{
m_compilerStack.parse(_code);
std::string generatedInterfaceString = m_compilerStack.getInterface();
@ -42,7 +42,9 @@ public:
m_reader.parse(generatedInterfaceString, generatedInterface);
Json::Value expectedInterface;
m_reader.parse(_expectedInterfaceString, expectedInterface);
return expectedInterface == generatedInterface;
BOOST_CHECK_MESSAGE(expectedInterface == generatedInterface,
"Expected " << _expectedInterfaceString <<
"\n but got:\n" << generatedInterfaceString);
}
private:
@ -50,7 +52,7 @@ private:
Json::Reader m_reader;
};
BOOST_FIXTURE_TEST_SUITE(SolidityCompilerJSONInterfaceOutput, InterfaceChecker)
BOOST_FIXTURE_TEST_SUITE(solidityABIJSON, InterfaceChecker)
BOOST_AUTO_TEST_CASE(basic_test)
{
@ -76,7 +78,7 @@ BOOST_AUTO_TEST_CASE(basic_test)
}
])";
BOOST_CHECK(checkInterface(sourceCode, interface));
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_CASE(empty_contract)
@ -86,7 +88,7 @@ BOOST_AUTO_TEST_CASE(empty_contract)
char const* interface = "[]";
BOOST_CHECK(checkInterface(sourceCode, interface));
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_CASE(multiple_methods)
@ -129,7 +131,7 @@ BOOST_AUTO_TEST_CASE(multiple_methods)
}
])";
BOOST_CHECK(checkInterface(sourceCode, interface));
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_CASE(multiple_params)
@ -160,7 +162,7 @@ BOOST_AUTO_TEST_CASE(multiple_params)
}
])";
BOOST_CHECK(checkInterface(sourceCode, interface));
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_CASE(multiple_methods_order)
@ -204,7 +206,7 @@ BOOST_AUTO_TEST_CASE(multiple_methods_order)
}
])";
BOOST_CHECK(checkInterface(sourceCode, interface));
checkInterface(sourceCode, interface);
}
BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save