diff --git a/src/main/java/org/kiwiproject/test/util/Fixtures.java b/src/main/java/org/kiwiproject/test/util/Fixtures.java index dc82f5d7..fd4dd324 100644 --- a/src/main/java/org/kiwiproject/test/util/Fixtures.java +++ b/src/main/java/org/kiwiproject/test/util/Fixtures.java @@ -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)}). diff --git a/src/test/java/org/kiwiproject/test/util/FixturesTest.java b/src/test/java/org/kiwiproject/test/util/FixturesTest.java index f2546d4a..6d48096f 100644 --- a/src/test/java/org/kiwiproject/test/util/FixturesTest.java +++ b/src/test/java/org/kiwiproject/test/util/FixturesTest.java @@ -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 { @@ -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 { diff --git a/src/test/resources/FixturesTest/pangram-leading-trailing-whitespace.txt b/src/test/resources/FixturesTest/pangram-leading-trailing-whitespace.txt new file mode 100644 index 00000000..f549eaaa --- /dev/null +++ b/src/test/resources/FixturesTest/pangram-leading-trailing-whitespace.txt @@ -0,0 +1,4 @@ + + + The quick brown fox jumps over the lazy dog +