More tests

This commit is contained in:
Clément Fournier committed 2022-04-24 16:33:00 +02:00
1 parent 32c8581f04
commit 2d2a1c5531
6 files changed
+127 -13

No files matched your search

@@ -22,7 +22,7 @@ public final class TextRange2d implements Comparable<TextRange2d> {
this.endLine = endLine;
this.endCol = endCol;
assert startCol >= 1 && startLine >= 1 && endLine >= 1 && endCol >= 1
: "Not a valid range " + displayString();
: "Not a valid range " + toDisplayStringWithColon();
}
@@ -34,9 +34,9 @@ public final class TextRange2d implements Comparable<TextRange2d> {
return TextPos2d.pos2d(endLine, endCol);
}
public String displayString() {
return "(" + startCol + ", " + endCol
+ ")-(" + endLine + ", " + endCol + ")";
public String toDisplayStringWithColon() {
return getStartPos().toDisplayStringWithColon() + "-"
+ getEndPos().toDisplayStringWithColon();
}
public int getStartLine() {
@@ -104,7 +104,8 @@ public final class TextRange2d implements Comparable<TextRange2d> {
@Override
public String toString() {
return "[" + getStartPos() + " - " + getEndPos() + ']';
return "!debug only! [" + getStartPos().toTupleString()
+ " - " + getEndPos().toTupleString() + ']';
}
}
@@ -50,7 +50,7 @@ public final class TextRegion implements Comparable<TextRegion> {
this.startOffset = startOffset;
this.length = length;
assert startOffset >= 0 && length >= 0 : "Invalid region" + parThis();
assert startOffset >= 0 && length >= 0 : "Invalid region " + this;
}
/** 0-based, inclusive index. */
@@ -122,8 +122,8 @@ public final class TextRegion implements Comparable<TextRegion> {
* @throws AssertionError If the parameter cannot produce a valid region
*/
public TextRegion growLeft(int delta) {
assert (delta + length) >= 0 : "Left delta " + delta + " would produce a negative length region" + parThis();
assert (startOffset - delta) >= 0 : "Left delta " + delta + " would produce a region that starts before zero" + parThis();
assert delta + length >= 0 : "Left delta " + delta + " would produce a negative length region " + parThis();
assert startOffset - delta >= 0 : "Left delta " + delta + " would produce a region that starts before zero " + parThis();
return new TextRegion(startOffset - delta, delta + length);
}
@@ -135,7 +135,7 @@ public final class TextRegion implements Comparable<TextRegion> {
* @throws AssertionError If the delta is negative and less than the length of this region
*/
public TextRegion growRight(int delta) {
assert (delta + length) >= 0 : "Right delta " + delta + " would produce a negative length region" + parThis();
assert delta + length >= 0 : "Right delta " + delta + " would produce a negative length region " + parThis();
return new TextRegion(startOffset, delta + length);
}
@@ -0,0 +1,44 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.document;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
/**
* @author Clément Fournier
*/
public class FileLocationTest {
@Test
public void testSimple() {
FileLocation loc = FileLocation.range("fname", TextRange2d.range2d(1, 1, 1, 2));
assertEquals("fname", loc.getFileName());
assertEquals(1, loc.getStartLine());
assertEquals(1, loc.getStartColumn());
assertEquals(1, loc.getEndLine());
assertEquals(2, loc.getEndColumn());
}
@Test
public void testToString() {
FileLocation loc = FileLocation.range("fname", TextRange2d.range2d(1, 1, 1, 2));
assertEquals(
"line 1, column 1",
loc.startPosToString()
);
assertEquals(
"fname:1:1",
loc.startPosToStringWithFile()
);
MatcherAssert.assertThat(loc.toString(), containsString("!debug only!"));
}
}
@@ -211,9 +211,7 @@ public class TextDocumentTest {
public void testRegionOutOfBounds() {
TextDocument doc = TextDocument.readOnlyString("bonjour\noha\ntristesse", dummyVersion);
expect.expect(AssertionError.class);
TextRegion.isValidRegion(0, 40, doc);
assertThrows(AssertionError.class, () -> TextRegion.isValidRegion(0, 40, doc));
}
// for junit params runner
@@ -228,7 +226,7 @@ public class TextDocumentTest {
TextDocument.readOnlyString("bonjour\n", dummyVersion),
TextDocument.readOnlyString("\n", dummyVersion),
TextDocument.readOnlyString("", dummyVersion),
};
};
}
}
@@ -0,0 +1,36 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.document;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
/**
* @author Clément Fournier
*/
public class TextPos2dTest {
@Test
public void testToString() {
TextPos2d pos = TextPos2d.pos2d(1, 2);
assertEquals(
"line 1, column 2",
pos.toDisplayStringInEnglish()
);
assertEquals(
"1:2",
pos.toDisplayStringWithColon()
);
assertEquals(
"(line=1, column=2)",
pos.toTupleString()
);
MatcherAssert.assertThat(pos.toString(), containsString("!debug only!"));
}
}
@@ -0,0 +1,35 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.document;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
/**
* @author Clément Fournier
*/
public class TextRange2dTest {
@Test
public void testCtors() {
TextRange2d pos = TextRange2d.range2d(1, 2, 3, 4);
TextRange2d pos2 = TextRange2d.range2d(TextPos2d.pos2d(1, 2), TextPos2d.pos2d(3, 4));
assertEquals(pos, pos2);
}
@Test
public void testToString() {
TextRange2d range = TextRange2d.range2d(1, 2, 3, 4);
assertEquals(
"1:2-3:4",
range.toDisplayStringWithColon()
);
MatcherAssert.assertThat(range.toString(), containsString("!debug only!"));
}
}