FormatTestProto.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //===- unittest/Format/FormatTestProto.cpp --------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "FormatTestUtils.h"
  10. #include "clang/Format/Format.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "gtest/gtest.h"
  13. #define DEBUG_TYPE "format-test"
  14. namespace clang {
  15. namespace format {
  16. class FormatTestProto : public ::testing::Test {
  17. protected:
  18. static std::string format(llvm::StringRef Code, unsigned Offset,
  19. unsigned Length, const FormatStyle &Style) {
  20. DEBUG(llvm::errs() << "---\n");
  21. DEBUG(llvm::errs() << Code << "\n\n");
  22. std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
  23. tooling::Replacements Replaces = reformat(Style, Code, Ranges);
  24. std::string Result = applyAllReplacements(Code, Replaces);
  25. EXPECT_NE("", Result);
  26. DEBUG(llvm::errs() << "\n" << Result << "\n\n");
  27. return Result;
  28. }
  29. static std::string format(llvm::StringRef Code) {
  30. FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
  31. Style.ColumnLimit = 60; // To make writing tests easier.
  32. return format(Code, 0, Code.size(), Style);
  33. }
  34. static void verifyFormat(llvm::StringRef Code) {
  35. EXPECT_EQ(Code.str(), format(test::messUp(Code)));
  36. }
  37. };
  38. TEST_F(FormatTestProto, FormatsMessages) {
  39. verifyFormat("message SomeMessage {\n"
  40. " required int32 field1 = 1;\n"
  41. "}");
  42. verifyFormat("message SomeMessage {\n"
  43. " required .absolute.Reference field1 = 1;\n"
  44. "}");
  45. verifyFormat("message SomeMessage {\n"
  46. " required int32 field1 = 1;\n"
  47. " optional string field2 = 2 [default = \"2\"]\n"
  48. "}");
  49. verifyFormat("message SomeMessage {\n"
  50. " optional really.really.long.qualified.type.aaa.aaaaaaa\n"
  51. " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
  52. " optional\n"
  53. " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
  54. " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
  55. "}");
  56. }
  57. TEST_F(FormatTestProto, KeywordsInOtherLanguages) {
  58. verifyFormat("optional string operator = 1;");
  59. }
  60. TEST_F(FormatTestProto, FormatsEnums) {
  61. verifyFormat("enum Type {\n"
  62. " UNKNOWN = 0;\n"
  63. " TYPE_A = 1;\n"
  64. " TYPE_B = 2;\n"
  65. "};");
  66. }
  67. TEST_F(FormatTestProto, UnderstandsReturns) {
  68. verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
  69. }
  70. TEST_F(FormatTestProto, MessageFieldAttributes) {
  71. verifyFormat("optional string test = 1 [default = \"test\"];");
  72. verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
  73. verifyFormat("optional LongMessageType long_proto_field = 1\n"
  74. " [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
  75. " deprecated = true];");
  76. verifyFormat("optional LongMessageType long_proto_field = 1\n"
  77. " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
  78. verifyFormat("repeated double value = 1\n"
  79. " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
  80. verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
  81. " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
  82. " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
  83. "}];");
  84. verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
  85. " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
  86. " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
  87. "}];");
  88. verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
  89. " type: \"AAAAAAAAAA\"\n"
  90. " is: \"AAAAAAAAAA\"\n"
  91. " or: \"BBBBBBBBBB\"\n"
  92. "}];");
  93. verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
  94. " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
  95. " bbbbbbb: BBBB,\n"
  96. " bbbb: BBB\n"
  97. "}];");
  98. }
  99. TEST_F(FormatTestProto, DoesntWrapFileOptions) {
  100. EXPECT_EQ(
  101. "option java_package = "
  102. "\"some.really.long.package.that.exceeds.the.column.limit\";",
  103. format("option java_package = "
  104. "\"some.really.long.package.that.exceeds.the.column.limit\";"));
  105. }
  106. TEST_F(FormatTestProto, FormatsOptions) {
  107. verifyFormat("option (MyProto.options) = {\n"
  108. " field_a: OK\n"
  109. " field_b: \"OK\"\n"
  110. " field_c: \"OK\"\n"
  111. " msg_field: {field_d: 123}\n"
  112. "};");
  113. verifyFormat("option (MyProto.options) = {\n"
  114. " field_a: OK\n"
  115. " field_b: \"OK\"\n"
  116. " field_c: \"OK\"\n"
  117. " msg_field: {\n"
  118. " field_d: 123\n"
  119. " field_e: OK\n"
  120. " }\n"
  121. "};");
  122. verifyFormat("option (MyProto.options) = {\n"
  123. " field_a: OK // Comment\n"
  124. " field_b: \"OK\"\n"
  125. " field_c: \"OK\"\n"
  126. " msg_field: {field_d: 123}\n"
  127. "};");
  128. verifyFormat("option (MyProto.options) = {\n"
  129. " field_c: \"OK\"\n"
  130. " msg_field{field_d: 123}\n"
  131. "};");
  132. }
  133. TEST_F(FormatTestProto, FormatsService) {
  134. verifyFormat("service SearchService {\n"
  135. " rpc Search(SearchRequest) returns (SearchResponse) {\n"
  136. " option foo = true;\n"
  137. " }\n"
  138. "};");
  139. }
  140. } // end namespace tooling
  141. } // end namespace clang