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);
|
|
|
|
std::string Result = applyAllReplacements(Code, Replaces);
|
|
|
|
EXPECT_NE("", Result);
|
|
|
|
DEBUG(llvm::errs() << "\n" << Result << "\n\n");
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
"}");
|
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"
|
|
|
|
"};");
|
2015-07-16 22:25:43 +08:00
|
|
|
verifyFormat("enum Type {\n"
|
|
|
|
" UNKNOWN = 0 [(some_options) = {\n"
|
|
|
|
" a: aa,\n"
|
|
|
|
" 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];");
|
|
|
|
verifyFormat("optional LongMessageType long_proto_field = 1\n"
|
|
|
|
" [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n"
|
|
|
|
" deprecated = true];");
|
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"
|
|
|
|
"}];");
|
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"
|
|
|
|
"}];");
|
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"
|
2014-10-27 21:25:59 +08:00
|
|
|
" msg_field: {\n"
|
|
|
|
" field_d: 123\n"
|
|
|
|
" field_e: OK\n"
|
|
|
|
" }\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"
|
|
|
|
" msg_field{field_d: 123}\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-06-23 07:07:51 +08:00
|
|
|
} // end namespace tooling
|
2014-01-19 17:04:08 +08:00
|
|
|
} // end namespace clang
|