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 | |
| 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 | |
| 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) { |
Daniel Jasper | 215d6c8 | 2014-01-22 08:04:52 +0000 | [diff] [blame^] | 35 | FormatStyle Style = getGoogleProtoStyle(); |
| 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" |
| 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 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\"];"); |
| 77 | verifyFormat("optional LongMessageType long_proto_field = 1\n" |
| 78 | " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];"); |
| 79 | } |
| 80 | |
Daniel Jasper | 929b1db | 2014-01-20 16:47:22 +0000 | [diff] [blame] | 81 | TEST_F(FormatTestProto, FormatsOptions) { |
| 82 | verifyFormat("option java_package = \"my.test.package\";"); |
| 83 | verifyFormat("option (my_custom_option) = \"abc\";"); |
| 84 | } |
| 85 | |
Daniel Jasper | 7052ce6 | 2014-01-19 09:04:08 +0000 | [diff] [blame] | 86 | } // end namespace tooling |
| 87 | } // end namespace clang |