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 }
77 
78 TEST_F(FormatTestProto, UnderstandsReturns) {
79   verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
80 }
81 
82 TEST_F(FormatTestProto, MessageFieldAttributes) {
83   verifyFormat("optional string test = 1 [default = \"test\"];");
84   verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
85   verifyFormat("optional LongMessageType long_proto_field = 1\n"
86                "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
87                "     deprecated = true];");
88   verifyFormat("optional LongMessageType long_proto_field = 1\n"
89                "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
90   verifyFormat("repeated double value = 1\n"
91                "    [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
92   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
93                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
94                "  bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
95                "}];");
96   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
97                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
98                "  bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
99                "}];");
100   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
101                "  type: \"AAAAAAAAAA\"\n"
102                "  is: \"AAAAAAAAAA\"\n"
103                "  or: \"BBBBBBBBBB\"\n"
104                "}];");
105   verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
106                "  aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
107                "  bbbbbbb: BBBB,\n"
108                "  bbbb: BBB\n"
109                "}];");
110 }
111 
112 TEST_F(FormatTestProto, DoesntWrapFileOptions) {
113   EXPECT_EQ(
114       "option java_package = "
115       "\"some.really.long.package.that.exceeds.the.column.limit\";",
116       format("option    java_package   =    "
117              "\"some.really.long.package.that.exceeds.the.column.limit\";"));
118 }
119 
120 TEST_F(FormatTestProto, FormatsOptions) {
121   verifyFormat("option (MyProto.options) = {\n"
122                "  field_a: OK\n"
123                "  field_b: \"OK\"\n"
124                "  field_c: \"OK\"\n"
125                "  msg_field: {field_d: 123}\n"
126                "};");
127 
128   verifyFormat("option (MyProto.options) = {\n"
129                "  field_a: OK\n"
130                "  field_b: \"OK\"\n"
131                "  field_c: \"OK\"\n"
132                "  msg_field: {\n"
133                "    field_d: 123\n"
134                "    field_e: OK\n"
135                "  }\n"
136                "};");
137 
138   verifyFormat("option (MyProto.options) = {\n"
139                "  field_a: OK  // Comment\n"
140                "  field_b: \"OK\"\n"
141                "  field_c: \"OK\"\n"
142                "  msg_field: {field_d: 123}\n"
143                "};");
144 
145   verifyFormat("option (MyProto.options) = {\n"
146                "  field_c: \"OK\"\n"
147                "  msg_field{field_d: 123}\n"
148                "};");
149 }
150 
151 TEST_F(FormatTestProto, FormatsService) {
152   verifyFormat("service SearchService {\n"
153                "  rpc Search(SearchRequest) returns (SearchResponse) {\n"
154                "    option foo = true;\n"
155                "  }\n"
156                "};");
157 }
158 
159 } // end namespace tooling
160 } // end namespace clang
161