Skip to content

Commit

Permalink
Add new tests for CharacterReader (#1931)
Browse files Browse the repository at this point in the history
* Update test to verify invalid unconsumption

* Add test to verify invalid rewinding

* Add test for digit matching

* Update matchesAny test for fully consumed buffer
  • Loading branch information
Deee92 authored Apr 4, 2023
1 parent e92c363 commit 907d09e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/test/java/org/jsoup/parser/CharacterReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jsoup.parser;

import org.jsoup.UncheckedIOException;
import org.jsoup.integration.ParseTest;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -61,6 +62,12 @@ public class CharacterReaderTest {

assertEquals(CharacterReader.EOF, r.consume());
assertTrue(r.isEmpty());

// unconsume all remaining characters
for (int i = 0; i < 4; i++) {
r.unconsume();
}
assertThrows(UncheckedIOException.class, r::unconsume);
}

@Test public void mark() {
Expand All @@ -78,6 +85,12 @@ public class CharacterReaderTest {
assertEquals(2, r.pos());
}

@Test public void rewindToMark() {
CharacterReader r = new CharacterReader("nothing");
// marking should be invalid
assertThrows(UncheckedIOException.class, r::rewindToMark);
}

@Test public void consumeToEnd() {
String in = "one two three";
CharacterReader r = new CharacterReader(in);
Expand Down Expand Up @@ -265,6 +278,20 @@ static String BufferBuster(String content) {
assertTrue(r.matchesAny(scan));
assertEquals('\n', r.consume());
assertFalse(r.matchesAny(scan));
// nothing to match
r.consumeToEnd();
assertTrue(r.isEmpty());
assertFalse(r.matchesAny(scan));
}

@Test public void matchesDigit() {
CharacterReader r = new CharacterReader("42");
r.consumeToEnd();
assertTrue(r.isEmpty());
// nothing to match
assertFalse(r.matchesDigit());
r.unconsume();
assertTrue(r.matchesDigit());
}

@Test public void cachesStrings() {
Expand Down

0 comments on commit 907d09e

Please sign in to comment.