[test-schema] Fix trim indentation for test codes

Co-authored-by: Juan Martín Sotuyo Dodero <juansotuyo@gmail.com>
This commit is contained in:
Andreas Dangel 2024-03-05 19:10:20 +01:00
parent dffc44ecde
commit a8c539eddb
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
2 changed files with 34 additions and 6 deletions

View File

@ -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) {

View File

@ -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 {
+ " <expected-problems>4</expected-problems>\n"
+ " <code><![CDATA[\n"
+ " public class Foo {\n"
+ " private int i;\n"
+ " }\n"
+ " ]]></code>\n"
+ " </test-code>\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 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<test-data\n"
+ " xmlns=\"http://pmd.sourceforge.net/rule-tests\"\n"
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ " xsi:schemaLocation=\"http://pmd.sourceforge.net/rule-tests net/sourceforge/pmd/test/schema/rule-tests_1_0_0.xsd\">\n"
+ " <code-fragment id=\"code1\"><![CDATA[\n"
+ " public class Foo {\n"
+ " private int i;\n"
+ " }\n"
+ " ]]></code-fragment>\n"
+ " <test-code>\n"
+ " <description>equality operators with Double.NaN</description>\n"
+ " <expected-problems>4</expected-problems>\n"
+ " <code-ref id=\"code1\" />\n"
+ " </test-code>\n"
+ "</test-data>\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