Skip to content

Commit

Permalink
Add fixture methods that strip leading/trailing whitespace (#408)
Browse files Browse the repository at this point in the history
* Add fixture methods that strip leading/trailing whitespace

Closes #407

* Add tests of the Charset overloaded methods
  • Loading branch information
sleberknight authored Aug 1, 2023
1 parent 1760e3d commit bb6bfb1
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 1 deletion.
81 changes: 81 additions & 0 deletions src/main/java/org/kiwiproject/test/util/Fixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,87 @@ public static String fixture(String resourceName, Charset charset) {
}
}

/**
* Reads the given fixture file from the classpath (e.g. {@code src/test/resources})
* and returns its contents with leading and trailing whitespace removed.
*
* @param resourceName the name/path of to the classpath resource
* @return the stripped fixture contents
* @throws UncheckedURISyntaxException if the resource name/path is invalid as a URI
* @throws UncheckedIOException if an I/O error occurs; see the implNote in {@link #fixture(String, Charset)}
*/
public static String fixtureStripLeadingAndTrailingWhitespace(String resourceName) {
return fixture(resourceName).strip();
}

/**
* Reads the given fixture file from the classpath (e.g. {@code src/test/resources})
* and returns its contents with leading and trailing whitespace removed.
*
* @param resourceName the name/path of to the classpath resource
* @param charset the charset of the fixture file
* @return the stripped fixture contents
* @throws UncheckedURISyntaxException if the resource name/path is invalid as a URI
* @throws UncheckedIOException if an I/O error occurs; see the implNote in {@link #fixture(String, Charset)}
*/
public static String fixtureStripLeadingAndTrailingWhitespace(String resourceName, Charset charset) {
return fixture(resourceName, charset).strip();
}

/**
* Reads the given fixture file from the classpath (e.g. {@code src/test/resources})
* and returns its contents with leading whitespace removed.
*
* @param resourceName the name/path of to the classpath resource
* @return the stripped fixture contents
* @throws UncheckedURISyntaxException if the resource name/path is invalid as a URI
* @throws UncheckedIOException if an I/O error occurs; see the implNote in {@link #fixture(String, Charset)}
*/
public static String fixtureStripLeadingWhitespace(String resourceName) {
return fixture(resourceName).stripLeading();
}

/**
* Reads the given fixture file from the classpath (e.g. {@code src/test/resources})
* and returns its contents with leading whitespace removed.
*
* @param resourceName the name/path of to the classpath resource
* @param charset the charset of the fixture file
* @return the stripped fixture contents
* @throws UncheckedURISyntaxException if the resource name/path is invalid as a URI
* @throws UncheckedIOException if an I/O error occurs; see the implNote in {@link #fixture(String, Charset)}
*/
public static String fixtureStripLeadingWhitespace(String resourceName, Charset charset) {
return fixture(resourceName, charset).stripLeading();
}

/**
* Reads the given fixture file from the classpath (e.g. {@code src/test/resources})
* and returns its contents with trailing whitespace removed.
*
* @param resourceName the name/path of to the classpath resource
* @return the stripped fixture contents
* @throws UncheckedURISyntaxException if the resource name/path is invalid as a URI
* @throws UncheckedIOException if an I/O error occurs; see the implNote in {@link #fixture(String, Charset)}
*/
public static String fixtureStripTrailingWhitespace(String resourceName) {
return fixture(resourceName).stripTrailing();
}

/**
* Reads the given fixture file from the classpath (e.g. {@code src/test/resources})
* and returns its contents with leading and trailing whitespace removed.
*
* @param resourceName the name/path of to the classpath resource
* @param charset the charset of the fixture file
* @return the stripped fixture contents
* @throws UncheckedURISyntaxException if the resource name/path is invalid as a URI
* @throws UncheckedIOException if an I/O error occurs; see the implNote in {@link #fixture(String, Charset)}
*/
public static String fixtureStripTrailingWhitespace(String resourceName, Charset charset) {
return fixture(resourceName, charset).stripTrailing();
}

/**
* This method only exists to permit testing it when building on JDK 11-16, since those throw UncheckedIOException
* whereas JDK 17 throws an Error (see above implNote in {@link #fixture(String, Charset)}).
Expand Down
77 changes: 76 additions & 1 deletion src/test/java/org/kiwiproject/test/util/FixturesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnmappableCharacterException;
import java.nio.file.Path;

@DisplayName("Fixtures")
class FixturesTest {

private static final String PANGRAM_FIXTURE = "FixturesTest/pangram.txt";
private static final String FIXTURES_TEST_DIRECTORY = "FixturesTest";

private static final String PANGRAM_FIXTURE =
Path.of(FIXTURES_TEST_DIRECTORY, "pangram.txt").toString();

private static final String PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE =
Path.of(FIXTURES_TEST_DIRECTORY, "pangram-leading-trailing-whitespace.txt").toString();

@Nested
class Fixture {
Expand Down Expand Up @@ -98,6 +105,74 @@ void shouldReturnUncheckedIOException_WhenErrorCause_IsUnmappableCharacterExcept
}
}

@Nested
class FixtureStripLeadingAndTrailingWhitespace {

@Test
void shouldStripLeadingAndTrailingWhitespace() {
var fixture = Fixtures.fixtureStripLeadingAndTrailingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE).strip();
assertThat(fixture).isEqualTo(expected);
}

@Test
void shouldStripLeadingAndTrailingWhitespaceWithCharset() {
var fixture = Fixtures.fixtureStripLeadingAndTrailingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE, StandardCharsets.UTF_8);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE, StandardCharsets.UTF_8).strip();
assertThat(fixture).isEqualTo(expected);
}
}

@Nested
class FixtureStripLeadingWhitespace {

@Test
void shouldStripLeadingWhitespace() {
var fixture = Fixtures.fixtureStripLeadingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE).stripLeading();
assertThat(fixture).isEqualTo(expected);
}

@Test
void shouldStripLeadingWhitespaceWithCharset() {
var fixture = Fixtures.fixtureStripLeadingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE, StandardCharsets.UTF_8);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE, StandardCharsets.UTF_8).stripLeading();
assertThat(fixture).isEqualTo(expected);
}

@Test
void shouldNotStripTrailingWhitespace() {
var fixture = Fixtures.fixtureStripLeadingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE).stripLeading();
assertThat(fixture).isEqualTo(expected);
}
}

@Nested
class FixtureStripTrailingWhitespace {

@Test
void shouldStripTrailingWhitespace() {
var fixture = Fixtures.fixtureStripTrailingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE).stripTrailing();
assertThat(fixture).isEqualTo(expected);
}

@Test
void shouldStripTrailingWhitespaceWithCharset() {
var fixture = Fixtures.fixtureStripTrailingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE, StandardCharsets.UTF_8);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE, StandardCharsets.UTF_8).stripTrailing();
assertThat(fixture).isEqualTo(expected);
}

@Test
void shouldNotStripLeadingWhitespace() {
var fixture = Fixtures.fixtureStripTrailingWhitespace(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE);
var expected = Fixtures.fixture(PANGRAM_LEADING_TRAILING_WHITESPACE_FIXTURE).stripTrailing();
assertThat(fixture).isEqualTo(expected);
}
}

@Nested
class FixtureFile {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


The quick brown fox jumps over the lazy dog

0 comments on commit bb6bfb1

Please sign in to comment.