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