Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===// |
| 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 | #include "FormatTestUtils.h" |
| 11 | #include "clang/Format/Format.h" |
| 12 | #include "llvm/Support/Debug.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | #define DEBUG_TYPE "format-test" |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace format { |
| 19 | |
| 20 | class FormatTestJava : 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( |
| 35 | llvm::StringRef Code, |
| 36 | const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) { |
| 37 | return format(Code, 0, Code.size(), Style); |
| 38 | } |
| 39 | |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 40 | static void verifyFormat( |
| 41 | llvm::StringRef Code, |
| 42 | const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) { |
| 43 | EXPECT_EQ(Code.str(), format(test::messUp(Code), Style)); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | TEST_F(FormatTestJava, ClassDeclarations) { |
| 48 | verifyFormat("public class SomeClass {\n" |
| 49 | " private int a;\n" |
| 50 | " private int b;\n" |
| 51 | "}"); |
| 52 | verifyFormat("public class A {\n" |
| 53 | " class B {\n" |
| 54 | " int i;\n" |
| 55 | " }\n" |
| 56 | " class C {\n" |
| 57 | " int j;\n" |
| 58 | " }\n" |
| 59 | "}"); |
Daniel Jasper | 4bf9d47 | 2014-10-21 09:31:29 +0000 | [diff] [blame] | 60 | verifyFormat("public class A extends B.C {}"); |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Daniel Jasper | f26c755 | 2014-10-17 13:36:14 +0000 | [diff] [blame] | 63 | TEST_F(FormatTestJava, ThrowsDeclarations) { |
| 64 | verifyFormat("public void doSooooooooooooooooooooooooooomething()\n" |
| 65 | " throws LooooooooooooooooooooooooooooongException {}"); |
| 66 | } |
| 67 | |
Daniel Jasper | fab69ff | 2014-10-21 08:24:18 +0000 | [diff] [blame] | 68 | TEST_F(FormatTestJava, Annotations) { |
| 69 | verifyFormat("@Override\n" |
| 70 | "public String toString() {\n}"); |
| 71 | verifyFormat("@Override\n" |
| 72 | "@Nullable\n" |
| 73 | "public String getNameIfPresent() {\n}"); |
Daniel Jasper | f1f0c35 | 2014-10-21 09:25:39 +0000 | [diff] [blame] | 74 | |
| 75 | verifyFormat("@SuppressWarnings(value = \"unchecked\")\n" |
| 76 | "public void doSomething() {\n}"); |
| 77 | |
Daniel Jasper | c7d024a | 2014-10-21 10:02:03 +0000 | [diff] [blame] | 78 | verifyFormat("DoSomething(new A() {\n" |
| 79 | " @Override\n" |
| 80 | " public String toString() {\n" |
| 81 | " }\n" |
| 82 | "});"); |
| 83 | |
Daniel Jasper | fab69ff | 2014-10-21 08:24:18 +0000 | [diff] [blame] | 84 | verifyFormat("@Partial @Mock DataLoader loader;"); |
Daniel Jasper | fd68191 | 2014-10-21 10:58:14 +0000 | [diff] [blame^] | 85 | verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n" |
| 86 | "public static int iiiiiiiiiiiiiiiiiiiiiiii;"); |
Daniel Jasper | fab69ff | 2014-10-21 08:24:18 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Daniel Jasper | 16b107e | 2014-10-21 09:57:09 +0000 | [diff] [blame] | 89 | TEST_F(FormatTestJava, Generics) { |
| 90 | verifyFormat("Iterable<?> a;"); |
| 91 | verifyFormat("Iterable<?> a;"); |
| 92 | verifyFormat("Iterable<? extends SomeObject> a;"); |
| 93 | } |
| 94 | |
Daniel Jasper | c58c70e | 2014-09-15 11:21:46 +0000 | [diff] [blame] | 95 | } // end namespace tooling |
| 96 | } // end namespace clang |