From 943163d61d58e5e69b014dec2e6750307daa04e8 Mon Sep 17 00:00:00 2001 From: Amit Kumar Deohoria Date: Fri, 15 Nov 2024 13:41:37 +0530 Subject: [PATCH] Issue #13345: Enable examples tests for RecordComponentNameCheck --- .../RecordComponentNameCheckExamplesTest.java | 21 ++++++++++++----- .../naming/recordcomponentname/Example1.java | 19 +++++++++++++++ .../naming/recordcomponentname/Example2.java | 21 +++++++++++++++++ .../naming/recordcomponentname/Example1.txt | 15 ------------ .../naming/recordcomponentname/Example2.txt | 16 ------------- .../checks/naming/recordcomponentname.xml | 23 +++++++++++-------- .../naming/recordcomponentname.xml.template | 8 +++---- 7 files changed, 73 insertions(+), 50 deletions(-) create mode 100644 src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.java create mode 100644 src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.java delete mode 100644 src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.txt delete mode 100644 src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.txt diff --git a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/naming/RecordComponentNameCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/naming/RecordComponentNameCheckExamplesTest.java index 08d070c84d8..a7bee32de09 100644 --- a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/naming/RecordComponentNameCheckExamplesTest.java +++ b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/naming/RecordComponentNameCheckExamplesTest.java @@ -19,12 +19,12 @@ package com.puppycrawl.tools.checkstyle.checks.naming; -import org.junit.jupiter.api.Disabled; +import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN; + import org.junit.jupiter.api.Test; import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport; -@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345") public class RecordComponentNameCheckExamplesTest extends AbstractExamplesModuleTestSupport { @Override protected String getPackageLocation() { @@ -33,19 +33,28 @@ protected String getPackageLocation() { @Test public void testExample1() throws Exception { - final String[] expected = { + final String pattern = "^[a-z][a-zA-Z0-9]*$"; + + final String[] expected = { + "15:22: " + getCheckMessage(MSG_INVALID_PATTERN, "Values", pattern), }; - verifyWithInlineConfigParser(getPath("Example1.txt"), expected); + verifyWithInlineConfigParser( + getNonCompilablePath("Example1.java"), expected); } @Test public void testExample2() throws Exception { - final String[] expected = { + final String pattern = "^[a-z]+$"; + + final String[] expected = { + "17:22: " + getCheckMessage(MSG_INVALID_PATTERN, "Values", pattern), + "19:22: " + getCheckMessage(MSG_INVALID_PATTERN, "myNumber", pattern), }; - verifyWithInlineConfigParser(getPath("Example2.txt"), expected); + verifyWithInlineConfigParser( + getNonCompilablePath("Example2.java"), expected); } } diff --git a/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.java b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.java new file mode 100644 index 00000000000..ab0e9b48420 --- /dev/null +++ b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.java @@ -0,0 +1,19 @@ +/*xml + + + + + +*/ +//non-compiled with javac: Compilable with Java17 +package com.puppycrawl.tools.checkstyle.checks.naming.recordcomponentname; + +// xdoc section -- start +class Example1 { + record Rec1(int other) {} + + record Rec2(String Values) {} // violation, Name must match '^[a-z][a-zA-Z0-9]*$' + + record Rec3(double myNumber) {} +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.java b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.java new file mode 100644 index 00000000000..f40dde9718b --- /dev/null +++ b/src/xdocs-examples/resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.java @@ -0,0 +1,21 @@ +/*xml + + + + + + + +*/ +//non-compiled with javac: Compilable with Java17 +package com.puppycrawl.tools.checkstyle.checks.naming.recordcomponentname; + +// xdoc section -- start +class Example2 { + record Rec1(int other) {} + + record Rec2(String Values) {} // violation, Name must match '^[a-z]+$' + + record Rec3(double myNumber) {} // violation, Name must match '^[a-z]+$' +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.txt deleted file mode 100644 index 01b38c75fd5..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.txt +++ /dev/null @@ -1,15 +0,0 @@ -/*xml - - - - - -*/ - -// xdoc section -- start -record MyRecord1(String value, int otherComponentName) {} // OK -record MyRecord2(String... Values) {} // violation, the record component name - // should match the regular expression "^[a-z][a-zA-Z0-9]*$" -record MyRecord3(double my_number) {} // violation, the record component name - // should match the regular expression "^[a-z][a-zA-Z0-9]*$" -// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.txt deleted file mode 100644 index 4e3b3ae5e4e..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.txt +++ /dev/null @@ -1,16 +0,0 @@ -/*xml - - - - - - - -*/ - -// xdoc section -- start -record MyRecord1(String value, int other) {} // OK -record MyRecord2(String... strings) {} // OK -record MyRecord3(double myNumber) {} // violation, the record component name - // should match the regular expression "^[a-z]+$" -// xdoc section -- end diff --git a/src/xdocs/checks/naming/recordcomponentname.xml b/src/xdocs/checks/naming/recordcomponentname.xml index 8d96964136e..5819ae274cf 100644 --- a/src/xdocs/checks/naming/recordcomponentname.xml +++ b/src/xdocs/checks/naming/recordcomponentname.xml @@ -46,11 +46,13 @@

Example:

-record MyRecord1(String value, int otherComponentName) {} // OK -record MyRecord2(String... Values) {} // violation, the record component name - // should match the regular expression "^[a-z][a-zA-Z0-9]*$" -record MyRecord3(double my_number) {} // violation, the record component name - // should match the regular expression "^[a-z][a-zA-Z0-9]*$" +class Example1 { + record Rec1(int other) {} + + record Rec2(String Values) {} // violation, Name must match '^[a-z][a-zA-Z0-9]*$' + + record Rec3(double myNumber) {} +}

An example of how to configure the check for names that are only letters in lowercase: @@ -67,10 +69,13 @@ record MyRecord3(double my_number) {} // violation, the record component name

Example:

-record MyRecord1(String value, int other) {} // OK -record MyRecord2(String... strings) {} // OK -record MyRecord3(double myNumber) {} // violation, the record component name - // should match the regular expression "^[a-z]+$" +class Example2 { + record Rec1(int other) {} + + record Rec2(String Values) {} // violation, Name must match '^[a-z]+$' + + record Rec3(double myNumber) {} // violation, Name must match '^[a-z]+$' +} diff --git a/src/xdocs/checks/naming/recordcomponentname.xml.template b/src/xdocs/checks/naming/recordcomponentname.xml.template index cb657ef0422..ba50feb97b9 100644 --- a/src/xdocs/checks/naming/recordcomponentname.xml.template +++ b/src/xdocs/checks/naming/recordcomponentname.xml.template @@ -27,13 +27,13 @@

+ value="resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.java"/>

Example:

+ value="resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example1.java"/>

@@ -42,13 +42,13 @@

Configuration:

+ value="resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.java"/>

Example:

+ value="resources-noncompilable/com/puppycrawl/tools/checkstyle/checks/naming/recordcomponentname/Example2.java"/>