Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 1 | //===- 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 | |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 10 | #include "FormatTestUtils.h" |
| 11 | #include "clang/Format/Format.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "format-test" |
| 16 | |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 17 | namespace clang { |
| 18 | namespace format { |
| 19 | |
| 20 | class FormatTestProto : public ::testing::Test { |
| 21 | protected: |
| 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) { |
Nico Weber | 514ecc8 | 2014-02-02 20:50:45 +0000 | [diff] [blame] | 35 | FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto); |
Daniel Jasper | 215d6c8 | 2014-01-22 08:04:52 +0000 | [diff] [blame] | 36 | Style.ColumnLimit = 60; // To make writing tests easier. |
| 37 | return format(Code, 0, Code.size(), Style); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static void verifyFormat(llvm::StringRef Code) { |
| 41 | EXPECT_EQ(Code.str(), format(test::messUp(Code))); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | TEST_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 Jasper | 215d6c8 | 2014-01-22 08:04:52 +0000 | [diff] [blame] | 53 | |
| 54 | verifyFormat("message SomeMessage {\n" |
Nikola Smiljanic | e08a91e | 2014-05-08 00:05:13 +0000 | [diff] [blame^] | 55 | " optional really.really.long.qualified.type.aaa.aaaaaaa\n" |
Daniel Jasper | 215d6c8 | 2014-01-22 08:04:52 +0000 | [diff] [blame] | 56 | " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n" |
| 57 | " optional\n" |
Nikola Smiljanic | e08a91e | 2014-05-08 00:05:13 +0000 | [diff] [blame^] | 58 | " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n" |
Daniel Jasper | 215d6c8 | 2014-01-22 08:04:52 +0000 | [diff] [blame] | 59 | " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n" |
| 60 | "}"); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TEST_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 | |
| 71 | TEST_F(FormatTestProto, UnderstandsReturns) { |
| 72 | verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);"); |
| 73 | } |
| 74 | |
| 75 | TEST_F(FormatTestProto, MessageFieldAttributes) { |
| 76 | verifyFormat("optional string test = 1 [default = \"test\"];"); |
Daniel Jasper | a0e9be2 | 2014-01-28 18:51:11 +0000 | [diff] [blame] | 77 | verifyFormat("optional bool a = 1 [default = true, deprecated = true];"); |
| 78 | verifyFormat("optional LongMessageType long_proto_field = 1\n" |
| 79 | " [default = REALLY_REALLY_LONG_CONSTANT_VALUE,\n" |
| 80 | " deprecated = true];"); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 81 | verifyFormat("optional LongMessageType long_proto_field = 1\n" |
| 82 | " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];"); |
Daniel Jasper | 6e58fee | 2014-01-29 18:43:40 +0000 | [diff] [blame] | 83 | verifyFormat("repeated double value = 1\n" |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 84 | " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];"); |
Daniel Jasper | f24301d | 2014-01-29 18:52:43 +0000 | [diff] [blame] | 85 | verifyFormat("repeated double value = 1\n" |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 86 | " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n" |
| 87 | " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];"); |
Daniel Jasper | 9d3adc0 | 2014-04-15 09:54:24 +0000 | [diff] [blame] | 88 | verifyFormat("repeated double value = 1\n" |
Daniel Jasper | 783bac6 | 2014-04-15 09:54:30 +0000 | [diff] [blame] | 89 | " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA\n" |
| 90 | " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];"); |
Daniel Jasper | 883ae9d | 2014-04-29 15:54:14 +0000 | [diff] [blame] | 91 | verifyFormat("repeated double value = 1\n" |
| 92 | " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n" |
| 93 | " bbbbbbb: BBBB,\n" |
| 94 | " bbbb: BBB}];"); |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Daniel Jasper | 929b1db | 2014-01-20 16:47:22 +0000 | [diff] [blame] | 97 | TEST_F(FormatTestProto, FormatsOptions) { |
| 98 | verifyFormat("option java_package = \"my.test.package\";"); |
| 99 | verifyFormat("option (my_custom_option) = \"abc\";"); |
| 100 | } |
| 101 | |
Daniel Jasper | 220c0d1 | 2014-04-10 07:27:12 +0000 | [diff] [blame] | 102 | TEST_F(FormatTestProto, FormatsService) { |
| 103 | verifyFormat("service SearchService {\n" |
| 104 | " rpc Search(SearchRequest) returns (SearchResponse) {\n" |
| 105 | " option foo = true;\n" |
| 106 | " }\n" |
| 107 | "};"); |
| 108 | } |
| 109 | |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 110 | } // end namespace tooling |
| 111 | } // end namespace clang |