2014-09-15 19:21:46 +08:00
|
|
|
//===- unittest/Format/FormatTestJava.cpp - Formatting tests for Java -----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-09-15 19:21:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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) {
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::errs() << "---\n");
|
|
|
|
LLVM_DEBUG(llvm::errs() << Code << "\n\n");
|
2014-09-15 19:21:46 +08:00
|
|
|
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
|
|
|
|
tooling::Replacements Replaces = reformat(Style, Code, Ranges);
|
2016-07-11 21:53:12 +08:00
|
|
|
auto Result = applyAllReplacements(Code, Replaces);
|
|
|
|
EXPECT_TRUE(static_cast<bool>(Result));
|
2018-05-15 21:30:56 +08:00
|
|
|
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
|
2016-07-11 21:53:12 +08:00
|
|
|
return *Result;
|
2014-09-15 19:21:46 +08:00
|
|
|
}
|
|
|
|
|
2015-06-17 21:08:06 +08:00
|
|
|
static std::string
|
|
|
|
format(llvm::StringRef Code,
|
|
|
|
const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
|
2014-09-15 19:21:46 +08:00
|
|
|
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)) {
|
2018-04-05 05:09:00 +08:00
|
|
|
EXPECT_EQ(Code.str(), format(Code, Style)) << "Expected code is not stable";
|
2014-09-15 19:21:46 +08:00
|
|
|
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-11-26 20:23:10 +08:00
|
|
|
TEST_F(FormatTestJava, UnderstandsCasts) {
|
|
|
|
verifyFormat("a[b >> 1] = (byte) (c() << 4);");
|
|
|
|
}
|
|
|
|
|
2014-11-25 18:05:17 +08:00
|
|
|
TEST_F(FormatTestJava, FormatsInstanceOfLikeOperators) {
|
|
|
|
FormatStyle Style = getStyleWithColumns(50);
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
" instanceof bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
|
|
|
|
Style);
|
|
|
|
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaa instanceof\n"
|
|
|
|
" bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
|
|
|
|
Style);
|
2015-07-03 18:12:53 +08:00
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaa instanceof bbbbbbbbbbbbbbbbbbbbbbb\n"
|
|
|
|
" && ccccccccccccccccccc instanceof dddddddddddddddddddddd;");
|
2014-11-25 18:05:17 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 00:43:18 +08:00
|
|
|
TEST_F(FormatTestJava, Chromium) {
|
|
|
|
verifyFormat("class SomeClass {\n"
|
|
|
|
" void f() {}\n"
|
|
|
|
" int g() {\n"
|
|
|
|
" return 0;\n"
|
|
|
|
" }\n"
|
|
|
|
" void h() {\n"
|
|
|
|
" while (true) f();\n"
|
|
|
|
" for (;;) f();\n"
|
|
|
|
" if (true) f();\n"
|
|
|
|
" }\n"
|
|
|
|
"}",
|
|
|
|
getChromiumStyle(FormatStyle::LK_Java));
|
|
|
|
}
|
|
|
|
|
2014-12-03 21:20:49 +08:00
|
|
|
TEST_F(FormatTestJava, QualifiedNames) {
|
|
|
|
verifyFormat("public some.package.Type someFunction( // comment\n"
|
|
|
|
" int parameter) {}");
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:17:08 +08:00
|
|
|
TEST_F(FormatTestJava, ClassKeyword) {
|
|
|
|
verifyFormat("SomeClass.class.getName();");
|
|
|
|
verifyFormat("Class c = SomeClass.class;");
|
|
|
|
}
|
|
|
|
|
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"
|
2014-11-26 18:43:58 +08:00
|
|
|
" extends bbbbbbbbbbbbbbb implements cccccccccccc {}",
|
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"
|
2014-11-26 18:43:58 +08:00
|
|
|
" extends bbbbbbbbbbbb<b> implements cccccccccccc {}",
|
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"
|
2018-01-24 00:30:56 +08:00
|
|
|
" default void doStuffWithDefault() {}\n"
|
2014-11-11 00:30:02 +08:00
|
|
|
"}");
|
|
|
|
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"
|
|
|
|
"}");
|
2018-10-20 00:19:52 +08:00
|
|
|
verifyFormat("class A {\n"
|
|
|
|
" public @interface SomeInterface {\n"
|
|
|
|
" int stuff;\n"
|
|
|
|
" void doMoreStuff(int moreStuff);\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("class A {\n"
|
|
|
|
" public @interface SomeInterface {}\n"
|
|
|
|
"}");
|
2014-09-15 19:21:46 +08:00
|
|
|
}
|
|
|
|
|
2015-03-12 22:44:29 +08:00
|
|
|
TEST_F(FormatTestJava, AnonymousClasses) {
|
|
|
|
verifyFormat("return new A() {\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" return \"NotReallyA\";\n"
|
|
|
|
" }\n"
|
|
|
|
"};");
|
|
|
|
verifyFormat("A a = new A() {\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" return \"NotReallyA\";\n"
|
|
|
|
" }\n"
|
|
|
|
"};");
|
|
|
|
}
|
|
|
|
|
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"
|
2014-11-26 18:43:58 +08:00
|
|
|
" void f() {}\n"
|
2014-11-03 06:31:39 +08:00
|
|
|
"}");
|
2014-11-13 23:56:28 +08:00
|
|
|
verifyFormat("public class SomeClass implements SomeInterface {\n"
|
|
|
|
" enum SomeThing { ABC, CDE }\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" void f() {}\n"
|
2014-11-13 23:56:28 +08:00
|
|
|
"}");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
|
|
|
" ABC,\n"
|
|
|
|
" CDE;\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" void f() {}\n"
|
2014-11-13 23:56:28 +08:00
|
|
|
"}");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
|
|
|
" ABC(1, \"ABC\"),\n"
|
|
|
|
" CDE(2, \"CDE\");\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" Something(int i, String s) {}\n"
|
2014-11-13 23:56:28 +08:00
|
|
|
"}");
|
|
|
|
verifyFormat("enum SomeThing {\n"
|
2014-11-24 04:54:37 +08:00
|
|
|
" ABC(new int[] {1, 2}),\n"
|
|
|
|
" CDE(new int[] {2, 3});\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" Something(int[] i) {}\n"
|
2014-11-13 23:56:28 +08:00
|
|
|
"}");
|
|
|
|
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"
|
2014-11-26 18:43:58 +08:00
|
|
|
" public void f() {}\n"
|
2014-11-13 23:56:28 +08:00
|
|
|
"}");
|
2014-11-20 06:38:18 +08:00
|
|
|
verifyFormat("private enum SomeEnum implements Foo<?, B> {\n"
|
|
|
|
" ABC {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" return \"ABC\";\n"
|
|
|
|
" }\n"
|
|
|
|
" },\n"
|
|
|
|
" CDE {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public String toString() {\n"
|
|
|
|
" return \"CDE\";\n"
|
|
|
|
" }\n"
|
|
|
|
" };\n"
|
|
|
|
"}");
|
clang-format: [Java] Fix bug in enum formatting.
Before:
public enum VeryLongEnum {
ENUM_WITH_MANY_PARAMETERS("aaaaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbbbbbbbbbbbbbbbbbbb",
"ccccccccccccccccccc")
,
SECOND_ENUM("a", "b", "c");
private VeryLongEnum(String a, String b, String c) {}
}
After:
public enum VeryLongEnum {
ENUM_WITH_MANY_PARAMETERS("aaaaaaaaaaaaaaaaaaaaaa",
"bbbbbbbbbbbbbbbbbbbbbbbbbbb",
"ccccccccccccccccccc") ,
SECOND_ENUM("a", "b", "c");
private VeryLongEnum(String a, String b, String c) {}
}
llvm-svn: 296499
2017-03-01 02:28:15 +08:00
|
|
|
verifyFormat("public enum VeryLongEnum {\n"
|
|
|
|
" ENUM_WITH_MANY_PARAMETERS(\n"
|
|
|
|
" \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"bbbbbbbbbbbbbbbb\", "
|
|
|
|
"\"cccccccccccccccccccccccc\"),\n"
|
|
|
|
" SECOND_ENUM(\"a\", \"b\", \"c\");\n"
|
|
|
|
" private VeryLongEnum(String a, String b, String c) {}\n"
|
|
|
|
"}\n");
|
2014-11-03 06:31:39 +08:00
|
|
|
}
|
|
|
|
|
2014-11-24 04:54:37 +08:00
|
|
|
TEST_F(FormatTestJava, ArrayInitializers) {
|
|
|
|
verifyFormat("new int[] {1, 2, 3, 4};");
|
|
|
|
verifyFormat("new int[] {\n"
|
clang-format: Do not binpack initialization lists
Summary:
This patch tries to avoid binpacking when initializing lists/arrays, to allow things like:
static int types[] = {
registerType1(),
registerType2(),
registerType3(),
};
std::map<int, std::string> x = {
{ 0, "foo fjakfjaklf kljj" },
{ 1, "bar fjakfjaklf kljj" },
{ 2, "stuff fjakfjaklf kljj" },
};
This is similar to how dictionnaries are formatted, and actually corresponds to the same conditions: when initializing a container (and not just 'calling' a constructor).
Such formatting involves 2 things:
* Line breaks around the content of the block. This can be forced by adding a comma or comment after the last element
* Elements should not be binpacked
This patch considers the block is an initializer list if it either ends with a comma, or follows an assignment, which seems to provide a sensible approximation.
Reviewers: krasimir, djasper
Reviewed By: djasper
Subscribers: malcolm.parsons, klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D34238
llvm-svn: 306868
2017-07-01 04:00:02 +08:00
|
|
|
" 1,\n"
|
|
|
|
" 2,\n"
|
|
|
|
" 3,\n"
|
|
|
|
" 4,\n"
|
2014-11-24 04:54:37 +08:00
|
|
|
"};");
|
2014-11-27 22:40:48 +08:00
|
|
|
|
|
|
|
FormatStyle Style = getStyleWithColumns(65);
|
|
|
|
Style.Cpp11BracedListStyle = false;
|
|
|
|
verifyFormat(
|
|
|
|
"expected = new int[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,\n"
|
|
|
|
" 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 };",
|
|
|
|
Style);
|
2014-11-24 04:54:37 +08:00
|
|
|
}
|
|
|
|
|
2014-10-17 21:36:14 +08:00
|
|
|
TEST_F(FormatTestJava, ThrowsDeclarations) {
|
|
|
|
verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" throws LooooooooooooooooooooooooooooongException {}");
|
2014-11-26 20:31:19 +08:00
|
|
|
verifyFormat("public void doSooooooooooooooooooooooooooomething()\n"
|
|
|
|
" throws LoooooooooongException, LooooooooooongException {}");
|
2014-10-17 21:36:14 +08:00
|
|
|
}
|
|
|
|
|
2014-10-21 16:24:18 +08:00
|
|
|
TEST_F(FormatTestJava, Annotations) {
|
|
|
|
verifyFormat("@Override\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
"public String toString() {}");
|
2014-10-21 16:24:18 +08:00
|
|
|
verifyFormat("@Override\n"
|
|
|
|
"@Nullable\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
"public String getNameIfPresent() {}");
|
2014-11-26 19:20:43 +08:00
|
|
|
verifyFormat("@Override // comment\n"
|
|
|
|
"@Nullable\n"
|
|
|
|
"public String getNameIfPresent() {}");
|
2015-01-10 07:25:06 +08:00
|
|
|
verifyFormat("@java.lang.Override // comment\n"
|
|
|
|
"@Nullable\n"
|
|
|
|
"public String getNameIfPresent() {}");
|
2014-10-21 17:25:39 +08:00
|
|
|
|
|
|
|
verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
"public void doSomething() {}");
|
2014-10-21 19:17:56 +08:00
|
|
|
verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
|
|
|
|
"@Author(name = \"abc\")\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
"public void doSomething() {}");
|
2014-10-21 17:25:39 +08:00
|
|
|
|
2014-10-21 18:02:03 +08:00
|
|
|
verifyFormat("DoSomething(new A() {\n"
|
|
|
|
" @Override\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" public String toString() {}\n"
|
2014-10-21 18:02:03 +08:00
|
|
|
"});");
|
|
|
|
|
2014-11-26 18:43:58 +08:00
|
|
|
verifyFormat("void SomeFunction(@Nullable String something) {}");
|
2015-01-10 07:25:06 +08:00
|
|
|
verifyFormat("void SomeFunction(@org.llvm.Nullable String something) {}");
|
2014-11-01 02:23:49 +08:00
|
|
|
|
2014-10-21 16:24:18 +08:00
|
|
|
verifyFormat("@Partial @Mock DataLoader loader;");
|
2015-10-16 00:03:01 +08:00
|
|
|
verifyFormat("@Partial\n"
|
|
|
|
"@Mock\n"
|
|
|
|
"DataLoader loader;",
|
|
|
|
getChromiumStyle(FormatStyle::LK_Java));
|
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;");
|
2015-01-10 07:25:06 +08:00
|
|
|
verifyFormat("@org.llvm.Qualified(\"With some really looooooooooong text\")\n"
|
|
|
|
"private static final long something = 0L;");
|
2014-12-09 04:08:04 +08:00
|
|
|
verifyFormat("@Mock\n"
|
|
|
|
"DataLoader loooooooooooooooooooooooader =\n"
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
|
|
|
|
getStyleWithColumns(60));
|
2015-01-10 07:25:06 +08:00
|
|
|
verifyFormat("@org.llvm.QualifiedMock\n"
|
|
|
|
"DataLoader loooooooooooooooooooooooader =\n"
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
|
|
|
|
getStyleWithColumns(60));
|
2015-01-14 17:47:57 +08:00
|
|
|
verifyFormat("@Test(a)\n"
|
|
|
|
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
|
|
|
|
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa);");
|
2015-01-14 17:51:32 +08:00
|
|
|
verifyFormat("@SomeAnnotation(\n"
|
|
|
|
" aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
"int i;",
|
|
|
|
getStyleWithColumns(50));
|
2015-01-14 18:00:20 +08:00
|
|
|
verifyFormat("@Test\n"
|
|
|
|
"ReturnType doSomething(\n"
|
|
|
|
" String aaaaaaaaaaaaa, String bbbbbbbbbbbbbbb) {}",
|
|
|
|
getStyleWithColumns(60));
|
2015-01-14 18:36:31 +08:00
|
|
|
verifyFormat("{\n"
|
|
|
|
" boolean someFunction(\n"
|
|
|
|
" @Param(aaaaaaaaaaaaaaaa) String aaaaa,\n"
|
|
|
|
" String bbbbbbbbbbbbbbb) {}\n"
|
|
|
|
"}",
|
|
|
|
getStyleWithColumns(60));
|
2016-01-28 04:14:23 +08:00
|
|
|
verifyFormat("@Annotation(\"Some\"\n"
|
|
|
|
" + \" text\")\n"
|
|
|
|
"List<Integer> list;");
|
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();");
|
2017-09-28 01:57:50 +08:00
|
|
|
verifyFormat("A.<B<C>>doSomething();");
|
|
|
|
verifyFormat("A.<B<C<D>>>doSomething();");
|
|
|
|
verifyFormat("A.<B<C<D<E>>>>doSomething();");
|
|
|
|
|
|
|
|
verifyFormat("OrderedPair<String, List<Box<Integer>>> p = null;");
|
2014-11-03 05:52:57 +08:00
|
|
|
|
|
|
|
verifyFormat("@Override\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
"public Map<String, ?> getAll() {}");
|
2014-11-03 10:35:14 +08:00
|
|
|
|
2014-11-26 18:43:58 +08:00
|
|
|
verifyFormat("public <R> ArrayList<R> get() {}");
|
|
|
|
verifyFormat("protected <R> ArrayList<R> get() {}");
|
|
|
|
verifyFormat("private <R> ArrayList<R> get() {}");
|
|
|
|
verifyFormat("public static <R> ArrayList<R> get() {}");
|
2015-01-14 06:32:50 +08:00
|
|
|
verifyFormat("public static native <R> ArrayList<R> get();");
|
2014-11-26 18:43:58 +08:00
|
|
|
verifyFormat("public final <X> Foo foo() {}");
|
2014-11-21 20:19:07 +08:00
|
|
|
verifyFormat("public abstract <X> Foo foo();");
|
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;");
|
2015-01-05 18:33:39 +08:00
|
|
|
verifyFormat("return (a instanceof List<?>)\n"
|
|
|
|
" ? aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
|
|
|
|
" : aaaaaaaaaaaaaaaaaaaaaaa;",
|
|
|
|
getStyleWithColumns(60));
|
2014-11-20 17:48:11 +08:00
|
|
|
|
|
|
|
verifyFormat(
|
|
|
|
"SomeLoooooooooooooooooooooongType name =\n"
|
|
|
|
" SomeType.foo(someArgument)\n"
|
|
|
|
" .<X>method()\n"
|
|
|
|
" .aaaaaaaaaaaaaaaaaaa()\n"
|
|
|
|
" .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
|
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"
|
2014-12-02 21:24:51 +08:00
|
|
|
" + \"cde\";");
|
2014-10-21 19:34:53 +08:00
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2015-01-14 18:48:41 +08:00
|
|
|
TEST_F(FormatTestJava, TryWithResources) {
|
|
|
|
verifyFormat("try (SomeResource rs = someFunction()) {\n"
|
|
|
|
" Something();\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("try (SomeResource rs = someFunction()) {\n"
|
|
|
|
" Something();\n"
|
|
|
|
"} catch (SomeException e) {\n"
|
|
|
|
" HandleException(e);\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2014-11-03 06:00:57 +08:00
|
|
|
TEST_F(FormatTestJava, SynchronizedKeyword) {
|
|
|
|
verifyFormat("synchronized (mData) {\n"
|
|
|
|
" // ...\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
2015-09-16 07:48:17 +08:00
|
|
|
TEST_F(FormatTestJava, AssertKeyword) {
|
|
|
|
verifyFormat("assert a && b;");
|
2017-09-26 06:42:49 +08:00
|
|
|
verifyFormat("assert (a && b);");
|
2015-09-16 07:48:17 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 02:03:42 +08:00
|
|
|
TEST_F(FormatTestJava, PackageDeclarations) {
|
|
|
|
verifyFormat("package some.really.loooooooooooooooooooooong.package;",
|
|
|
|
getStyleWithColumns(50));
|
|
|
|
}
|
|
|
|
|
2014-11-03 06:13:03 +08:00
|
|
|
TEST_F(FormatTestJava, ImportDeclarations) {
|
|
|
|
verifyFormat("import some.really.loooooooooooooooooooooong.imported.Class;",
|
|
|
|
getStyleWithColumns(50));
|
2015-01-14 18:02:49 +08:00
|
|
|
verifyFormat("import static some.really.looooooooooooooooong.imported.Class;",
|
|
|
|
getStyleWithColumns(50));
|
2014-11-03 06:13:03 +08:00
|
|
|
}
|
|
|
|
|
2014-11-19 07:55:27 +08:00
|
|
|
TEST_F(FormatTestJava, MethodDeclarations) {
|
|
|
|
verifyFormat("void methodName(Object arg1,\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" Object arg2, Object arg3) {}",
|
2014-11-19 07:55:27 +08:00
|
|
|
getStyleWithColumns(40));
|
|
|
|
verifyFormat("void methodName(\n"
|
2014-11-26 18:43:58 +08:00
|
|
|
" Object arg1, Object arg2) {}",
|
2014-11-19 07:55:27 +08:00
|
|
|
getStyleWithColumns(40));
|
|
|
|
}
|
|
|
|
|
2018-10-06 02:22:21 +08:00
|
|
|
TEST_F(FormatTestJava, MethodReference) {
|
|
|
|
EXPECT_EQ(
|
|
|
|
"private void foo() {\n"
|
|
|
|
" f(this::methodReference);\n"
|
|
|
|
" f(C.super::methodReference);\n"
|
|
|
|
" Consumer<String> c = System.out::println;\n"
|
|
|
|
" Iface<Integer> mRef = Ty::<Integer>meth;\n"
|
|
|
|
"}",
|
|
|
|
format("private void foo() {\n"
|
|
|
|
" f(this ::methodReference);\n"
|
|
|
|
" f(C.super ::methodReference);\n"
|
|
|
|
" Consumer<String> c = System.out ::println;\n"
|
|
|
|
" Iface<Integer> mRef = Ty :: <Integer> meth;\n"
|
|
|
|
"}"));
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:11:11 +08:00
|
|
|
TEST_F(FormatTestJava, CppKeywords) {
|
|
|
|
verifyFormat("public void union(Type a, Type b);");
|
|
|
|
verifyFormat("public void struct(Object o);");
|
|
|
|
verifyFormat("public void delete(Object o);");
|
2015-12-22 23:47:56 +08:00
|
|
|
verifyFormat("return operator && (aa);");
|
2014-11-19 22:11:11 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 17:54:49 +08:00
|
|
|
TEST_F(FormatTestJava, NeverAlignAfterReturn) {
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaa\n"
|
|
|
|
" && bbbbbbbbbbbbbbbbbbb\n"
|
|
|
|
" && ccccccccccccccccccc;",
|
|
|
|
getStyleWithColumns(40));
|
|
|
|
verifyFormat("return (result == null)\n"
|
|
|
|
" ? aaaaaaaaaaaaaaaaa\n"
|
|
|
|
" : bbbbbbbbbbbbbbbbb;",
|
|
|
|
getStyleWithColumns(40));
|
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
|
|
|
|
" .bbbbbbbbbbbbbbbbbbb()\n"
|
|
|
|
" .ccccccccccccccccccc();",
|
|
|
|
getStyleWithColumns(40));
|
2014-12-09 05:28:31 +08:00
|
|
|
verifyFormat("return aaaaaaaaaaaaaaaaaaa()\n"
|
|
|
|
" .bbbbbbbbbbbbbbbbbbb(\n"
|
2015-04-07 14:41:24 +08:00
|
|
|
" ccccccccccccccc)\n"
|
2014-12-09 05:28:31 +08:00
|
|
|
" .ccccccccccccccccccc();",
|
|
|
|
getStyleWithColumns(40));
|
2014-11-20 17:54:49 +08:00
|
|
|
}
|
|
|
|
|
2014-11-21 21:38:53 +08:00
|
|
|
TEST_F(FormatTestJava, FormatsInnerBlocks) {
|
|
|
|
verifyFormat("someObject.someFunction(new Runnable() {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public void run() {\n"
|
|
|
|
" System.out.println(42);\n"
|
|
|
|
" }\n"
|
|
|
|
"}, someOtherParameter);");
|
2015-01-05 04:40:51 +08:00
|
|
|
verifyFormat("someFunction(new Runnable() {\n"
|
|
|
|
" public void run() {\n"
|
|
|
|
" System.out.println(42);\n"
|
|
|
|
" }\n"
|
|
|
|
"});");
|
2014-11-21 21:38:53 +08:00
|
|
|
verifyFormat("someObject.someFunction(\n"
|
|
|
|
" new Runnable() {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public void run() {\n"
|
|
|
|
" System.out.println(42);\n"
|
|
|
|
" }\n"
|
|
|
|
" },\n"
|
|
|
|
" new Runnable() {\n"
|
|
|
|
" @Override\n"
|
|
|
|
" public void run() {\n"
|
|
|
|
" System.out.println(43);\n"
|
|
|
|
" }\n"
|
|
|
|
" },\n"
|
|
|
|
" someOtherParameter);");
|
|
|
|
}
|
|
|
|
|
2014-11-21 20:14:12 +08:00
|
|
|
TEST_F(FormatTestJava, FormatsLambdas) {
|
|
|
|
verifyFormat("(aaaaaaaaaa, bbbbbbbbbb) -> aaaaaaaaaa + bbbbbbbbbb;");
|
|
|
|
verifyFormat("(aaaaaaaaaa, bbbbbbbbbb)\n"
|
|
|
|
" -> aaaaaaaaaa + bbbbbbbbbb;",
|
|
|
|
getStyleWithColumns(40));
|
|
|
|
verifyFormat("Runnable someLambda = () -> DoSomething();");
|
|
|
|
verifyFormat("Runnable someLambda = () -> {\n"
|
|
|
|
" DoSomething();\n"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
verifyFormat("Runnable someLambda =\n"
|
|
|
|
" (int aaaaa) -> DoSomething(aaaaa);",
|
|
|
|
getStyleWithColumns(40));
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:03:22 +08:00
|
|
|
TEST_F(FormatTestJava, BreaksStringLiterals) {
|
|
|
|
// FIXME: String literal breaking is currently disabled for Java and JS, as it
|
|
|
|
// requires strings to be merged using "+" which we don't support.
|
|
|
|
EXPECT_EQ("\"some text other\";",
|
|
|
|
format("\"some text other\";", getStyleWithColumns(14)));
|
|
|
|
}
|
|
|
|
|
2015-01-04 17:11:17 +08:00
|
|
|
TEST_F(FormatTestJava, AlignsBlockComments) {
|
|
|
|
EXPECT_EQ("/*\n"
|
|
|
|
" * Really multi-line\n"
|
|
|
|
" * comment.\n"
|
|
|
|
" */\n"
|
|
|
|
"void f() {}",
|
|
|
|
format(" /*\n"
|
|
|
|
" * Really multi-line\n"
|
|
|
|
" * comment.\n"
|
|
|
|
" */\n"
|
|
|
|
" void f() {}"));
|
|
|
|
}
|
|
|
|
|
2017-07-21 06:29:39 +08:00
|
|
|
TEST_F(FormatTestJava, KeepsDelimitersOnOwnLineInJavaDocComments) {
|
|
|
|
EXPECT_EQ("/**\n"
|
|
|
|
" * javadoc line 1\n"
|
|
|
|
" * javadoc line 2\n"
|
|
|
|
" */",
|
|
|
|
format("/** javadoc line 1\n"
|
|
|
|
" * javadoc line 2 */"));
|
|
|
|
}
|
|
|
|
|
2017-04-11 23:50:04 +08:00
|
|
|
TEST_F(FormatTestJava, RetainsLogicalShifts) {
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
" int a = 1;\n"
|
|
|
|
" a >>>= 1;\n"
|
|
|
|
"}");
|
|
|
|
verifyFormat("void f() {\n"
|
|
|
|
" int a = 1;\n"
|
|
|
|
" a = a >>> 1;\n"
|
|
|
|
"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
} // end namespace tooling
|
2014-09-15 19:21:46 +08:00
|
|
|
} // end namespace clang
|