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:
format(llvm::StringRef Code,unsigned Offset,unsigned Length,const FormatStyle & Style)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
format(llvm::StringRef Code)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
verifyFormat(llvm::StringRef Code)40 static void verifyFormat(llvm::StringRef Code) {
41 EXPECT_EQ(Code.str(), format(test::messUp(Code)));
42 }
43 };
44
TEST_F(FormatTestProto,FormatsMessages)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
TEST_F(FormatTestProto,FormatsEnums)66 TEST_F(FormatTestProto, FormatsEnums) {
67 verifyFormat("enum Type {\n"
68 " UNKNOWN = 0;\n"
69 " TYPE_A = 1;\n"
70 " TYPE_B = 2;\n"
71 "};");
72 }
73
TEST_F(FormatTestProto,UnderstandsReturns)74 TEST_F(FormatTestProto, UnderstandsReturns) {
75 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
76 }
77
TEST_F(FormatTestProto,MessageFieldAttributes)78 TEST_F(FormatTestProto, MessageFieldAttributes) {
79 verifyFormat("optional string test = 1 [default = \"test\"];");
80 verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
81 verifyFormat("optional LongMessageType long_proto_field = 1\n"
82 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
83 " deprecated = true];");
84 verifyFormat("optional LongMessageType long_proto_field = 1\n"
85 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
86 verifyFormat("repeated double value = 1\n"
87 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
88 verifyFormat("repeated double value = 1\n"
89 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
90 " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];");
91 verifyFormat("repeated double value = 1\n"
92 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
93 " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];");
94 verifyFormat("repeated double value = 1\n"
95 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
96 " bbbbbbb: BBBB,\n"
97 " bbbb: BBB}];");
98 }
99
TEST_F(FormatTestProto,FormatsOptions)100 TEST_F(FormatTestProto, FormatsOptions) {
101 verifyFormat("option java_package = \"my.test.package\";");
102 verifyFormat("option (my_custom_option) = \"abc\";");
103 }
104
TEST_F(FormatTestProto,FormatsService)105 TEST_F(FormatTestProto, FormatsService) {
106 verifyFormat("service SearchService {\n"
107 " rpc Search(SearchRequest) returns (SearchResponse) {\n"
108 " option foo = true;\n"
109 " }\n"
110 "};");
111 }
112
113 } // end namespace tooling
114 } // end namespace clang
115