2014-01-19 17:04:08 +08:00
|
|
|
//===- unittest/Format/FormatTestProto.cpp --------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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,
|
|
|
|
unsigned Length, const FormatStyle &Style) {
|
|
|
|
DEBUG(llvm::errs() << "---\n");
|
|
|
|
DEBUG(llvm::errs() << Code << "\n\n");
|
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
|
|
|
|
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));
|
|
|
|
DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
|
|
|
|
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) {
|
|
|
|
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"
|
|
|
|
" UNKNOWN = 0 [(some_options) = {a: aa, b: bb}];\n"
|
|
|
|
"};");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2014-04-15 17:54:30 +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"
|
|
|
|
"];");
|
2017-02-03 16:29:02 +08:00
|
|
|
verifyFormat("optional string test = 1 [default =\n"
|
|
|
|
" \"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"
|
|
|
|
"];");
|
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\";"));
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
" msg_field: {field_d: 123}\n"
|
|
|
|
"};");
|
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_a: OK\n"
|
|
|
|
" field_b: \"OK\"\n"
|
|
|
|
" field_c: \"OK\"\n"
|
2016-06-23 17:40:19 +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"
|
|
|
|
" msg_field: {field_d: 123}\n"
|
|
|
|
"};");
|
2014-08-15 13:00:35 +08:00
|
|
|
verifyFormat("option (MyProto.options) = {\n"
|
|
|
|
" field_c: \"OK\"\n"
|
2017-07-06 21:58:29 +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"
|
2017-07-06 21:58:29 +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"
|
|
|
|
" msg_field: <field_d: 123>\n"
|
|
|
|
"};");
|
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"
|
2017-07-06 21:58:29 +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"
|
|
|
|
" msg_field: <field_d: 123>\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" msg_field: <field_d: 12>\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"
|
|
|
|
" 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"
|
|
|
|
" msg_field: {field_b: OK}\n"
|
|
|
|
" 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"
|
2017-07-06 21:58:29 +08:00
|
|
|
" msg_field <field_A: 1 field_B: 2 field_C: 3 field_D: 4>\n"
|
2017-06-27 21:43:07 +08:00
|
|
|
" field_e: OK\n"
|
|
|
|
" field_f: OK\n"
|
|
|
|
" }\n"
|
|
|
|
" field_g: OK\n"
|
|
|
|
">;");
|
2014-01-21 00:47:22 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
verifyFormat("extend .foo.Bar {\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2016-06-21 02:20:38 +08:00
|
|
|
TEST_F(FormatTestProto, FormatsImports) {
|
|
|
|
verifyFormat("import \"a.proto\";\n"
|
|
|
|
"import \"b.proto\";\n"
|
|
|
|
"// comment\n"
|
|
|
|
"message A {\n"
|
|
|
|
"}");
|
|
|
|
|
2016-06-21 04:39:53 +08:00
|
|
|
verifyFormat("import public \"a.proto\";\n"
|
|
|
|
"import \"b.proto\";\n"
|
|
|
|
"// comment\n"
|
|
|
|
"message A {\n"
|
|
|
|
"}");
|
|
|
|
|
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"
|
|
|
|
"message A {\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
} // end namespace tooling
|
2014-01-19 17:04:08 +08:00
|
|
|
} // end namespace clang
|