blob: 3ff38eab024ea1c759f7fa4d7bdb32b310be55e6 [file] [log] [blame]
Daniel Jasper7052ce62014-01-19 09:04:08 +00001//===- 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
Daniel Jasper7052ce62014-01-19 09:04:08 +000010#include "FormatTestUtils.h"
11#include "clang/Format/Format.h"
12#include "llvm/Support/Debug.h"
13#include "gtest/gtest.h"
14
Chandler Carruth10346662014-04-22 03:17:02 +000015#define DEBUG_TYPE "format-test"
16
Daniel Jasper7052ce62014-01-19 09:04:08 +000017namespace clang {
18namespace format {
19
20class FormatTestProto : public ::testing::Test {
21protected:
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) {
Nico Weber514ecc82014-02-02 20:50:45 +000035 FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
Daniel Jasper215d6c82014-01-22 08:04:52 +000036 Style.ColumnLimit = 60; // To make writing tests easier.
37 return format(Code, 0, Code.size(), Style);
Daniel Jasper7052ce62014-01-19 09:04:08 +000038 }
39
40 static void verifyFormat(llvm::StringRef Code) {
41 EXPECT_EQ(Code.str(), format(test::messUp(Code)));
42 }
43};
44
45TEST_F(FormatTestProto, FormatsMessages) {
46 verifyFormat("message SomeMessage {\n"
47 " required int32 field1 = 1;\n"
48 "}");
49 verifyFormat("message SomeMessage {\n"
Daniel Jasper9c2820c2014-06-23 07:36:25 +000050 " required .absolute.Reference field1 = 1;\n"
51 "}");
52 verifyFormat("message SomeMessage {\n"
Daniel Jasper7052ce62014-01-19 09:04:08 +000053 " required int32 field1 = 1;\n"
54 " optional string field2 = 2 [default = \"2\"]\n"
55 "}");
Daniel Jasper215d6c82014-01-22 08:04:52 +000056
57 verifyFormat("message SomeMessage {\n"
Nikola Smiljanice08a91e2014-05-08 00:05:13 +000058 " optional really.really.long.qualified.type.aaa.aaaaaaa\n"
Daniel Jasper215d6c82014-01-22 08:04:52 +000059 " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
60 " optional\n"
Nikola Smiljanice08a91e2014-05-08 00:05:13 +000061 " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
Daniel Jasper215d6c82014-01-22 08:04:52 +000062 " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
63 "}");
Daniel Jasper7052ce62014-01-19 09:04:08 +000064}
65
66TEST_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
74TEST_F(FormatTestProto, UnderstandsReturns) {
75 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
76}
77
78TEST_F(FormatTestProto, MessageFieldAttributes) {
79 verifyFormat("optional string test = 1 [default = \"test\"];");
Daniel Jaspera0e9be22014-01-28 18:51:11 +000080 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];");
Daniel Jasper7052ce62014-01-19 09:04:08 +000084 verifyFormat("optional LongMessageType long_proto_field = 1\n"
85 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
Daniel Jasper6e58fee2014-01-29 18:43:40 +000086 verifyFormat("repeated double value = 1\n"
Daniel Jasper783bac62014-04-15 09:54:30 +000087 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];");
Daniel Jasperf24301d2014-01-29 18:52:43 +000088 verifyFormat("repeated double value = 1\n"
Daniel Jasper783bac62014-04-15 09:54:30 +000089 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
90 " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];");
Daniel Jasper9d3adc02014-04-15 09:54:24 +000091 verifyFormat("repeated double value = 1\n"
Daniel Jasper783bac62014-04-15 09:54:30 +000092 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
93 " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];");
Daniel Jasper883ae9d2014-04-29 15:54:14 +000094 verifyFormat("repeated double value = 1\n"
95 " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
96 " bbbbbbb: BBBB,\n"
97 " bbbb: BBB}];");
Daniel Jasper7052ce62014-01-19 09:04:08 +000098}
99
Daniel Jasper929b1db2014-01-20 16:47:22 +0000100TEST_F(FormatTestProto, FormatsOptions) {
Daniel Jaspera382cbe2014-07-28 14:08:09 +0000101 verifyFormat("option (MyProto.options) = {\n"
102 " field_a: OK\n"
103 " field_b: \"OK\"\n"
104 " field_c: \"OK\"\n"
105 " msg_field: {field_d: 123}\n"
106 "};");
107
108 verifyFormat("option (MyProto.options) = {\n"
109 " field_a: OK\n"
110 " field_b: \"OK\"\n"
111 " field_c: \"OK\"\n"
112 " msg_field: {field_d: 123\n"
113 " field_e: OK}\n"
114 "};");
115
116 verifyFormat("option (MyProto.options) = {\n"
117 " field_a: OK // Comment\n"
118 " field_b: \"OK\"\n"
119 " field_c: \"OK\"\n"
120 " msg_field: {field_d: 123}\n"
121 "};");
Daniel Jasper929b1db2014-01-20 16:47:22 +0000122}
123
Daniel Jasper220c0d12014-04-10 07:27:12 +0000124TEST_F(FormatTestProto, FormatsService) {
125 verifyFormat("service SearchService {\n"
126 " rpc Search(SearchRequest) returns (SearchResponse) {\n"
127 " option foo = true;\n"
128 " }\n"
129 "};");
130}
131
Daniel Jasper7052ce62014-01-19 09:04:08 +0000132} // end namespace tooling
133} // end namespace clang