diff options
author | dim <dim@FreeBSD.org> | 2014-11-24 09:15:30 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2014-11-24 09:15:30 +0000 |
commit | 173a4f43a911175643bda81ee675e8d9269056ea (patch) | |
tree | 47df2c12b57214af6c31e47404b005675b8b7ffc /unittests/Format/FormatTestProto.cpp | |
parent | 88f7a7d5251a2d813460274c92decc143a11569b (diff) | |
download | FreeBSD-src-173a4f43a911175643bda81ee675e8d9269056ea.zip FreeBSD-src-173a4f43a911175643bda81ee675e8d9269056ea.tar.gz |
Vendor import of clang RELEASE_350/final tag r216957 (effectively, 3.5.0 release):
https://llvm.org/svn/llvm-project/cfe/tags/RELEASE_350/final@216957
Diffstat (limited to 'unittests/Format/FormatTestProto.cpp')
-rw-r--r-- | unittests/Format/FormatTestProto.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/unittests/Format/FormatTestProto.cpp b/unittests/Format/FormatTestProto.cpp new file mode 100644 index 0000000..bfd5025 --- /dev/null +++ b/unittests/Format/FormatTestProto.cpp @@ -0,0 +1,114 @@ +//===- 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" + +#define DEBUG_TYPE "format-test" + +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) { + FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto); + Style.ColumnLimit = 60; // To make writing tests easier. + return format(Code, 0, Code.size(), Style); + } + + 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" + "}"); + verifyFormat("message SomeMessage {\n" + " required .absolute.Reference field1 = 1;\n" + "}"); + verifyFormat("message SomeMessage {\n" + " required int32 field1 = 1;\n" + " optional string field2 = 2 [default = \"2\"]\n" + "}"); + + verifyFormat("message SomeMessage {\n" + " optional really.really.long.qualified.type.aaa.aaaaaaa\n" + " fiiiiiiiiiiiiiiiiiiiiiiiiield = 1;\n" + " optional\n" + " really.really.long.qualified.type.aaa.aaaaaaa.aaaaaaaa\n" + " another_fiiiiiiiiiiiiiiiiiiiiield = 2;\n" + "}"); +} + +TEST_F(FormatTestProto, FormatsEnums) { + verifyFormat("enum Type {\n" + " UNKNOWN = 0;\n" + " TYPE_A = 1;\n" + " TYPE_B = 2;\n" + "};"); +} + +TEST_F(FormatTestProto, UnderstandsReturns) { + verifyFormat("rpc Search(SearchRequest) returns (SearchResponse);"); +} + +TEST_F(FormatTestProto, MessageFieldAttributes) { + verifyFormat("optional string test = 1 [default = \"test\"];"); + 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];"); + verifyFormat("optional LongMessageType long_proto_field = 1\n" + " [default = REALLY_REALLY_LONG_CONSTANT_VALUE];"); + verifyFormat("repeated double value = 1\n" + " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaaa: AAAAAAAA}];"); + verifyFormat("repeated double value = 1\n" + " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n" + " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];"); + verifyFormat("repeated double value = 1\n" + " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA\n" + " bbbbbbbbbbbbbbbb: BBBBBBBBBB}];"); + verifyFormat("repeated double value = 1\n" + " [(aaaaaaa.aaaaaaaaa) = {aaaaaaaaaaaaaaaa: AAAAAAAAAA,\n" + " bbbbbbb: BBBB,\n" + " bbbb: BBB}];"); +} + +TEST_F(FormatTestProto, FormatsOptions) { + verifyFormat("option java_package = \"my.test.package\";"); + verifyFormat("option (my_custom_option) = \"abc\";"); +} + +TEST_F(FormatTestProto, FormatsService) { + verifyFormat("service SearchService {\n" + " rpc Search(SearchRequest) returns (SearchResponse) {\n" + " option foo = true;\n" + " }\n" + "};"); +} + +} // end namespace tooling +} // end namespace clang |