|
| 1 | +/* |
| 2 | + This file is part of solidity. |
| 3 | +
|
| 4 | + solidity is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + solidity is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with solidity. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | +/** |
| 18 | + * Unit tests for the solidity compiler ABI JSON Interface output. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <test/libsolidity/NatspecJSONTest.h> |
| 22 | + |
| 23 | +#include <libsolutil/CommonIO.h> |
| 24 | +#include <libsolutil/StringUtils.h> |
| 25 | + |
| 26 | +#include <boost/algorithm/string/predicate.hpp> |
| 27 | + |
| 28 | +#include <fmt/format.h> |
| 29 | + |
| 30 | +#include <vector> |
| 31 | + |
| 32 | +using namespace std; |
| 33 | +using namespace solidity::frontend::test; |
| 34 | +using namespace solidity::util; |
| 35 | + |
| 36 | +ostream& solidity::frontend::test::operator<<(ostream& _output, NatspecJSONKind _kind) |
| 37 | +{ |
| 38 | + switch (_kind) { |
| 39 | + case NatspecJSONKind::Devdoc: _output << "devdoc"; break; |
| 40 | + case NatspecJSONKind::Userdoc: _output << "userdoc"; break; |
| 41 | + } |
| 42 | + return _output; |
| 43 | +} |
| 44 | + |
| 45 | +unique_ptr<TestCase> NatspecJSONTest::create(Config const& _config) |
| 46 | +{ |
| 47 | + return make_unique<NatspecJSONTest>(_config.filename, _config.evmVersion); |
| 48 | +} |
| 49 | + |
| 50 | +void NatspecJSONTest::parseCustomExpectations(istream& _stream) |
| 51 | +{ |
| 52 | + soltestAssert(m_expectedNatspecJSON.empty()); |
| 53 | + |
| 54 | + // We expect a series of expectations in the following format: |
| 55 | + // |
| 56 | + // // <qualified contract name> <devdoc|userdoc> |
| 57 | + // // <json> |
| 58 | + |
| 59 | + string line; |
| 60 | + while (getline(_stream, line)) |
| 61 | + { |
| 62 | + string_view strippedLine = expectLinePrefix(line); |
| 63 | + if (strippedLine.empty()) |
| 64 | + continue; |
| 65 | + |
| 66 | + auto [contractName, kind] = parseExpectationHeader(strippedLine); |
| 67 | + |
| 68 | + string rawJSON = extractExpectationJSON(_stream); |
| 69 | + string jsonErrors; |
| 70 | + Json::Value parsedJSON; |
| 71 | + bool jsonParsingSuccessful = jsonParseStrict(rawJSON, parsedJSON, &jsonErrors); |
| 72 | + if (!jsonParsingSuccessful) |
| 73 | + BOOST_THROW_EXCEPTION(runtime_error(fmt::format( |
| 74 | + "Malformed JSON in {} expectation for contract {}.\n" |
| 75 | + "Note that JSON expectations must be pretty-printed to be split correctly. " |
| 76 | + "The object is assumed to and at the first unindented closing brace.\n" |
| 77 | + "{}", |
| 78 | + toString(kind), |
| 79 | + contractName, |
| 80 | + rawJSON |
| 81 | + ))); |
| 82 | + |
| 83 | + m_expectedNatspecJSON[string(contractName)][kind] = parsedJSON; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +bool NatspecJSONTest::expectationsMatch() |
| 88 | +{ |
| 89 | + // NOTE: Comparing pretty printed Json::Values to avoid using its operator==, which fails to |
| 90 | + // compare equal numbers as equal. For example, for 'version' field the value is sometimes int, |
| 91 | + // sometimes uint and they compare as different even when both are 1. |
| 92 | + return |
| 93 | + SyntaxTest::expectationsMatch() && |
| 94 | + prettyPrinted(obtainedNatspec()) == prettyPrinted(m_expectedNatspecJSON); |
| 95 | +} |
| 96 | + |
| 97 | +void NatspecJSONTest::printExpectedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const |
| 98 | +{ |
| 99 | + SyntaxTest::printExpectedResult(_stream, _linePrefix, _formatted); |
| 100 | + if (!m_expectedNatspecJSON.empty()) |
| 101 | + { |
| 102 | + _stream << _linePrefix << "----" << endl; |
| 103 | + printIndented(_stream, formatNatspecExpectations(m_expectedNatspecJSON), _linePrefix); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +void NatspecJSONTest::printObtainedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const |
| 108 | +{ |
| 109 | + SyntaxTest::printObtainedResult(_stream, _linePrefix, _formatted); |
| 110 | + |
| 111 | + NatspecMap natspecJSON = obtainedNatspec(); |
| 112 | + if (!natspecJSON.empty()) |
| 113 | + { |
| 114 | + _stream << _linePrefix << "----" << endl; |
| 115 | + // TODO: Diff both versions and highlight differences. |
| 116 | + // We should have a helper for doing that in newly defined test cases without much effort. |
| 117 | + printIndented(_stream, formatNatspecExpectations(natspecJSON), _linePrefix); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +tuple<string_view, NatspecJSONKind> NatspecJSONTest::parseExpectationHeader(string_view _line) |
| 122 | +{ |
| 123 | + for (NatspecJSONKind kind: {NatspecJSONKind::Devdoc, NatspecJSONKind::Userdoc}) |
| 124 | + { |
| 125 | + string kindSuffix = " " + toString(kind); |
| 126 | + if (boost::algorithm::ends_with(_line, kindSuffix)) |
| 127 | + return {_line.substr(0, _line.size() - kindSuffix.size()), kind}; |
| 128 | + } |
| 129 | + |
| 130 | + BOOST_THROW_EXCEPTION(runtime_error( |
| 131 | + "Natspec kind (devdoc/userdoc) not present in the expectation: "s.append(_line) |
| 132 | + )); |
| 133 | +} |
| 134 | + |
| 135 | +string NatspecJSONTest::extractExpectationJSON(istream& _stream) |
| 136 | +{ |
| 137 | + string rawJSON; |
| 138 | + string line; |
| 139 | + while (getline(_stream, line)) |
| 140 | + { |
| 141 | + string_view strippedLine = expectLinePrefix(line); |
| 142 | + rawJSON += strippedLine; |
| 143 | + rawJSON += "\n"; |
| 144 | + |
| 145 | + if (boost::algorithm::starts_with(strippedLine, "}")) |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + return rawJSON; |
| 150 | +} |
| 151 | + |
| 152 | +string_view NatspecJSONTest::expectLinePrefix(string_view _line) |
| 153 | +{ |
| 154 | + size_t startPosition = 0; |
| 155 | + if (!boost::algorithm::starts_with(_line, "//")) |
| 156 | + BOOST_THROW_EXCEPTION(runtime_error( |
| 157 | + "Expectation line is not a comment: "s.append(_line) |
| 158 | + )); |
| 159 | + |
| 160 | + startPosition += 2; |
| 161 | + if (startPosition < _line.size() && _line[startPosition] == ' ') |
| 162 | + ++startPosition; |
| 163 | + |
| 164 | + return _line.substr(startPosition, _line.size() - startPosition); |
| 165 | +} |
| 166 | + |
| 167 | +string NatspecJSONTest::formatNatspecExpectations(NatspecMap const& _expectations) const |
| 168 | +{ |
| 169 | + string output; |
| 170 | + bool first = true; |
| 171 | + // NOTE: Not sorting explicitly because CompilerStack seems to put contracts roughly in the |
| 172 | + // order in which they appear in the source, which is much better than alphabetical order. |
| 173 | + for (auto const& [contractName, expectationsForAllKinds]: _expectations) |
| 174 | + for (auto const& [jsonKind, natspecJSON]: expectationsForAllKinds) |
| 175 | + { |
| 176 | + if (!first) |
| 177 | + output += "\n\n"; |
| 178 | + first = false; |
| 179 | + |
| 180 | + output += contractName + " " + toString(jsonKind) + "\n"; |
| 181 | + output += jsonPrint(natspecJSON, {JsonFormat::Pretty, 4}); |
| 182 | + } |
| 183 | + |
| 184 | + return output; |
| 185 | +} |
| 186 | + |
| 187 | +NatspecMap NatspecJSONTest::obtainedNatspec() const |
| 188 | +{ |
| 189 | + if (compiler().state() < CompilerStack::AnalysisSuccessful) |
| 190 | + return {}; |
| 191 | + |
| 192 | + NatspecMap result; |
| 193 | + for (string contractName: compiler().contractNames()) |
| 194 | + { |
| 195 | + result[contractName][NatspecJSONKind::Devdoc] = compiler().natspecDev(contractName); |
| 196 | + result[contractName][NatspecJSONKind::Userdoc] = compiler().natspecUser(contractName); |
| 197 | + } |
| 198 | + |
| 199 | + return result; |
| 200 | +} |
| 201 | + |
| 202 | +SerializedNatspecMap NatspecJSONTest::prettyPrinted(NatspecMap const& _expectations) const |
| 203 | +{ |
| 204 | + SerializedNatspecMap result; |
| 205 | + for (auto const& [contractName, expectationsForAllKinds]: _expectations) |
| 206 | + for (auto const& [jsonKind, natspecJSON]: expectationsForAllKinds) |
| 207 | + result[contractName][jsonKind] = jsonPrint(natspecJSON, {JsonFormat::Pretty, 4}); |
| 208 | + |
| 209 | + return result; |
| 210 | +} |
0 commit comments