| 1 | //===- unittest/Format/FormatTestTableGen.cpp -----------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "FormatTestUtils.h" |
| 10 | #include "clang/Format/Format.h" |
| 11 | #include "llvm/Support/Debug.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | #define DEBUG_TYPE "format-test" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace format { |
| 18 | |
| 19 | class FormatTestTableGen : public ::testing::Test { |
| 20 | protected: |
| 21 | static std::string format(llvm::StringRef Code, unsigned Offset, |
| 22 | unsigned Length, const FormatStyle &Style) { |
| 23 | LLVM_DEBUG(llvm::errs() << "---\n"); |
| 24 | LLVM_DEBUG(llvm::errs() << Code << "\n\n"); |
| 25 | std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); |
| 26 | tooling::Replacements Replaces = reformat(Style, Code, Ranges); |
| 27 | auto Result = applyAllReplacements(Code, Replaces); |
| 28 | EXPECT_TRUE(static_cast<bool>(Result)); |
| 29 | LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); |
| 30 | return *Result; |
| 31 | } |
| 32 | |
| 33 | static std::string format(llvm::StringRef Code) { |
| 34 | FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen); |
| 35 | Style.ColumnLimit = 60; // To make writing tests easier. |
| 36 | return format(Code, 0, Code.size(), Style); |
| 37 | } |
| 38 | |
| 39 | static void verifyFormat(llvm::StringRef Code) { |
| 40 | EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable"; |
| 41 | EXPECT_EQ(Code.str(), format(test::messUp(Code))); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | TEST_F(FormatTestTableGen, FormatStringBreak) { |
| 46 | verifyFormat("include \"OptParser.td\"\n" |
| 47 | "def flag : Flag<\"--foo\">,\n" |
| 48 | " HelpText<\n" |
| 49 | " \"This is a very, very, very, very, \"\n" |
| 50 | " \"very, very, very, very, very, very, \"\n" |
| 51 | " \"very long help string\">;\n"); |
| 52 | } |
| 53 | |
| 54 | TEST_F(FormatTestTableGen, NoSpacesInSquareBracketLists) { |
| 55 | verifyFormat("def flag : Flag<[\"-\", \"--\"], \"foo\">;\n"); |
| 56 | } |
| 57 | |
| 58 | } // namespace format |
| 59 | } // end namespace clang |
| 60 | |