aboutsummaryrefslogtreecommitdiff
path: root/src/test/resources/com/puppycrawl/tools/checkstyle
diff options
context:
space:
mode:
authorAndrei Selkin <andreyselkin@gmail.com>2015-10-31 14:57:33 +0300
committerRoman Ivanov <ivanov-jr@mail.ru>2015-10-31 15:48:32 -0700
commit866daeeb2ad31e6a5a78235aa46819d9f966ee1c (patch)
treecf0b244dba4b5dadefabe97b0666cf9439c0f76d /src/test/resources/com/puppycrawl/tools/checkstyle
parentc56dce9222a819b4741b4c0cd664d38c58b31050 (diff)
downloadcheckstyle-866daeeb2ad31e6a5a78235aa46819d9f966ee1c.tar.gz
Issue #2078: Update UTs for CommentsIndentationCheck
Diffstat (limited to 'src/test/resources/com/puppycrawl/tools/checkstyle')
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationCommentIsAtTheEndOfBlock.java367
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInEmptyBlock.java86
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInSwitchBlock.java268
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationNoNpe.java (renamed from src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationNpe.java)2
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationSurroundingCode.java147
5 files changed, 789 insertions, 81 deletions
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationCommentIsAtTheEndOfBlock.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationCommentIsAtTheEndOfBlock.java
new file mode 100644
index 000000000..49a5aa8f1
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationCommentIsAtTheEndOfBlock.java
@@ -0,0 +1,367 @@
+package com.puppycrawl.tools.checkstyle.checks.indentation;
+
+import com.puppycrawl.tools.checkstyle.api.DetailAST;
+import com.puppycrawl.tools.checkstyle.utils.CheckUtils;
+
+/**
+ * Contains examples of using comments at the end of the block.
+ */
+public class InputCommentsIndentationCommentIsAtTheEndOfBlock {
+
+ public void foo1() {
+ foo2();
+ // TODO: missing functionality
+ }
+
+ public void foo2() {
+ foo3();
+ // violation
+ }
+
+ public void foo3() {
+ foo2();
+ // refreshDisplay();
+ }
+
+ public void foo4() {
+ foooooooooooooooooooooooooooooooooooooooooo();
+ // ^-- some hint
+ }
+
+ public void foooooooooooooooooooooooooooooooooooooooooo() { }
+
+ /////////////////////////////// violation (a single line border to separate a group of methods)
+
+ public void foo7() {
+ int a = 0;
+// violation
+ }
+
+ /////////////////////////////// (a single line border to separate a group of methods)
+
+ public void foo8() {}
+
+ public class TestClass {
+ public void test() {
+ int a = 0;
+ // violation
+ }
+ // violation
+ }
+
+ public void foo9() {
+ this.foo1();
+ // violation
+ }
+
+ // public void foo10() {
+ //
+ // }
+
+ public void foo11() {
+ CheckUtils
+ .getFirstNode(new DetailAST())
+ .getFirstChild()
+ .getNextSibling();
+ // comment
+ }
+
+ public void foo12() {
+ CheckUtils
+ .getFirstNode(new DetailAST())
+ .getFirstChild()
+ .getNextSibling();
+ // violation
+ }
+
+ public void foo13() {
+ CheckUtils.getFirstNode(new DetailAST())
+ .getFirstChild()
+ .getNextSibling();
+ // comment
+ }
+
+ public void foo14() {
+ CheckUtils.getFirstNode(new DetailAST())
+ .getFirstChild()
+ .getNextSibling();
+ // violation
+ }
+
+ public void foo15() {
+ CheckUtils
+ .getFirstNode(new DetailAST());
+ // comment
+ }
+
+ public void foo16() {
+ CheckUtils
+ .getFirstNode(new DetailAST());
+ // violation
+ }
+
+ public void foo17() {
+ CheckUtils
+ .getFirstNode(new DetailAST())
+ .getFirstChild()
+ // comment
+ .getNextSibling();
+ }
+
+ public void foo18() {
+ CheckUtils
+ .getFirstNode(new DetailAST())
+ .getFirstChild()
+ // violation
+ .getNextSibling();
+ }
+
+ public void foo19() {
+ (new Thread(new Runnable() {
+ @Override
+ public void run() {
+
+ }
+ })).
+ run();
+ // comment
+ }
+
+ public void foo20() {
+ (new Thread(new Runnable() {
+ @Override
+ public void run() {
+
+ }
+ })).
+ run();
+ // violation
+ }
+
+ public void foo21() {
+ int[] array = new int[5];
+
+ java.util.List<String> expected = new java.util.ArrayList<>();
+ for (int i = 0; i < 5; i++) {
+ org.junit.Assert.assertEquals(expected.get(i), array[i]);
+ }
+ String s = String.format("The array element "
+ + "immediately following the end of the collection should be nulled",
+ array[1]);
+ // the above example was taken from hibernate-orm and was modified a bit
+ }
+
+ public void foo22() {
+ int[] array = new int[5];
+
+ java.util.List<String> expected = new java.util.ArrayList<>();
+ for (int i = 0; i < 5; i++) {
+ org.junit.Assert.assertEquals(expected.get(i), array[i]);
+ }
+ String s = String.format("The array element "
+ + "immediately following the end of the collection should be nulled",
+ array[1]);
+ // violation
+ }
+
+ public void foo23() {
+ new Object();
+ // comment
+ }
+
+ public void foo24() {
+ new Object();
+ // violation
+ }
+
+ public String foo25() {
+ return String.format("%d",
+ 1);
+ // comment
+ }
+
+ public String foo26() {
+ return String.format("%d",
+ 1);
+ // violation
+ }
+
+ public void foo27() {
+ // comment
+ // block
+ foo17();
+
+ // TODO
+ }
+
+ public String foo28() {
+ int a = 5;
+ return String.format("%d",
+ 1);
+ // comment
+ }
+
+ public String foo29() {
+ int a = 5;
+ return String.format("%d",
+ 1);
+ // violation
+ }
+
+ public void foo30() {
+ // comment
+ int a = 5;
+// violation
+ }
+
+ public void foo31() {
+ String s = new String ("A"
+ + "B"
+ + "C");
+ // comment
+ }
+
+ public void foo32() {
+ String s = new String ("A"
+ + "B"
+ + "C");
+ // violation
+ }
+
+ public void foo33() {
+ // comment
+ this.foo22();
+// violation
+ }
+
+ public void foo34() throws Exception {
+ throw new Exception("",
+ new Exception()
+ );
+ // comment
+ }
+
+ public void foo35() throws Exception {
+ throw new Exception("",
+ new Exception()
+ );
+ // violation
+ }
+
+ public void foo36() throws Exception {
+ throw new Exception("",
+ new Exception()
+ );
+// violation
+ }
+
+ public void foo37() throws Exception {
+ throw new Exception("", new Exception());
+ // comment
+ }
+
+ public void foo38() throws Exception {
+ throw new Exception("", new Exception());
+ // violation
+ }
+
+ public void foo39() throws Exception {
+ throw new Exception("",
+ new Exception());
+ // violation
+ }
+
+ public void foo40() throws Exception {
+ int a = 88;
+ throw new Exception("", new Exception());
+ // violation
+ }
+
+ public void foo41() throws Exception {
+ int a = 88;
+ throw new Exception("", new Exception());
+ // comment
+ }
+
+ public void foo42() {
+ int a = 5;
+ if (a == 5) {
+ int b;
+ // comment
+ } else if (a ==6) {
+
+ }
+ }
+
+ public void foo43() {
+ try {
+ int a;
+ // comment
+ } catch (Exception e) {
+
+ }
+ }
+
+ public void foo44() {
+ int ar = 5;
+ // comment
+ ar = 6;
+ // comment
+ }
+
+ public void foo45() {
+ int ar = 5;
+ // comment
+ ar = 6;
+ // violation
+ }
+
+ public void foo46() {
+// comment
+// block
+// violation
+ }
+
+ public void foo47() {
+ int a = 5;
+ // comment
+ // block
+ // comment
+ }
+
+ public void foo48() {
+ int a = 5;
+// comment
+// block
+// violation
+ }
+
+ public void foo49() {
+ // comment
+ // block
+ // ok
+ }
+
+ public void foo50() {
+ return;
+
+ // No NPE here!
+ }
+
+ public String foo51() {
+ return String
+ .valueOf("11"
+ );
+ // violation
+ }
+
+ public String foo52() {
+ return String
+ .valueOf("11"
+ );
+ // comment
+ }
+
+ // We almost reached the end of the class here.
+}
+// The END of the class.
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInEmptyBlock.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInEmptyBlock.java
new file mode 100644
index 000000000..d032fa8cf
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInEmptyBlock.java
@@ -0,0 +1,86 @@
+package com.puppycrawl.tools.checkstyle.checks.indentation;
+
+public class InputCommentsIndentationInEmptyBlock {
+
+ private void foo1() {
+ int a = 5, b = 3, v = 6;
+ if (a == b
+ && v == b || ( a ==1
+ /// violation
+ /* violation
+ * one fine day ... */
+ && b == 1) ) {
+ // Cannot clearly detect user intention of explanation target.
+ }
+ }
+
+ private void foo2() {
+ int a = 5, b = 3, v = 6;
+ if (a == b
+ && v == b || ( a ==1
+ && b == 1) ) {
+
+
+ // comment
+ }
+ }
+
+ private void foo3() {
+ int a = 5, b = 3, v = 6;
+ if (a == b
+ && v == b || (a == 1
+ && b == 1)) {
+// violation
+ }
+ }
+
+ // Comments here should be ok by Check
+ @SuppressWarnings("unused") // trailing
+ private static void foo4() { // trailing
+ if (true) // trailing comment
+ {
+ // some comment
+ }
+ if (true) { // trailing comment
+
+ }
+ /**
+ *
+ */
+ }
+
+ // Comments here should be ok by Check
+ @SuppressWarnings("unused") // trailing
+ private static void foo5() { // trailing
+ if (true) // trailing comment
+ {
+// violation
+ }
+ if (true) { // trailing comment
+
+ }
+ /**
+ *
+ */
+ }
+
+ public void foo6() {
+ try {
+
+ } catch (Exception e) {
+// violation
+ }
+ }
+
+ public void foo7() {
+ try {
+
+ } catch (Exception e) {
+ // TODO: handle exception here
+ }
+ }
+
+ private static class MyClass extends Object {
+ // no members
+ }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInSwitchBlock.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInSwitchBlock.java
new file mode 100644
index 000000000..eb9d9aded
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationInSwitchBlock.java
@@ -0,0 +1,268 @@
+package com.puppycrawl.tools.checkstyle.checks.indentation;
+
+public class InputCommentsIndentationInSwitchBlock {
+
+ private static void fooSwitch() {
+ switch("") {
+ case "0": //some comment
+ case "1":
+ // my comment
+ foo1();
+ break;
+ case "2":
+ // my comment
+ //comment
+ foo1();
+ // comment
+ break;
+ case "3":
+ /* com */
+ foo1();
+ /* com */
+ break;
+ case "5":
+ foo1();
+ // violation
+ case "6":
+ int k = 7;
+ // fall through
+ case "7":
+ if (true) {}
+ // violation
+ case "8":
+ break;
+ case "9":
+ foo1();
+ // fall through
+ case "10": {
+ if (true) {}
+ // fall through
+ }
+ case "11": {
+ // fall through
+ }
+ case "28": {
+ // fall through
+ }
+ case "12": {
+ // violation
+ int i;
+ }
+ case "13": {
+ // some comment in empty case block
+ }
+ case "14": {
+ // TODO: violation
+ }
+ case "15": {
+ foo1();
+ // violation
+ }
+ case "16": {
+ int a;
+ }
+ // fall through
+ case "17": {
+ int a;
+ }
+ // violation
+ case "18": { System.out.println();
+ } // trailing comment
+ case "19":
+ // comment
+ case "20":
+ // comment
+ case "21":
+ default:
+ // comment
+ break;
+ }
+ }
+
+ private static void foo1() {
+ if (true) {
+ switch(1) {
+ case 0:
+
+ case 1:
+ // violation
+ int b = 10;
+ default:
+ // comment
+ }
+
+ }
+ }
+
+ public void fooDotInCaseBlock() {
+ int i = 0;
+ String s = "";
+
+ switch (i) {
+ case -2:
+ // what
+ i++;
+ // no break here
+ case 0:
+ // what
+ s.indexOf("ignore");
+ // no break here
+ case -1:
+ // what
+ s.indexOf("no way");
+ // violation
+ case 1:
+ case 2:
+ i--;
+ // no break here
+ case 3: { }
+ // fall through
+
+
+ }
+
+ String breaks = ""
+ // violation
+ + "</table>"
+ // middle
+ + ""
+ // end
+ ;
+ }
+
+ public void foo2() {
+ int a = 1;
+ switch (a) {
+ case 1:
+ default:
+ // violation
+ }
+ }
+
+ public void foo3() {
+ int a = 1;
+ switch (a) {
+ case 1:
+ default:
+
+ // comment
+ }
+ }
+
+ public void foo4() {
+ int a = 1;
+ switch (a) {
+ case 1:
+ int b;
+ // violation
+ default:
+ }
+ }
+
+ public void foo5() {
+ int a = 1;
+ switch (a) {
+ case 1:
+ int b;
+ // comment
+ default:
+ }
+ }
+
+ public void foo6() {
+ int a = 1;
+ switch (a) {
+ case 1:
+ int b;
+ // comment
+ default:
+ }
+ }
+
+ public void foo7() {
+ int a = 2;
+ String s = "";
+ switch (a) {
+ // comment
+ // comment
+ // comment
+ case 1:
+ case 2:
+ // comment
+ // comment
+ foo1();
+ // comment
+ case 3:
+ // comment
+ // comment
+ // comment
+ case 4:
+ // violation
+ case 5:
+ s.toString().toString().toString();
+ // violation
+ // violation
+ // violation
+ default:
+ }
+ }
+
+ public void foo8() {
+ int a = 2;
+ String s = "";
+ switch (a) {
+ // comment
+ // comment
+ // comment
+ case 1:
+ case 2:
+ // comment
+ // comment
+ foo1();
+ // comment
+ case 3:
+ // comment
+ // comment
+ s.toString().toString().toString();
+ // comment
+ case 4:
+ // violation
+ default:
+ }
+ }
+
+ public void foo9() {
+ int a = 5;
+ switch (a) {
+ case 1:
+ case 2:
+ // comment
+ }
+ }
+
+ public void foo10() {
+ int a = 5;
+ switch (a) {
+ case 1:
+ default:
+ // comment
+ }
+ }
+
+ public void foo11() {
+ int a = 5;
+ switch (a) {
+ case 1:
+ case 2:
+ // comment
+ }
+ }
+
+ public void foo12() {
+ int a = 5;
+ switch (a) {
+ // comment
+ case 1:
+ }
+ }
+}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationNpe.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationNoNpe.java
index da3909b83..a217d802a 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationNpe.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationNoNpe.java
@@ -26,7 +26,7 @@ import java.lang.reflect.Method;
* Provides convenience methods such as {@link #isPublic} and {@link #isPackagePrivate}.
*
*/
-class InputCommentsIndentationNpe {
+class InputCommentsIndentationNoNpe {
}
/* The Check should not throw NPE here! */
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationSurroundingCode.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationSurroundingCode.java
index e308cdbb1..0a13df6e7 100644
--- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationSurroundingCode.java
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/indentation/InputCommentsIndentationSurroundingCode.java
@@ -6,34 +6,34 @@ import java.util.*;
// some
public class InputCommentsIndentationSurroundingCode
{
- private void foo() {
+ private void foo1() {
if (true) {
// here initialize some variables
int k = 0; // trailing comment
- // initialize b
+ // violation
int b = 10;
// sss
}
}
- private void foo1() {
+ private void foo2() {
if (true) {
/* some */
int k = 0;
- /* some */
+ /* violation */
int b = 10;
- /*
+ /* violation
* */
double d; /* trailing comment */
- /*
- *
+ /* violation
+ *
*/
boolean bb;
/***/
/* my comment*/
/*
- *
- *
+ *
+ *
* some
*/
/*
@@ -42,76 +42,24 @@ public class InputCommentsIndentationSurroundingCode
boolean x;
}
}
-
+
private void foo3() {
int a = 5, b = 3, v = 6;
if (a == b
- && v == b || ( a ==1
- /// What about that case ? the same for block comments
- /*
+ && v == b || ( a ==1
+ /// violation
+ /* violation
* one fine day ... */
&& b == 1) ) {
- // code
- }
- }
-
- // Comments here should be ok by Check
- @SuppressWarnings("unused") // trailing
- private static void check () { // trailing
- if (true) // trailing comment
- {
- // some comment
}
- if (true) { // trailing comment
-
- }
- /**
- *
- */
}
-
+
private static void com() {
/* here's my weird trailing comment */ boolean b = true;
}
-
- private static void cases() {
- switch("") {
- case "0": //some comment
- case "1":
- // my comment
- com();
- break;
- case "2":
- // my comment
- //comment
- check();
- // comment
- break;
- case "3":
- /* com */
- check();
- /* com */
- break;
- case "5":
- check();
- // fall through
- case "6":
- int k = 7;
- // fall through
- case "7":
- if (true) {}
- // fall through
- case "8":
- break;
- default:
- // comment
- break;
- }
- }
-
private static final String[][] mergeMatrix = {
- // TOP ALWAYS NEVER UNKNOWN
+ // This example of trailing block comments was found in PMD sources.
/* TOP */{ "", },
/* ALWAYS */{ "", "", },
/* NEVER */{ "NEVER", "UNKNOWN", "NEVER", },
@@ -125,18 +73,57 @@ public class InputCommentsIndentationSurroundingCode
/**
* some javadoc
*/
- private static void l() {}
-
- private void foo32() {
- if (true) {
- switch(1) {
- case 0:
-
- case 1:
- // initialize b
- int b = 10;
- }
-
- }
+ private static void l() {
+ }
+
+ public void foid5() {
+ String s = "";
+ s.toString().toString().toString();
+ // comment
}
+
+ public void foo6() {
+ // comment
+ // ...
+ // block
+ // ...
+ // violation
+ String someStr = new String();
+ }
+
+ public void foo7() {
+ // comment
+ // ...
+ // block
+ // violation
+ // comment
+ String someStr = new String();
+ }
+
+ public void foo8() {
+ String s = new String(); // comment
+ // ...
+ // block
+ // ...
+ // violation
+ String someStr = new String();
+ }
+
+
+ public String foo9(String s1, String s2, String s3) {
+ return "";
+ }
+
+ public void foo10()
+ throws Exception {
+
+ final String pattern = "^foo$";
+
+ final String[] expected = {
+ "7:13: " + foo9("", "", ""),
+ // comment
+ };
+ }
+
} // The Check should not throw NPE here!
+// The Check should not throw NPE here!