diff --git a/README.md b/README.md
index 71cca2d..c3f2141 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@ System Lambda is available from
You can assert that nothing is written to @@ -509,6 +532,8 @@ public static void restoreSystemProperties( * @return text that is written to {@code System.err} by the statement. * @throws Exception any exception thrown by the statement. * @see #tapSystemOut(Statement) + * @see #tapSystemErrAndOut(Statement) + * @see #tapSystemErrAndOutNormalized(Statement) * @since 1.0.0 */ public static String tapSystemErr( @@ -541,6 +566,8 @@ public static String tapSystemErr( * @return text that is written to {@code System.err} by the statement. * @throws Exception any exception thrown by the statement. * @see #tapSystemOut(Statement) + * @see #tapSystemErrAndOut(Statement) + * @see #tapSystemErrAndOutNormalized(Statement) * @since 1.0.0 */ public static String tapSystemErrNormalized( @@ -550,6 +577,80 @@ public static String tapSystemErrNormalized( .replace(lineSeparator(), "\n"); } + /** + * Executes the statement and returns the text that was written to + * {@code System.err} and {@code System.out} by the statement. + *
+ * @Test + * void application_writes_text_to_System_err_and_out( + * ) throws Exception { + * String text = tapSystemErrAndOut((){@literal ->} { + * System.err.print("text from err"); + * System.out.print("text from out"); + * }); + * assertEquals("text from errtext from out", text); + * } + *+ * + * @param statement an arbitrary piece of code. + * @return text that is written to {@code System.err} and {@code System.out} + * by the statement. + * @throws Exception any exception thrown by the statement. + * @see #tapSystemErrAndOutNormalized(Statement) + * @see #tapSystemErr(Statement) + * @see #tapSystemErrNormalized(Statement) + * @see #tapSystemOut(Statement) + * @see #tapSystemOutNormalized(Statement) + * @since 1.2.0 + */ + public static String tapSystemErrAndOut( + Statement statement + ) throws Exception { + TapStream tapStream = new TapStream(); + executeWithSystemErrReplacement( + tapStream, + () -> executeWithSystemOutReplacement( + tapStream, + statement + ) + ); + return tapStream.textThatWasWritten(); + } + + /** + * Executes the statement and returns the text that was written to + * {@code System.err} and {@code System.out} by the statement. New line + * characters are replaced with a single {@code \n}. + *
+ * @Test + * void application_writes_mutliple_lines_to_System_err_and_out( + * ) throws Exception { + * String text = tapSystemErrAndOutNormalized((){@literal ->} { + * System.err.println("text from err"); + * System.out.println("text from out"); + * }); + * assertEquals("text from err\ntext from out\n", text); + * } + *+ * + * @param statement an arbitrary piece of code. + * @return text that is written to {@code System.err} and {@code System.out} + * by the statement. + * @throws Exception any exception thrown by the statement. + * @see #tapSystemErrAndOut(Statement) + * @see #tapSystemErr(Statement) + * @see #tapSystemErrNormalized(Statement) + * @see #tapSystemOut(Statement) + * @see #tapSystemOutNormalized(Statement) + * @since 1.2.0 + */ + public static String tapSystemErrAndOutNormalized( + Statement statement + ) throws Exception { + return tapSystemErrAndOut(statement) + .replace(lineSeparator(), "\n"); + } + /** * Executes the statement and returns the text that was written to * {@code System.out} by the statement. @@ -568,6 +669,8 @@ public static String tapSystemErrNormalized( * @return text that is written to {@code System.out} by the statement. * @throws Exception any exception thrown by the statement. * @see #tapSystemErr(Statement) + * @see #tapSystemErrAndOut(Statement) + * @see #tapSystemErrAndOutNormalized(Statement) * @since 1.0.0 */ public static String tapSystemOut( @@ -600,6 +703,8 @@ public static String tapSystemOut( * @return text that is written to {@code System.out} by the statement. * @throws Exception any exception thrown by the statement. * @see #tapSystemErr(Statement) + * @see #tapSystemErrAndOut(Statement) + * @see #tapSystemErrAndOutNormalized(Statement) * @since 1.0.0 */ public static String tapSystemOutNormalized( diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutNormalizedTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutNormalizedTest.java new file mode 100644 index 0000000..e7976c2 --- /dev/null +++ b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutNormalizedTest.java @@ -0,0 +1,60 @@ +package com.github.stefanbirkner.systemlambda; + +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.*; +import static java.lang.System.err; +import static java.lang.System.out; +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayNameGeneration(ReplaceUnderscores.class) +class TapSystemErrAndOutNormalizedTest { + + @Test + void taps_text_that_is_written_to_System_err_and_out_by_statement_has_only_slash_n_for_new_line( + ) throws Exception { + String textWrittenToSystemErr = tapSystemErrAndOutNormalized( + () -> { + err.println("line 1"); + out.println("line 2"); + err.println("line 3"); + out.println("line 4"); + } + ); + + assertThat(textWrittenToSystemErr) + .isEqualTo("line 1\nline 2\nline 3\nline 4\n"); + } + + @Test + void tapped_text_is_empty_when_statement_does_not_write_to_System_err_nor_out( + ) throws Exception { + String textWrittenToSystemErr = tapSystemErrAndOutNormalized( + () -> {} + ); + + assertThat(textWrittenToSystemErr) + .isEqualTo(""); + } + + @Nested + class System_err_is_same_as_before + extends RestoreSystemErrChecks + { + System_err_is_same_as_before() { + super(SystemLambda::tapSystemErrAndOutNormalized); + } + } + + @Nested + class System_out_is_same_as_before + extends RestoreSystemOutChecks + { + System_out_is_same_as_before() { + super(SystemLambda::tapSystemErrAndOutNormalized); + } + } +} diff --git a/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutTest.java b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutTest.java new file mode 100644 index 0000000..0f3d589 --- /dev/null +++ b/src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutTest.java @@ -0,0 +1,61 @@ +package com.github.stefanbirkner.systemlambda; + +import org.junit.jupiter.api.DisplayNameGeneration; +import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErr; +import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemErrAndOut; +import static java.lang.System.err; +import static java.lang.System.out; +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayNameGeneration(ReplaceUnderscores.class) +class TapSystemErrAndOutTest { + + @Test + void taps_text_that_is_written_to_System_err_and_out_by_statement( + ) throws Exception { + String textWrittenToSystemErrAndOut = tapSystemErrAndOut( + () -> { + err.print("word1 "); + out.print("word2 "); + err.print("word3 "); + out.print("word4 "); + } + ); + + assertThat(textWrittenToSystemErrAndOut) + .isEqualTo("word1 word2 word3 word4 "); + } + + @Test + void tapped_text_is_empty_when_statement_does_not_write_to_System_err_nor_out( + ) throws Exception { + String textWrittenToSystemErrAndOut = tapSystemErrAndOut( + () -> {} + ); + + assertThat(textWrittenToSystemErrAndOut) + .isEqualTo(""); + } + + @Nested + class System_err_is_same_as_before + extends RestoreSystemErrChecks + { + System_err_is_same_as_before() { + super(SystemLambda::tapSystemErrAndOut); + } + } + + @Nested + class System_out_is_same_as_before + extends RestoreSystemOutChecks + { + System_out_is_same_as_before() { + super(SystemLambda::tapSystemErrAndOut); + } + } +}