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 // SPDX-License-Identifier: GPL-3.0 18 19 #pragma once 20 21 #include <libsolutil/AnsiColorized.h> 22 #include <libsolidity/interface/CompilerStack.h> 23 #include <test/TestCase.h> 24 25 #include <iosfwd> 26 #include <string> 27 #include <vector> 28 #include <utility> 29 30 namespace solidity::frontend 31 { 32 class CompilerStack; 33 } 34 35 namespace solidity::frontend::test 36 { 37 38 class ASTJSONTest: public TestCase 39 { 40 public: 41 struct TestVariant 42 { TestVariantTestVariant43 TestVariant(std::string_view _baseName, CompilerStack::State _stopAfter): 44 baseName(_baseName), 45 stopAfter(_stopAfter) 46 {} 47 nameTestVariant48 std::string name() const 49 { 50 return stopAfter == CompilerStack::State::Parsed ? "parseOnly" : ""; 51 } 52 astFilenameTestVariant53 std::string astFilename() const 54 { 55 return std::string(baseName) + 56 (name().empty() ? "" : "_") + 57 name() + 58 ".json"; 59 } 60 61 std::string baseName; 62 CompilerStack::State stopAfter; 63 std::string result; 64 std::string expectation; 65 }; 66 create(Config const & _config)67 static std::unique_ptr<TestCase> create(Config const& _config) 68 { 69 return std::make_unique<ASTJSONTest>(_config.filename); 70 } 71 ASTJSONTest(std::string const& _filename); 72 73 TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override; 74 75 void printSource(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) const override; 76 void printUpdatedExpectations(std::ostream& _stream, std::string const& _linePrefix) const override; 77 private: 78 bool runTest( 79 TestVariant& _testVariant, 80 std::map<std::string, unsigned> const& _sourceIndices, 81 CompilerStack& _compiler, 82 std::ostream& _stream, 83 std::string const& _linePrefix = "", 84 bool const _formatted = false 85 ); 86 void updateExpectation( 87 std::string const& _filename, 88 std::string const& _expectation, 89 std::string const& _variant 90 ) const; 91 92 std::vector<TestVariant> m_variants; 93 94 std::vector<std::pair<std::string, std::string>> m_sources; 95 }; 96 97 } 98