blob: 7623e46db44a1f7275e2473ab9fd7fc51bcaadf5 [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) {
35 return format(Code, 0, Code.size(), getGoogleProtoStyle());
36 }
37
38 static void verifyFormat(llvm::StringRef Code) {
39 EXPECT_EQ(Code.str(), format(test::messUp(Code)));
40 }
41};
42
43TEST_F(FormatTestProto, FormatsMessages) {
44 verifyFormat("message SomeMessage {\n"
45 " required int32 field1 = 1;\n"
46 "}");
47 verifyFormat("message SomeMessage {\n"
48 " required int32 field1 = 1;\n"
49 " optional string field2 = 2 [default = \"2\"]\n"
50 "}");
51}
52
53TEST_F(FormatTestProto, FormatsEnums) {
54 verifyFormat("enum Type {\n"
55 " UNKNOWN = 0;\n"
56 " TYPE_A = 1;\n"
57 " TYPE_B = 2;\n"
58 "};");
59}
60
61TEST_F(FormatTestProto, UnderstandsReturns) {
62 verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);");
63}
64
65TEST_F(FormatTestProto, MessageFieldAttributes) {
66 verifyFormat("optional string test = 1 [default = \"test\"];");
67 verifyFormat("optional LongMessageType long_proto_field = 1\n"
68 " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];");
69}
70
Daniel Jasper929b1db2014-01-20 16:47:22 +000071TEST_F(FormatTestProto, FormatsOptions) {
72 verifyFormat("option java_package = \"my.test.package\";");
73 verifyFormat("option (my_custom_option) = \"abc\";");
74}
75
Daniel Jasper7052ce62014-01-19 09:04:08 +000076} // end namespace tooling
77} // end namespace clang