FormatTestTableGen.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "FormatTestUtils.h"
  9. #include "clang/Format/Format.h"
  10. #include "llvm/Support/Debug.h"
  11. #include "gtest/gtest.h"
  12. #define DEBUG_TYPE "format-test"
  13. namespace clang {
  14. namespace format {
  15. class FormatTestTableGen : public ::testing::Test {
  16. protected:
  17. static std::string format(llvm::StringRef Code, unsigned Offset,
  18. unsigned Length, const FormatStyle &Style) {
  19. LLVM_DEBUG(llvm::errs() << "---\n");
  20. LLVM_DEBUG(llvm::errs() << Code << "\n\n");
  21. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  22. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  23. auto Result = applyAllReplacements(Code, Replaces);
  24. EXPECT_TRUE(static_cast<bool>(Result));
  25. LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  26. return *Result;
  27. }
  28. static std::string format(llvm::StringRef Code) {
  29. FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
  30. Style.ColumnLimit = 60; // To make writing tests easier.
  31. return format(Code, 0, Code.size(), Style);
  32. }
  33. static void verifyFormat(llvm::StringRef Code) {
  34. EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable";
  35. EXPECT_EQ(Code.str(), format(test::messUp(Code)));
  36. }
  37. };
  38. TEST_F(FormatTestTableGen, FormatStringBreak) {
  39. verifyFormat("include \"OptParser.td\"\n"
  40. "def flag : Flag<\"--foo\">,\n"
  41. " HelpText<\n"
  42. " \"This is a very, very, very, very, \"\n"
  43. " \"very, very, very, very, very, very, \"\n"
  44. " \"very long help string\">;\n");
  45. }
  46. TEST_F(FormatTestTableGen, NoSpacesInSquareBracketLists) {
  47. verifyFormat("def flag : Flag<[\"-\", \"--\"], \"foo\">;\n");
  48. }
  49. } // namespace format
  50. } // end namespace clang