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 #define DEBUG_TYPE "format-test"
11 
12 #include "FormatTestUtils.h"
13 #include "clang/Format/Format.h"
14 #include "llvm/Support/Debug.h"
15 #include "gtest/gtest.h"
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 int32 field1 = 1;\n"
51                "  optional string field2 = 2 [default = \"2\"]\n"
52                "}");
53 
54   verifyFormat("message SomeMessage {\n"
55                "  optional really.really.long.and.qualified.type.aaaaaaa\n"
56                "      fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
57                "  optional\n"
58                "      really.really.long.and.qualified.type.aaaaaaa.aaaaaaaa\n"
59                "          another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
60                "}");
61 }
62 
63 TEST_F(FormatTestProto, FormatsEnums) {
64   verifyFormat("enum Type {\n"
65                "  UNKNOWN = 0;\n"
66                "  TYPE_A = 1;\n"
67                "  TYPE_B = 2;\n"
68                "};");
69 }
70 
71 TEST_F(FormatTestProto, UnderstandsReturns) {
72   verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
73 }
74 
75 TEST_F(FormatTestProto, MessageFieldAttributes) {
76   verifyFormat("optional string test = 1 [default = \"test\"];");
77   verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
78   verifyFormat("optional LongMessageType long_proto_field = 1\n"
79                "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
80                "     deprecated = true];");
81   verifyFormat("optional LongMessageType long_proto_field = 1\n"
82                "    [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
83   verifyFormat("repeated double value = 1\n"
84                "    [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
85   verifyFormat("repeated double value = 1\n"
86                "    [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
87                "                            bbbbbbbbbbbbbbbb: BBBBBBBBBB}];");
88   verifyFormat("repeated double value = 1\n"
89                "    [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
90                "                            bbbbbbbbbbbbbbbb: BBBBBBBBBB}];");
91 }
92 
93 TEST_F(FormatTestProto, FormatsOptions) {
94   verifyFormat("option java_package = \"my.test.package\";");
95   verifyFormat("option (my_custom_option) = \"abc\";");
96 }
97 
98 TEST_F(FormatTestProto, FormatsService) {
99   verifyFormat("service SearchService {\n"
100                "  rpc Search(SearchRequest) returns (SearchResponse) {\n"
101                "    option foo = true;\n"
102                "  }\n"
103                "};");
104 }
105 
106 } // end namespace tooling
107 } // end namespace clang
108