diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/Chars.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/Chars.java index d55562b822..664a58330c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/Chars.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/document/Chars.java @@ -346,21 +346,20 @@ public final class Chars implements CharSequence { } /** - * Returns the substring starting at the given offset and with the - * given length. This differs from {@link String#substring(int, int)} - * in that it uses offset + length instead of start + end. + * Returns the substring between the given offsets. + * given length. * - * @param off Start offset ({@code 0 <= off < this.length()}) - * @param len Length of the substring ({@code 0 <= len <= this.length() - off}) + * @param start Start offset ({@code 0 <= start < this.length()}) + * @param end End offset ({@code start <= end <= this.length()}) * * @return A substring * * @throws IndexOutOfBoundsException If the parameters are not a valid range + * @see String#substring(int, int) */ - public String substring(int off, int len) { - validateRange(off, len, this.len); - int start = idx(off); - return str.substring(start, start + len); + public String substring(int start, int end) { + validateRange(start, end - start, this.len); + return str.substring(idx(start), idx(end)); } private static void validateRangeWithAssert(int off, int len, int bound) { diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleContextTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleContextTest.java index 835d227dbb..a24170661a 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleContextTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleContextTest.java @@ -9,6 +9,7 @@ import java.util.function.BiConsumer; import org.junit.Assert; import org.junit.Test; +import net.sourceforge.pmd.lang.ast.DummyNode.DummyRootNode; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.RootNode; import net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil; @@ -49,7 +50,10 @@ public class RuleContextTest { } private RuleViolation makeViolation(String unescapedMessage, Object... args) throws Exception { - Report report = getReport(new FooRule(), (r, ctx) -> ctx.addViolationWithMessage(DummyTreeUtil.tree(DummyTreeUtil::root), unescapedMessage, args)); + Report report = getReport(new FooRule(), (r, ctx) -> { + DummyRootNode node = DummyTreeUtil.tree(DummyTreeUtil::root); + ctx.addViolationWithMessage(node, unescapedMessage, args); + }); return report.getViolations().get(0); } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java index 6e2ae33f8f..ba7970fca1 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/ant/PMDTaskTest.java @@ -110,12 +110,6 @@ public class PMDTaskTest { expected = expected.replaceFirst("timestamp=\"[^\"]+\"", "timestamp=\"\""); expected = expected.replaceFirst("\\.xsd\" version=\"[^\"]+\"", ".xsd\" version=\"\""); - // under windows, the file source sample.dummy has different line endings - // and therefore the endcolumn of the nodes also change - if (System.lineSeparator().equals("\r\n")) { - expected = expected.replaceFirst("endcolumn=\"109\"", "endcolumn=\"110\""); - } - Assert.assertEquals(expected, actual); } } diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/DummyNode.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/DummyNode.java index 92d3aa8740..ed94e8ccc2 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/DummyNode.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/ast/DummyNode.java @@ -5,7 +5,6 @@ package net.sourceforge.pmd.lang.ast; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -154,28 +153,39 @@ public class DummyNode extends AbstractNode implements Gen public static class DummyRootNode extends DummyNode implements RootNode { - private Map suppressMap = Collections.emptyMap(); - private TextDocument sourceText = TextDocument.readOnlyString( - "dummy text", - TextFile.UNKNOWN_FILENAME, - DummyLanguageModule.getInstance().getDefaultVersion() - ); - private AstInfo astInfo; + public DummyRootNode() { + TextDocument document = TextDocument.readOnlyString( + "dummy text", + TextFile.UNKNOWN_FILENAME, + DummyLanguageModule.getInstance().getDefaultVersion() + ); + astInfo = new AstInfo<>( + new ParserTask( + document, + SemanticErrorReporter.noop() + ), + this); + } + public DummyRootNode withTaskInfo(ParserTask task) { this.astInfo = new AstInfo<>(task, this); return this; } public DummyRootNode withNoPmdComments(Map suppressMap) { - this.suppressMap = suppressMap; + this.astInfo = new AstInfo<>( + astInfo.getTextDocument(), + this, + suppressMap + ); return this; } @Override public AstInfo getAstInfo() { - return Objects.requireNonNull(astInfo, "no ast info, don't use DummyRootNode's ctor directly"); + return Objects.requireNonNull(astInfo, "no ast info"); } @Override diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/CharsTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/CharsTest.java index 762bf68b2a..e872f0e288 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/CharsTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/CharsTest.java @@ -77,7 +77,7 @@ public class CharsTest { Chars bc = Chars.wrap("abcd").slice(1, 2); bc.getChars(0, arr, 1, 2); - assertArrayEquals(arr, new char[] {0, 'b', 'c', 0}); + assertArrayEquals(arr, new char[] { 0, 'b', 'c', 0 }); assertThrows(IndexOutOfBoundsException.class, () -> bc.getChars(2, arr, 0, 1)); assertThrows(IndexOutOfBoundsException.class, () -> bc.getChars(-1, arr, 0, 1)); @@ -232,4 +232,34 @@ public class CharsTest { assertTrue(chars.contentEquals(Chars.wrap("A_B_C"), true)); } + @Test + public void testSlice() { + // slice is offset + length + Chars chars = Chars.wrap("a_a_b_c_s").slice(2, 5); + // ----- + assertEquals(Chars.wrap("_b_"), chars.slice(1, 3)); + assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, 6)); + } + + @Test + public void testSubsequence() { + // subsequence is start + end + Chars chars = Chars.wrap("a_a_b_c_s").slice(2, 5); + // ----- + assertEquals(Chars.wrap("_b"), chars.subSequence(1, 3)); + assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> chars.slice(0, 6)); + } + + @Test + public void testSubstring() { + // substring is start + end + Chars chars = Chars.wrap("a_a_b_c_s").slice(2, 5); + // ----- + assertEquals("_b", chars.substring(1, 3)); + assertThrows(IndexOutOfBoundsException.class, () -> chars.substring(0, -1)); + assertThrows(IndexOutOfBoundsException.class, () -> chars.substring(0, 6)); + } + } diff --git a/pmd-core/src/test/resources/net/sourceforge/pmd/ant/xml/expected-pmd-ant-xml.xml b/pmd-core/src/test/resources/net/sourceforge/pmd/ant/xml/expected-pmd-ant-xml.xml index 0bfe352391..081d871709 100644 --- a/pmd-core/src/test/resources/net/sourceforge/pmd/ant/xml/expected-pmd-ant-xml.xml +++ b/pmd-core/src/test/resources/net/sourceforge/pmd/ant/xml/expected-pmd-ant-xml.xml @@ -1,7 +1,7 @@ - + Test Rule 2