From f9d79d084f20645eaf838a1fb9b665e00f5fa799 Mon Sep 17 00:00:00 2001 From: Stoyan Kostadinov Date: Sat, 29 Jul 2023 15:50:13 +0300 Subject: [PATCH] Issue #13100: Create tests from InnerTypeLast xdoc examples --- .../checks/design/InnerTypeLastCheck.java | 26 ------- .../internal/XdocsJavaDocsTest.java | 3 +- .../InnerTypeLastCheckExamplesTest.java | 47 ++++++++++++ .../checks/design/innertypelast/Example1.txt | 27 +++++++ src/xdocs/checks/design/innertypelast.xml | 10 ++- .../checks/design/innertypelast.xml.template | 72 +++++++++++++++++++ 6 files changed, 155 insertions(+), 30 deletions(-) create mode 100644 src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckExamplesTest.java create mode 100644 src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/design/innertypelast/Example1.txt create mode 100644 src/xdocs/checks/design/innertypelast.xml.template diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java index d7ce3250f48..6ec605ae82c 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheck.java @@ -35,32 +35,6 @@ * method, constructor and field declarations. *

*

- * To configure the check: - *

- *
- * <module name="InnerTypeLast"/>
- * 
- *

Example:

- *
- * class Test {
- *     private String s; // OK
- *     class InnerTest1 {}
- *     public void test() {} // violation, method should be declared before inner types.
- * }
- *
- * class Test2 {
- *     static {}; // OK
- *     class InnerTest1 {}
- *     public Test2() {} // violation, constructor should be declared before inner types.
- * }
- *
- * class Test3 {
- *     private String s; // OK
- *     public void test() {} // OK
- *     class InnerTest1 {}
- * }
- * 
- *

* Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} *

*

diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java index 7d8ae08a139..bf55a79ba8b 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsJavaDocsTest.java @@ -116,7 +116,8 @@ public class XdocsJavaDocsTest extends AbstractModuleTestSupport { // We skip validation of examples section on modules that have this section generated // until https://github.com/checkstyle/checkstyle/issues/13429 private static final Set MODULES_EXAMPLES_TO_SKIP = Set.of( - "WhitespaceAfter" + "WhitespaceAfter", + "InnerTypeLast" ); private static final List> CHECK_PROPERTIES = new ArrayList<>(); diff --git a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckExamplesTest.java new file mode 100644 index 00000000000..0fb8a905387 --- /dev/null +++ b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/design/InnerTypeLastCheckExamplesTest.java @@ -0,0 +1,47 @@ +/////////////////////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code and other text files for adherence to a set of rules. +// Copyright (C) 2001-2023 the original author or authors. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +/////////////////////////////////////////////////////////////////////////////////////////////// + +package com.puppycrawl.tools.checkstyle.checks.design; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport; + +@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345") +public class InnerTypeLastCheckExamplesTest extends AbstractModuleTestSupport { + @Override + protected String getResourceLocation() { + return "xdocs-examples"; + } + + @Override + protected String getPackageLocation() { + return "com/puppycrawl/tools/checkstyle/checks/design/innertypelast"; + } + + @Test + public void testExample1() throws Exception { + final String[] expected = { + + }; + + verifyWithInlineConfigParser(getPath("Example1.txt"), expected); + } +} diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/design/innertypelast/Example1.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/design/innertypelast/Example1.txt new file mode 100644 index 00000000000..0a3d564fa69 --- /dev/null +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/design/innertypelast/Example1.txt @@ -0,0 +1,27 @@ +/*xml + + + + + +*/ + +// xdoc section -- start +class Test { + private String s; // OK + class InnerTest1 {} + public void test() {} // violation, method should be declared before inner types. +} + +class Test2 { + static {}; // OK + class InnerTest1 {} + public Test2() {} // violation, constructor should be declared before inner types. +} + +class Test3 { + private String s; // OK + public void test() {} // OK + class InnerTest1 {} +} +// xdoc section -- end diff --git a/src/xdocs/checks/design/innertypelast.xml b/src/xdocs/checks/design/innertypelast.xml index 303e2890ad2..80ef9c03f32 100644 --- a/src/xdocs/checks/design/innertypelast.xml +++ b/src/xdocs/checks/design/innertypelast.xml @@ -17,13 +17,17 @@ -

+

To configure the check:

-<module name="InnerTypeLast"/> +<module name="Checker"> + <module name="TreeWalker"> + <module name="InnerTypeLast"/> + </module> +</module> -

Example:

+

Example:

class Test { private String s; // OK diff --git a/src/xdocs/checks/design/innertypelast.xml.template b/src/xdocs/checks/design/innertypelast.xml.template new file mode 100644 index 00000000000..36f826479c9 --- /dev/null +++ b/src/xdocs/checks/design/innertypelast.xml.template @@ -0,0 +1,72 @@ + + + + InnerTypeLast + + +
+

Since Checkstyle 5.2

+ +

+ Checks nested (internal) classes/interfaces are declared at the bottom of the + primary (top-level) class after all init and static init blocks, + method, constructor and field declarations. +

+
+ + +

+ To configure the check: +

+ + + + +

Example:

+ + + + +
+ + + + + + + +

+ All messages can be customized if the default message doesn't suit you. + Please see the documentation + to learn how to. +

+
+ + +

+ com.puppycrawl.tools.checkstyle.checks.design +

+
+ + +

+ TreeWalker +

+
+
+ +