blob: 83e003465fa2d6befa4bd0d80a369368f406530f [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
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
17namespace 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) {
Daniel Jasper215d6c82014-01-22 08:04:52 +000035 FormatStyle Style = getGoogleProtoStyle();
36 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"
50 " required int32 field1 = 1;\n"
51 " optional string field2 = 2 [default = \"2\"]\n"
52 "}");
Daniel Jasper215d6c82014-01-22 08:04:52 +000053
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 "}");
Daniel Jasper7052ce62014-01-19 09:04:08 +000061}
62
63TEST_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
71TEST_F(FormatTestProto, UnderstandsReturns) {
72 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
73}
74
75TEST_F(FormatTestProto, MessageFieldAttributes) {
76 verifyFormat("optional string test = 1 [default = \"test\"];");
77 verifyFormat("optional LongMessageType long_proto_field = 1\n"
78 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
79}
80
Daniel Jasper929b1db2014-01-20 16:47:22 +000081TEST_F(FormatTestProto, FormatsOptions) {
82 verifyFormat("option java_package = \"my.test.package\";");
83 verifyFormat("option (my_custom_option) = \"abc\";");
84}
85
Daniel Jasper7052ce62014-01-19 09:04:08 +000086} // end namespace tooling
87} // end namespace clang