2014-09-15 19:21:46 +08:00
|
|
|
//===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
|
|
|
|
//
|
|
|
|
// 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 FormatTestJava : 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,
|
|
|
|
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
|
|
|
|
return format(Code, 0, Code.size(), Style);
|
|
|
|
}
|
|
|
|
|
2014-11-03 03:16:41 +08:00
|
|
|
static FormatStyle getStyleWithColumns(unsigned ColumnLimit) {
|
|
|
|
FormatStyle Style = getGoogleStyle(FormatStyle::LK_Java);
|
|
|
|
Style.ColumnLimit = ColumnLimit;
|
|
|
|
return Style;
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:21:46 +08:00
|
|
|
static void verifyFormat(
|
|
|
|
llvm::StringRef Code,
|
|
|
|
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
|
|
|
|
EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-11-14 17:02:28 +08:00
|
|
|
TEST_F(FormatTestJava, NoAlternativeOperatorNames) {
|
|
|
|
verifyFormat("someObject.and();");
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:21:46 +08:00
|
|
|
TEST_F(FormatTestJava, ClassDeclarations) {
|
|
|
|
verifyFormat("public class SomeClass {\n"
|
|
|
|
" private int a;\n"
|
|
|
|
" private int b;\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("public class A {\n"
|
|
|
|
" class B {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" }\n"
|
|
|
|
" class C {\n"
|
|
|
|
" int j;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2014-10-21 17:31:29 +08:00
|
|
|
verifyFormat("public class A extends B.C {}");
|
2014-11-03 03:16:41 +08:00
|
|
|
|
2014-11-14 18:15:56 +08:00
|
|
|
verifyFormat("abstract class SomeClass\n"
|
|
|
|
" extends SomeOtherClass implements SomeInterface {}",
|
|
|
|
getStyleWithColumns(60));
|
2014-11-03 03:16:41 +08:00
|
|
|
verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
|
2014-11-14 18:15:56 +08:00
|
|
|
" implements SomeInterfaceeeeeeeeeeeee {}",
|
2014-11-03 03:16:41 +08:00
|
|
|
getStyleWithColumns(60));
|
|
|
|
verifyFormat("abstract class SomeClass\n"
|
|
|
|
" extends SomeOtherClass\n"
|
|
|
|
" implements SomeInterface {}",
|
|
|
|
getStyleWithColumns(40));
|
|
|
|
verifyFormat("abstract class SomeClass\n"
|
|
|
|
" extends SomeOtherClass\n"
|
|
|
|
" implements SomeInterface,\n"
|
|
|
|
" AnotherInterface {}",
|
|
|
|
getStyleWithColumns(40));
|
2014-11-14 18:15:56 +08:00
|
|
|
verifyFormat("abstract class SomeClass\n"
|
|
|
|
" implements SomeInterface, AnotherInterface {}",
|
|
|
|
getStyleWithColumns(60));
|
2014-11-03 10:27:28 +08:00
|
|
|
verifyFormat("@SomeAnnotation()\n"
|
2014-11-14 18:15:56 +08:00
|
|
|
"abstract class aaaaaaaaaaaa\n"
|
|
|
|
" extends bbbbbbbbbbbbbbb implements cccccccccccc {\n"
|
2014-11-03 10:27:28 +08:00
|
|
|
"}",
|
|
|
|
getStyleWithColumns(76));
|
2014-11-04 18:53:14 +08:00
|
|
|
verifyFormat("@SomeAnnotation()\n"
|
2014-11-14 18:15:56 +08:00
|
|
|
"abstract class aaaaaaaaa<a>\n"
|
|
|
|
" extends bbbbbbbbbbbb<b> implements cccccccccccc {\n"
|
2014-11-04 18:53:14 +08:00
|
|
|
"}",
|
|
|
|
getStyleWithColumns(76));
|
2014-11-11 00:30:02 +08:00
|
|
|
verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"
|
|
|
|
" void doStuff(int theStuff);\n"
|
|
|
|
" void doMoreStuff(int moreStuff);\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("public interface SomeInterface {\n"
|
|
|
|
" void doStuff(int theStuff);\n"
|
|
|
|
" void doMoreStuff(int moreStuff);\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("@interface SomeInterface {\n"
|
|
|
|
" void doStuff(int theStuff);\n"
|
|
|
|
" void doMoreStuff(int moreStuff);\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("public @interface SomeInterface {\n"
|
|
|
|
" void doStuff(int theStuff);\n"
|
|
|
|
" void doMoreStuff(int moreStuff);\n"
|
|
|
|
"}");
|
2014-09-15 19:21:46 +08:00
|
|
|
}
|
|
|
|
|
2014-11-03 06:31:39 +08:00
|
|
|
TEST_F(FormatTestJava, EnumDeclarations) {
|
|
|
|
verifyFormat("enum SomeThing { ABC, CDE }");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
|
|
|
" ABC,\n"
|
|
|
|
" CDE,\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("public class SomeClass {\n"
|
|
|
|
" enum SomeThing { ABC, CDE }\n"
|
|
|
|
" void f() {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2014-11-13 23:56:28 +08:00
|
|
|
verifyFormat("public class SomeClass implements SomeInterface {\n"
|
|
|
|
" enum SomeThing { ABC, CDE }\n"
|
|
|
|
" void f() {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
|
|
|
" ABC,\n"
|
|
|
|
" CDE;\n"
|
|
|
|
" void f() {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
|
|
|
" ABC(1, \"ABC\"),\n"
|
|
|
|
" CDE(2, \"CDE\");\n"
|
|
|
|
" Something(int i, String s) {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
|
|
|
" ABC(new int[]{1, 2}),\n"
|
|
|
|
" CDE(new int[]{2, 3});\n"
|
|
|
|
" Something(int[] i) {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("public enum SomeThing {\n"
|
|
|
|
" ABC {\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" return \"ABC\";\n"
|
|
|
|
" }\n"
|
|
|
|
" },\n"
|
|
|
|
" CDE {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" return \"CDE\";\n"
|
|
|
|
" }\n"
|
|
|
|
" };\n"
|
|
|
|
" public void f() {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2014-11-03 06:31:39 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 21:36:14 +08:00
|
|
|
TEST_F(FormatTestJava, ThrowsDeclarations) {
|
|
|
|
verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
|
2014-10-29 00:29:56 +08:00
|
|
|
" throws LooooooooooooooooooooooooooooongException {\n}");
|
2014-10-17 21:36:14 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 16:24:18 +08:00
|
|
|
TEST_F(FormatTestJava, Annotations) {
|
|
|
|
verifyFormat("@Override\n"
|
|
|
|
"public String toString() {\n}");
|
|
|
|
verifyFormat("@Override\n"
|
|
|
|
"@Nullable\n"
|
|
|
|
"public String getNameIfPresent() {\n}");
|
2014-10-21 17:25:39 +08:00
|
|
|
|
|
|
|
verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
|
|
|
|
"public void doSomething() {\n}");
|
2014-10-21 19:17:56 +08:00
|
|
|
verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
|
|
|
|
"@Author(name = \"abc\")\n"
|
|
|
|
"public void doSomething() {\n}");
|
2014-10-21 17:25:39 +08:00
|
|
|
|
2014-10-21 18:02:03 +08:00
|
|
|
verifyFormat("DoSomething(new A() {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" }\n"
|
|
|
|
"});");
|
|
|
|
|
2014-11-01 02:23:49 +08:00
|
|
|
verifyFormat("void SomeFunction(@Nullable String something) {\n"
|
|
|
|
"}");
|
|
|
|
|
2014-10-21 16:24:18 +08:00
|
|
|
verifyFormat("@Partial @Mock DataLoader loader;");
|
2014-10-21 18:58:14 +08:00
|
|
|
verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
|
|
|
|
"public static int iiiiiiiiiiiiiiiiiiiiiiii;");
|
2014-11-01 02:23:49 +08:00
|
|
|
|
|
|
|
verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
|
|
|
|
"private static final long something = 0L;");
|
2014-10-21 16:24:18 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 17:57:09 +08:00
|
|
|
TEST_F(FormatTestJava, Generics) {
|
|
|
|
verifyFormat("Iterable<?> a;");
|
|
|
|
verifyFormat("Iterable<?> a;");
|
|
|
|
verifyFormat("Iterable<? extends SomeObject> a;");
|
2014-10-21 19:13:31 +08:00
|
|
|
|
|
|
|
verifyFormat("A.<B>doSomething();");
|
2014-11-03 05:52:57 +08:00
|
|
|
|
|
|
|
verifyFormat("@Override\n"
|
2014-11-03 10:35:14 +08:00
|
|
|
"public Map<String, ?> getAll() {\n}");
|
|
|
|
|
2014-11-14 17:05:32 +08:00
|
|
|
verifyFormat("public <R> ArrayList<R> get() {\n}");
|
2014-11-15 01:30:15 +08:00
|
|
|
verifyFormat("protected <R> ArrayList<R> get() {\n}");
|
|
|
|
verifyFormat("private <R> ArrayList<R> get() {\n}");
|
2014-11-03 10:35:14 +08:00
|
|
|
verifyFormat("public static <R> ArrayList<R> get() {\n}");
|
2014-11-03 10:45:58 +08:00
|
|
|
verifyFormat("<T extends B> T getInstance(Class<T> type);");
|
2014-11-14 16:22:46 +08:00
|
|
|
verifyFormat("Function<F, ? extends T> function;");
|
2014-11-19 07:48:01 +08:00
|
|
|
|
|
|
|
verifyFormat("private Foo<X, Y>[] foos;");
|
|
|
|
verifyFormat("Foo<X, Y>[] foos = this.foos;");
|
2014-10-21 17:57:09 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 19:34:53 +08:00
|
|
|
TEST_F(FormatTestJava, StringConcatenation) {
|
|
|
|
verifyFormat("String someString = \"abc\"\n"
|
|
|
|
" + \"cde\";");
|
|
|
|
}
|
|
|
|
|
2014-11-03 03:21:48 +08:00
|
|
|
TEST_F(FormatTestJava, TryCatchFinally) {
|
|
|
|
verifyFormat("try {\n"
|
|
|
|
" Something();\n"
|
|
|
|
"} catch (SomeException e) {\n"
|
|
|
|
" HandleException(e);\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("try {\n"
|
|
|
|
" Something();\n"
|
|
|
|
"} finally {\n"
|
|
|
|
" AlwaysDoThis();\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("try {\n"
|
|
|
|
" Something();\n"
|
|
|
|
"} catch (SomeException e) {\n"
|
|
|
|
" HandleException(e);\n"
|
|
|
|
"} finally {\n"
|
|
|
|
" AlwaysDoThis();\n"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
verifyFormat("try {\n"
|
|
|
|
" Something();\n"
|
|
|
|
"} catch (SomeException | OtherException e) {\n"
|
|
|
|
" HandleException(e);\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2014-11-03 06:00:57 +08:00
|
|
|
TEST_F(FormatTestJava, SynchronizedKeyword) {
|
|
|
|
verifyFormat("synchronized (mData) {\n"
|
|
|
|
" // ...\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2014-11-03 06:13:03 +08:00
|
|
|
TEST_F(FormatTestJava, ImportDeclarations) {
|
|
|
|
verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
|
|
|
|
getStyleWithColumns(50));
|
|
|
|
}
|
|
|
|
|
2014-09-15 19:21:46 +08:00
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|