From a8c539eddbc66c5f9d3b6cdd732483d3670861f7 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Tue, 5 Mar 2024 19:10:20 +0100 Subject: [PATCH] [test-schema] Fix trim indentation for test codes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Juan Martín Sotuyo Dodero --- .../pmd/test/schema/BaseTestParserImpl.java | 10 +++---- .../pmd/test/schema/TestSchemaParserTest.java | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pmd-test-schema/src/main/java/net/sourceforge/pmd/test/schema/BaseTestParserImpl.java b/pmd-test-schema/src/main/java/net/sourceforge/pmd/test/schema/BaseTestParserImpl.java index 0f26b41bf2..972956e17d 100644 --- a/pmd-test-schema/src/main/java/net/sourceforge/pmd/test/schema/BaseTestParserImpl.java +++ b/pmd-test-schema/src/main/java/net/sourceforge/pmd/test/schema/BaseTestParserImpl.java @@ -98,7 +98,7 @@ class BaseTestParserImpl { if (description == null) { return; } - descriptor.setDescription(description); + descriptor.setDescription(description.trim()); } parseBoolAttribute(testCode, "reinitializeRule", true, err, "Attribute 'reinitializeRule' is deprecated and ignored, assumed true"); @@ -206,11 +206,9 @@ class BaseTestParserImpl { } usedFragments.add(id.getValue()); code = parseTextNodeNoTrim(fragment); - // first trim empty lines at beginning/end - code = code.trim(); - // then trim any indentation - code = StringUtil.trimIndent(Chars.wrap(code)).toString(); } + // first trim empty lines at beginning/end, then trim any indentation + code = StringUtil.trimIndent(Chars.wrap(code).trimBlankLines()).toString(); return code; } @@ -296,7 +294,7 @@ class BaseTestParserImpl { if (node == null) { return null; } - return parseTextNode(node); + return parseTextNodeNoTrim(node); } private Element getSingleChild(Element parentElm, String nodeName, boolean required, PmdXmlReporter err) { diff --git a/pmd-test-schema/src/test/java/net/sourceforge/pmd/test/schema/TestSchemaParserTest.java b/pmd-test-schema/src/test/java/net/sourceforge/pmd/test/schema/TestSchemaParserTest.java index 73878474ae..abe7afcf3a 100644 --- a/pmd-test-schema/src/test/java/net/sourceforge/pmd/test/schema/TestSchemaParserTest.java +++ b/pmd-test-schema/src/test/java/net/sourceforge/pmd/test/schema/TestSchemaParserTest.java @@ -6,6 +6,7 @@ package net.sourceforge.pmd.test.schema; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -39,6 +40,7 @@ class TestSchemaParserTest { + " 4\n" + " \n" + " \n" @@ -55,6 +57,34 @@ class TestSchemaParserTest { RuleTestCollection parsed = parseFile(file); assertEquals(2, parsed.getTests().size()); + assertThat("Indentation should be removed", + parsed.getTests().get(0).getCode(), equalTo("public class Foo {\n private int i;\n}")); + } + + @Test + void testSharedCodeFragment() throws IOException { + String file = "\n" + + "\n" + + " \n" + + " \n" + + " equality operators with Double.NaN\n" + + " 4\n" + + " \n" + + " \n" + + "\n"; + + RuleTestCollection parsed = parseFile(file); + + assertEquals(1, parsed.getTests().size()); + assertThat("Indentation should be removed", + parsed.getTests().get(0).getCode(), equalTo("public class Foo {\n private int i;\n}")); } @Test