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 10 #include "FormatTestUtils.h" 11 #include "clang/Format/Format.h" 12 #include "llvm/Support/Debug.h" 13 #include "gtest/gtest.h" 14 15 #define DEBUG_TYPE "format-test" 16 17 namespace clang { 18 namespace format { 19 20 class FormatTestProto : public ::testing::Test { 21 protected: 22 static std::string format(llvm::StringRef Code, unsigned Offset, 23 unsigned Length, const FormatStyle &Style) { 24 DEBUG(llvm::errs() << "---\n"); 25 DEBUG(llvm::errs() << Code << "\n\n"); 26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length)); 27 tooling::Replacements Replaces = reformat(Style, Code, Ranges); 28 std::string Result = applyAllReplacements(Code, Replaces); 29 EXPECT_NE("", Result); 30 DEBUG(llvm::errs() << "\n" << Result << "\n\n"); 31 return Result; 32 } 33 34 static std::string format(llvm::StringRef Code) { 35 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto); 36 Style.ColumnLimit = 60; // To make writing tests easier. 37 return format(Code, 0, Code.size(), Style); 38 } 39 40 static void verifyFormat(llvm::StringRef Code) { 41 EXPECT_EQ(Code.str(), format(test::messUp(Code))); 42 } 43 }; 44 45 TEST_F(FormatTestProto, FormatsMessages) { 46 verifyFormat("message SomeMessage {\n" 47 " required int32 field1 = 1;\n" 48 "}"); 49 verifyFormat("message SomeMessage {\n" 50 " required .absolute.Reference field1 = 1;\n" 51 "}"); 52 verifyFormat("message SomeMessage {\n" 53 " required int32 field1 = 1;\n" 54 " optional string field2 = 2 [default = \"2\"]\n" 55 "}"); 56 57 verifyFormat("message SomeMessage {\n" 58 " optional really.really.long.qualified.type.aaa.aaaaaaa\n" 59 " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n" 60 " optional\n" 61 " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n" 62 " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n" 63 "}"); 64 } 65 66 TEST_F(FormatTestProto, KeywordsInOtherLanguages) { 67 verifyFormat("optional string operator = 1;"); 68 } 69 70 TEST_F(FormatTestProto, FormatsEnums) { 71 verifyFormat("enum Type {\n" 72 " UNKNOWN = 0;\n" 73 " TYPE_A = 1;\n" 74 " TYPE_B = 2;\n" 75 "};"); 76 verifyFormat("enum Type {\n" 77 " UNKNOWN = 0 [(some_options) = {\n" 78 " a: aa,\n" 79 " b: bb\n" 80 " }];\n" 81 "};"); 82 } 83 84 TEST_F(FormatTestProto, UnderstandsReturns) { 85 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);"); 86 } 87 88 TEST_F(FormatTestProto, MessageFieldAttributes) { 89 verifyFormat("optional string test = 1 [default = \"test\"];"); 90 verifyFormat("optional bool a = 1 [default = true, deprecated = true];"); 91 verifyFormat("optional LongMessageType long_proto_field = 1 [\n" 92 " default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n" 93 " deprecated = true\n" 94 "];"); 95 verifyFormat("optional LongMessageType long_proto_field = 1\n" 96 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];"); 97 verifyFormat("repeated double value = 1\n" 98 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];"); 99 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n" 100 " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n" 101 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n" 102 "}];"); 103 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n" 104 " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n" 105 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n" 106 "}];"); 107 verifyFormat("repeated double value = 1 [\n" 108 " (aaaaaaa.aaaaaaaaa) = {\n" 109 " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n" 110 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n" 111 " },\n" 112 " (bbbbbbb.bbbbbbbbb) = {\n" 113 " aaaaaaaaaaaaaaaa: AAAAAAAAAA\n" 114 " bbbbbbbbbbbbbbbb: BBBBBBBBBB\n" 115 " }\n" 116 "];"); 117 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n" 118 " type: \"AAAAAAAAAA\"\n" 119 " is: \"AAAAAAAAAA\"\n" 120 " or: \"BBBBBBBBBB\"\n" 121 "}];"); 122 verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n" 123 " aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n" 124 " bbbbbbb: BBBB,\n" 125 " bbbb: BBB\n" 126 "}];"); 127 verifyFormat("optional AAA aaa = 1 [\n" 128 " foo = {\n" 129 " key: 'a' //\n" 130 " },\n" 131 " bar = {\n" 132 " key: 'a' //\n" 133 " }\n" 134 "];"); 135 } 136 137 TEST_F(FormatTestProto, DoesntWrapFileOptions) { 138 EXPECT_EQ( 139 "option java_package = " 140 "\"some.really.long.package.that.exceeds.the.column.limit\";", 141 format("option java_package = " 142 "\"some.really.long.package.that.exceeds.the.column.limit\";")); 143 } 144 145 TEST_F(FormatTestProto, FormatsOptions) { 146 verifyFormat("option (MyProto.options) = {\n" 147 " field_a: OK\n" 148 " field_b: \"OK\"\n" 149 " field_c: \"OK\"\n" 150 " msg_field: {field_d: 123}\n" 151 "};"); 152 verifyFormat("option (MyProto.options) = {\n" 153 " field_a: OK\n" 154 " field_b: \"OK\"\n" 155 " field_c: \"OK\"\n" 156 " msg_field: {\n" 157 " field_d: 123\n" 158 " field_e: OK\n" 159 " }\n" 160 "};"); 161 verifyFormat("option (MyProto.options) = {\n" 162 " field_a: OK // Comment\n" 163 " field_b: \"OK\"\n" 164 " field_c: \"OK\"\n" 165 " msg_field: {field_d: 123}\n" 166 "};"); 167 verifyFormat("option (MyProto.options) = {\n" 168 " field_c: \"OK\"\n" 169 " msg_field{field_d: 123}\n" 170 "};"); 171 172 // Support syntax with <> instead of {}. 173 verifyFormat("option (MyProto.options) = {\n" 174 " field_c: \"OK\",\n" 175 " msg_field: <field_d: 123>\n" 176 "};"); 177 } 178 179 TEST_F(FormatTestProto, FormatsService) { 180 verifyFormat("service SearchService {\n" 181 " rpc Search(SearchRequest) returns (SearchResponse) {\n" 182 " option foo = true;\n" 183 " }\n" 184 "};"); 185 } 186 187 TEST_F(FormatTestProto, ExtendingMessage) { 188 verifyFormat("extend .foo.Bar {\n" 189 "}"); 190 } 191 192 TEST_F(FormatTestProto, FormatsImports) { 193 verifyFormat("import \"a.proto\";\n" 194 "import \"b.proto\";\n" 195 "// comment\n" 196 "message A {\n" 197 "}"); 198 199 // Missing semicolons should not confuse clang-format. 200 verifyFormat("import \"a.proto\"\n" 201 "import \"b.proto\"\n" 202 "// comment\n" 203 "message A {\n" 204 "}"); 205 } 206 207 } // end namespace tooling 208 } // end namespace clang 209