2014-01-19 17:04:08 +08:00
|
|
|
//===- unittest/Format/FormatTestProto.cpp --------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-01-19 17:04:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "FormatTestUtils.h"
|
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2014-04-22 11:17:02 +08:00
|
|
|
#define DEBUG_TYPE "format-test"
|
|
|
|
|
2014-01-19 17:04:08 +08:00
|
|
|
namespace clang {
|
|
|
|
namespace format {
|
|
|
|
|
|
|
|
class FormatTestProto : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
static std::string format(llvm::StringRef Code, unsigned Offset,
|
2018-04-23 16:50:36 +08:00
|
|
|
unsigned Length, const FormatStyle &Style) {
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::errs() << "---\n");
|
|
|
|
LLVM_DEBUG(llvm::errs() << Code << "\n\n");
|
2014-01-19 17:04:08 +08:00
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
|
2018-04-23 16:50:36 +08:00
|
|
|
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
|
2016-07-11 21:53:12 +08:00
|
|
|
auto Result = applyAllReplacements(Code, Replaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Result));
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
|
2016-07-11 21:53:12 +08:00
|
|
|
return *Result;
|
2014-01-19 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::string format(llvm::StringRef Code) {
|
2014-02-03 04:50:45 +08:00
|
|
|
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
|
2014-01-22 16:04:52 +08:00
|
|
|
Style.ColumnLimit = 60; // To make writing tests easier.
|
|
|
|
return format(Code, 0, Code.size(), Style);
|
2014-01-19 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void verifyFormat(llvm::StringRef Code) {
|
2018-04-05 05:09:00 +08:00
|
|
|
EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable";
|
2014-01-19 17:04:08 +08:00
|
|
|
EXPECT_EQ(Code.str(), format(test::messUp(Code)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(FormatTestProto, FormatsMessages) {
|
|
|
|
verifyFormat("message SomeMessage {\n"
|
|
|
|
" required int32 field1 = 1;\n"
|
|
|
|
"}");
|
2014-06-23 15:36:25 +08:00
|
|
|
verifyFormat("message SomeMessage {\n"
|
|
|
|
" required .absolute.Reference field1 = 1;\n"
|
|
|
|
"}");
|
2014-01-19 17:04:08 +08:00
|
|
|
verifyFormat("message SomeMessage {\n"
|
|
|
|
" required int32 field1 = 1;\n"
|
|
|
|
" optional string field2 = 2 [default = \"2\"]\n"
|
|
|
|
"}");
|
2014-01-22 16:04:52 +08:00
|
|
|
|
|
|
|
verifyFormat("message SomeMessage {\n"
|
2014-05-08 08:05:13 +08:00
|
|
|
" optional really.really.long.qualified.type.aaa.aaaaaaa\n"
|
2014-01-22 16:04:52 +08:00
|
|
|
" fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n"
|
|
|
|
" optional\n"
|
2014-05-08 08:05:13 +08:00
|
|
|
" really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n"
|
2014-01-22 16:04:52 +08:00
|
|
|
" another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n"
|
|
|
|
"}");
|
2017-06-27 18:06:40 +08:00
|
|
|
verifyFormat("message SomeMessage {\n"
|
|
|
|
" map<string, Project> projects = 1;\n"
|
|
|
|
" optional map<string, int32> size_projects = 2;\n"
|
|
|
|
" map<int, really.really.really.long.qualified.type.nameeee>\n"
|
|
|
|
" projects = 3;\n"
|
|
|
|
" map<int, really.really.really.really.long.qualified.type\n"
|
|
|
|
" .nameeee> projects = 4;\n"
|
|
|
|
" map<int,\n"
|
|
|
|
" reallyreallyreallyreallyreallyreallyreallylongname>\n"
|
|
|
|
" projects = 5;\n"
|
|
|
|
" map<int, Project>\n"
|
|
|
|
" longlonglonglonglonglonglonglonglonglongonglon = 6;\n"
|
|
|
|
" map<releleallyreallyreallyreallyreallyreallyreallylongname,\n"
|
|
|
|
" int> projects = 7;\n"
|
|
|
|
" map<releleallyreallyreallyreallyreallyreallyreallylongname,\n"
|
|
|
|
" releleallyreallyreallyreallyreallyreallyreallylongname>\n"
|
|
|
|
" releleallyreallyreallyreallyreallyreallyreallylongnam =\n"
|
|
|
|
" 8;\n"
|
|
|
|
" map<relele.llyreal.yreallyr.allyreally.eallyreal\n"
|
|
|
|
" .sauenirylongname,\n"
|
|
|
|
" really.really.really.really.long.qualified.type\n"
|
|
|
|
" .nameeee> projects = 9;\n"
|
|
|
|
"}");
|
2014-01-19 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 16:38:46 +08:00
|
|
|
TEST_F(FormatTestProto, KeywordsInOtherLanguages) {
|
|
|
|
verifyFormat("optional string operator = 1;");
|
|
|
|
}
|
|
|
|
|
2014-01-19 17:04:08 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsEnums) {
|
|
|
|
verifyFormat("enum Type {\n"
|
|
|
|
" UNKNOWN = 0;\n"
|
|
|
|
" TYPE_A = 1;\n"
|
|
|
|
" TYPE_B = 2;\n"
|
|
|
|
"};");
|
2016-06-23 17:40:19 +08:00
|
|
|
verifyFormat("enum Type {\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" UNKNOWN = 0 [(some_options) = { a: aa, b: bb }];\n"
|
2016-06-23 17:40:19 +08:00
|
|
|
"};");
|
2015-07-16 22:25:43 +08:00
|
|
|
verifyFormat("enum Type {\n"
|
|
|
|
" UNKNOWN = 0 [(some_options) = {\n"
|
2016-06-23 17:40:19 +08:00
|
|
|
" a: aa, // wrap\n"
|
2015-07-16 22:25:43 +08:00
|
|
|
" b: bb\n"
|
|
|
|
" }];\n"
|
|
|
|
"};");
|
2014-01-19 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
2019-03-23 22:24:30 +08:00
|
|
|
TEST_F(FormatTestProto, EnumAsFieldName) {
|
|
|
|
verifyFormat("message SomeMessage {\n"
|
|
|
|
" required int32 enum = 1;\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2014-01-19 17:04:08 +08:00
|
|
|
TEST_F(FormatTestProto, UnderstandsReturns) {
|
|
|
|
verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FormatTestProto, MessageFieldAttributes) {
|
|
|
|
verifyFormat("optional string test = 1 [default = \"test\"];");
|
2014-01-29 02:51:11 +08:00
|
|
|
verifyFormat("optional bool a = 1 [default = true, deprecated = true];");
|
2016-01-04 15:27:33 +08:00
|
|
|
verifyFormat("optional LongMessageType long_proto_field = 1 [\n"
|
|
|
|
" default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
|
|
|
|
" deprecated = true\n"
|
|
|
|
"];");
|
2014-01-19 17:04:08 +08:00
|
|
|
verifyFormat("optional LongMessageType long_proto_field = 1\n"
|
|
|
|
" [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
|
2014-01-30 02:43:40 +08:00
|
|
|
verifyFormat("repeated double value = 1\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" [(aaaaaaa.aaaaaaaaa) = { aaaaaaaaaaaaaaaaa: AAAAAAAA }];");
|
2014-10-27 21:25:59 +08:00
|
|
|
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
|
|
|
|
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
|
|
|
|
"}];");
|
|
|
|
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
|
|
|
|
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
|
|
|
|
"}];");
|
2016-01-04 15:27:33 +08:00
|
|
|
verifyFormat("repeated double value = 1 [\n"
|
|
|
|
" (aaaaaaa.aaaaaaaaa) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
|
|
|
|
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
|
|
|
|
" },\n"
|
|
|
|
" (bbbbbbb.bbbbbbbbb) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: AAAAAAAAAA\n"
|
|
|
|
" bbbbbbbbbbbbbbbb: BBBBBBBBBB\n"
|
|
|
|
" }\n"
|
|
|
|
"];");
|
2015-04-04 05:15:46 +08:00
|
|
|
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
|
|
|
|
" type: \"AAAAAAAAAA\"\n"
|
|
|
|
" is: \"AAAAAAAAAA\"\n"
|
|
|
|
" or: \"BBBBBBBBBB\"\n"
|
|
|
|
"}];");
|
2014-10-27 21:25:59 +08:00
|
|
|
verifyFormat("repeated double value = 1 [(aaaaaaa.aaaaaaaaa) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n"
|
|
|
|
" bbbbbbb: BBBB,\n"
|
|
|
|
" bbbb: BBB\n"
|
|
|
|
"}];");
|
2016-01-04 15:27:33 +08:00
|
|
|
verifyFormat("optional AAA aaa = 1 [\n"
|
|
|
|
" foo = {\n"
|
|
|
|
" key: 'a' //\n"
|
|
|
|
" },\n"
|
|
|
|
" bar = {\n"
|
|
|
|
" key: 'a' //\n"
|
|
|
|
" }\n"
|
|
|
|
"];");
|
2018-03-12 18:32:18 +08:00
|
|
|
verifyFormat("optional string test = 1 [default = \"test\"\n"
|
|
|
|
" \"test\"];");
|
2017-03-14 08:40:32 +08:00
|
|
|
verifyFormat("optional Aaaaaaaa aaaaaaaa = 12 [\n"
|
|
|
|
" (aaa) = aaaa,\n"
|
|
|
|
" (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaaa: true,\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: true\n"
|
|
|
|
" }\n"
|
|
|
|
"];");
|
2018-02-19 23:31:25 +08:00
|
|
|
verifyFormat("extensions 20 [(proto2.type) = 'Aaaa.bbbb'];");
|
|
|
|
verifyFormat("extensions 20\n"
|
|
|
|
" [(proto3.type) = 'Aaaa.bbbb', (aaa.Aaa) = 'aaa.bbb'];");
|
|
|
|
verifyFormat("extensions 123 [\n"
|
|
|
|
" (aaa) = aaaa,\n"
|
|
|
|
" (bbbbbbbbbbbbbbbbbbbbbbbbbb) = {\n"
|
|
|
|
" aaaaaaaaaaaaaaaaa: true,\n"
|
|
|
|
" aaaaaaaaaaaaaaaa: true\n"
|
|
|
|
" }\n"
|
|
|
|
"];");
|
2014-01-19 17:04:08 +08:00
|
|
|
}
|
|
|
|
|
2015-04-23 17:54:10 +08:00
|
|
|
TEST_F(FormatTestProto, DoesntWrapFileOptions) {
|
|
|
|
EXPECT_EQ(
|
|
|
|
"option java_package = "
|
|
|
|
"\"some.really.long.package.that.exceeds.the.column.limit\";",
|
|
|
|
format("option java_package = "
|
|
|
|
"\"some.really.long.package.that.exceeds.the.column.limit\";"));
|
|
|
|
}
|
|
|
|
|
2019-04-12 18:11:34 +08:00
|
|
|
TEST_F(FormatTestProto, TrailingCommentAfterFileOption) {
|
|
|
|
verifyFormat("option java_package = \"foo.pkg\"; // comment\n");
|
|
|
|
}
|
|
|
|
|
2014-01-21 00:47:22 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsOptions) {
|
2014-07-28 22:08:09 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: OK\n"
|
|
|
|
" field_b: \"OK\"\n"
|
|
|
|
" field_c: \"OK\"\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" msg_field: { field_d: 123 }\n"
|
2014-07-28 22:08:09 +08:00
|
|
|
"};");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: OK\n"
|
|
|
|
" field_b: \"OK\"\n"
|
|
|
|
" field_c: \"OK\"\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" msg_field: { field_d: 123 field_e: OK }\n"
|
2014-07-28 22:08:09 +08:00
|
|
|
"};");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: OK // Comment\n"
|
|
|
|
" field_b: \"OK\"\n"
|
|
|
|
" field_c: \"OK\"\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" msg_field: { field_d: 123 }\n"
|
2014-07-28 22:08:09 +08:00
|
|
|
"};");
|
2014-08-15 13:00:35 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_c: \"OK\"\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" msg_field { field_d: 123 }\n"
|
2014-08-15 13:00:35 +08:00
|
|
|
"};");
|
2017-06-29 21:30:41 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: OK\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" field_b { field_c: OK }\n"
|
2017-06-29 21:30:41 +08:00
|
|
|
" field_d: OKOKOK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
"}");
|
2016-01-04 15:28:12 +08:00
|
|
|
|
|
|
|
// Support syntax with <> instead of {}.
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_c: \"OK\",\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" msg_field: < field_d: 123 >\n"
|
|
|
|
" empty: <>\n"
|
|
|
|
" empty <>\n"
|
2016-01-04 15:28:12 +08:00
|
|
|
"};");
|
2017-06-27 21:43:07 +08:00
|
|
|
|
2017-06-29 21:30:41 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: OK\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" field_b < field_c: OK >\n"
|
2017-06-29 21:30:41 +08:00
|
|
|
" field_d: OKOKOK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
"}");
|
|
|
|
|
2017-06-27 21:43:07 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" msg_field: <>\n"
|
|
|
|
" field_c: \"OK\",\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" msg_field: < field_d: 123 >\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_e: OK\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" msg_field: < field_d: 12 >\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
"};");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: OK\n"
|
|
|
|
" field_b: \"OK\"\n"
|
|
|
|
" field_c: 1\n"
|
|
|
|
" field_d: 12.5\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: OK,\n"
|
|
|
|
" field_b: \"OK\",\n"
|
|
|
|
" field_c: 1,\n"
|
|
|
|
" field_d: 12.5,\n"
|
|
|
|
" field_e: OK,\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" msg_field: { field_b: OK }\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_g: OK\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field <\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" >\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field <\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_b: OK,\n"
|
|
|
|
" field_c: OK,\n"
|
|
|
|
" field_d: OK,\n"
|
|
|
|
" field_e: OK,\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" >\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
|
|
|
" msg_field: <\n"
|
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" >\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
|
|
|
" msg_field: {\n"
|
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" }\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field {\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" }\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: \"OK\"\n"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field <\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" >\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
"};");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: \"OK\"\n"
|
|
|
|
" msg_field: <\n"
|
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" >\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
"};");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" field_a: \"OK\"\n"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field {\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_b: OK\n"
|
|
|
|
" field_c: OK\n"
|
|
|
|
" field_d: OK\n"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field <\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_A: 1\n"
|
|
|
|
" field_B: 2\n"
|
|
|
|
" field_C: 3\n"
|
|
|
|
" field_D: 4\n"
|
|
|
|
" field_E: 5\n"
|
|
|
|
" >\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" msg_field < field_A: 1 field_B: 2 field_C: 3 f_D: 4 >\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" }\n"
|
|
|
|
" field_g: OK\n"
|
2017-08-03 21:43:45 +08:00
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" data1 < key1: value1 >\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" data2 { key2: value2 }\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
">;");
|
2017-08-03 22:17:29 +08:00
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = <\n"
|
|
|
|
" app_id: 'com.javax.swing.salsa.latino'\n"
|
|
|
|
" head_id: 1\n"
|
2018-02-06 19:34:34 +08:00
|
|
|
" data < key: value >\n"
|
2017-08-03 22:17:29 +08:00
|
|
|
">;");
|
|
|
|
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" app_id: 'com.javax.swing.salsa.latino'\n"
|
|
|
|
" head_id: 1\n"
|
|
|
|
" headheadheadheadheadhead_id: 1\n"
|
2018-01-31 18:14:10 +08:00
|
|
|
" product_data { product { 1 } }\n"
|
2017-08-03 22:17:29 +08:00
|
|
|
"};");
|
2014-01-21 00:47:22 +08:00
|
|
|
}
|
|
|
|
|
2019-05-10 17:15:11 +08:00
|
|
|
TEST_F(FormatTestProto, DoesntWrapPackageStatements) {
|
|
|
|
verifyFormat(
|
|
|
|
"package"
|
|
|
|
" some.really.long.package.that.exceeds.the.column.limit00000000;");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(FormatTestProto, TrailingCommentAfterPackage) {
|
|
|
|
verifyFormat("package foo.pkg; // comment\n");
|
|
|
|
}
|
|
|
|
|
2014-04-10 15:27:12 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsService) {
|
|
|
|
verifyFormat("service SearchService {\n"
|
|
|
|
" rpc Search(SearchRequest) returns (SearchResponse) {\n"
|
|
|
|
" option foo = true;\n"
|
|
|
|
" }\n"
|
|
|
|
"};");
|
|
|
|
}
|
|
|
|
|
2015-11-20 22:32:54 +08:00
|
|
|
TEST_F(FormatTestProto, ExtendingMessage) {
|
2018-08-16 03:07:55 +08:00
|
|
|
verifyFormat("extend .foo.Bar {}");
|
2015-11-20 22:32:54 +08:00
|
|
|
}
|
|
|
|
|
2016-06-21 02:20:38 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsImports) {
|
|
|
|
verifyFormat("import \"a.proto\";\n"
|
|
|
|
"import \"b.proto\";\n"
|
|
|
|
"// comment\n"
|
2018-08-16 03:07:55 +08:00
|
|
|
"message A {}");
|
2016-06-21 02:20:38 +08:00
|
|
|
|
2016-06-21 04:39:53 +08:00
|
|
|
verifyFormat("import public \"a.proto\";\n"
|
|
|
|
"import \"b.proto\";\n"
|
|
|
|
"// comment\n"
|
2018-08-16 03:07:55 +08:00
|
|
|
"message A {}");
|
2016-06-21 04:39:53 +08:00
|
|
|
|
2016-06-21 02:20:38 +08:00
|
|
|
// Missing semicolons should not confuse clang-format.
|
|
|
|
verifyFormat("import \"a.proto\"\n"
|
|
|
|
"import \"b.proto\"\n"
|
|
|
|
"// comment\n"
|
2018-08-16 03:07:55 +08:00
|
|
|
"message A {}");
|
2016-06-21 02:20:38 +08:00
|
|
|
}
|
|
|
|
|
2018-02-08 18:47:12 +08:00
|
|
|
TEST_F(FormatTestProto, KeepsLongStringLiteralsOnSameLine) {
|
|
|
|
verifyFormat(
|
|
|
|
"option (MyProto.options) = {\n"
|
|
|
|
" foo: {\n"
|
|
|
|
" text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasaaaaaaaa\"\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2018-02-13 18:20:39 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsOptionsExtensions) {
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" msg_field: { field_d: 123 }\n"
|
|
|
|
" [ext.t/u] { key: value }\n"
|
|
|
|
" key: value\n"
|
|
|
|
" [t.u/v] <\n"
|
|
|
|
" [ext] { key: value }\n"
|
|
|
|
" >\n"
|
|
|
|
"};");
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:33:03 +08:00
|
|
|
TEST_F(FormatTestProto, SpacesAroundPercents) {
|
2018-02-15 03:47:58 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: %lld\n"
|
2018-04-05 17:33:03 +08:00
|
|
|
" key: 0x%04x\n"
|
|
|
|
" key: \"%d %d\"\n"
|
2018-02-15 03:47:58 +08:00
|
|
|
"};");
|
|
|
|
}
|
|
|
|
|
2018-02-15 23:30:22 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsRepeatedListInitializersInOptions) {
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: item\n"
|
|
|
|
" keys: [\n"
|
|
|
|
" 'ala',\n"
|
|
|
|
" 'bala',\n"
|
|
|
|
" 'porto',\n"
|
|
|
|
" 'kala',\n"
|
|
|
|
" 'too',\n"
|
|
|
|
" 'long',\n"
|
|
|
|
" 'long',\n"
|
|
|
|
" 'long'\n"
|
|
|
|
" ]\n"
|
|
|
|
" key: [ item ]\n"
|
|
|
|
" msg {\n"
|
|
|
|
" key: item\n"
|
|
|
|
" keys: [\n"
|
|
|
|
" 'ala',\n"
|
|
|
|
" 'bala',\n"
|
|
|
|
" 'porto',\n"
|
|
|
|
" 'kala',\n"
|
|
|
|
" 'too',\n"
|
|
|
|
" 'long',\n"
|
|
|
|
" 'long'\n"
|
|
|
|
" ]\n"
|
|
|
|
" }\n"
|
|
|
|
" key: value\n"
|
|
|
|
"};");
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:07:47 +08:00
|
|
|
TEST_F(FormatTestProto, AcceptsOperatorAsKeyInOptions) {
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" bbbbbbbbb: <\n"
|
|
|
|
" ccccccccccccccccccccccc: <\n"
|
|
|
|
" operator: 1\n"
|
|
|
|
" operator: 2\n"
|
2018-05-09 17:02:11 +08:00
|
|
|
" operator: 3\n"
|
2018-02-28 03:07:47 +08:00
|
|
|
" operator { key: value }\n"
|
|
|
|
" >\n"
|
|
|
|
" >\n"
|
|
|
|
"};");
|
|
|
|
}
|
|
|
|
|
2018-06-11 20:53:25 +08:00
|
|
|
TEST_F(FormatTestProto, BreaksEntriesOfSubmessagesContainingSubmessages) {
|
|
|
|
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
|
|
|
|
Style.ColumnLimit = 60;
|
|
|
|
// The column limit allows for the keys submessage to be put on 1 line, but we
|
|
|
|
// break it since it contains a submessage an another entry.
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" sub <>\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" sub {}\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub {}\n"
|
|
|
|
" sub: <>\n"
|
|
|
|
" sub: []\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: 'aaaaaaaaaaa'\n"
|
|
|
|
" sub { msg: 1 }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: 'aaaaaaaaaaa'\n"
|
|
|
|
" sub: { msg: 1 }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: 'aaaaaaaaaaa'\n"
|
|
|
|
" sub < msg: 1 >\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: 'aaaaaaaaaaa'\n"
|
|
|
|
" sub: [ msg: 1 ]\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: <\n"
|
|
|
|
" item: 'aaaaaaaaaaa'\n"
|
|
|
|
" sub: [ 1, 2 ]\n"
|
|
|
|
" >\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub {}\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub: []\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub <>\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub { key: value }\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub: [ 1, 2 ]\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub < sub_2: {} >\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: data\n"
|
|
|
|
" sub: [ 1, 2 ]\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" item: data\n"
|
|
|
|
" sub < sub_2: {} >\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" sub: {\n"
|
|
|
|
" key: valueeeeeeee\n"
|
|
|
|
" keys: {\n"
|
|
|
|
" sub: [ 1, 2 ]\n"
|
|
|
|
" item: 'aaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2018-06-13 01:26:31 +08:00
|
|
|
TEST_F(FormatTestProto, PreventBreaksBetweenKeyAndSubmessages) {
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" submessage: {\n"
|
|
|
|
" key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" submessage {\n"
|
|
|
|
" key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" submessage: <\n"
|
|
|
|
" key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
|
|
|
|
" >\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" submessage <\n"
|
|
|
|
" key: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
|
|
|
|
" >\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" repeatedd: [\n"
|
|
|
|
" 'eyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'\n"
|
|
|
|
" ]\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:42:20 +08:00
|
|
|
} // namespace format
|
2014-01-19 17:04:08 +08:00
|
|
|
} // end namespace clang
|