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 com.github.stefanbirkner system-lambda - 1.1.1 + 1.2.0 ``` @@ -113,8 +113,9 @@ void execute_code_that_manipulates_system_properties( Command-line applications usually write to the console. If you write such applications you need to test the output of these applications. The methods -`tapSystemErr`, `tapSystemErrNormalized`, `tapSystemOut` and -`tapSystemOutNormalized` allow you to tap the text that is written to +`tapSystemErr`, `tapSystemErrNormalized`, `tapSystemOut`, +`tapSystemOutNormalized`, `tapSystemErrAndOut` and +`tapSystemErrAndOutNormalized` allow you to tap the text that is written to `System.err`/`System.out`. The methods with the suffix `Normalized` normalize line breaks to `\n` so that you can run tests with the same assertions on different operating systems. @@ -157,6 +158,26 @@ void application_writes_mutliple_lines_to_System_out( }); assertEquals("first line\nsecond line\n", text); } + +@Test +void application_writes_text_to_System_err_and_out( +) throws Exception { + String text = tapSystemErrAndOut(() -> { + System.err.print("text from err"); + System.out.print("text from out"); + }); + assertEquals("text from errtext from out", text); +} + +@Test +void application_writes_mutliple_lines_to_System_err_and_out( +) throws Exception { + String text = tapSystemErrAndOutNormalized(() -> { + System.err.println("text from err"); + System.out.println("text from out"); + }); + assertEquals("text from err\ntext from out\n", text); +} ``` You can assert that nothing is written to `System.err`/`System.out` by wrapping diff --git a/pom.xml b/pom.xml index fb5e156..d965d3f 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ system-lambda - 1.2.0-SNAPSHOT + 1.2.0 jar System Lambda diff --git a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java index f6c28c8..f20cde1 100644 --- a/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java +++ b/src/main/java/com/github/stefanbirkner/systemlambda/SystemLambda.java @@ -87,8 +87,11 @@ * applications you need to test the output of these applications. The methods * {@link #tapSystemErr(Statement) tapSystemErr}, * {@link #tapSystemErrNormalized(Statement) tapSystemErrNormalized}, - * {@link #tapSystemOut(Statement) tapSystemOut} and - * {@link #tapSystemOutNormalized(Statement) tapSystemOutNormalized} allow you + * {@link #tapSystemOut(Statement) tapSystemOut}, + * {@link #tapSystemOutNormalized(Statement) tapSystemOutNormalized}, + * {@link #tapSystemErrAndOut(Statement) tapSystemErrAndOut} and + * {@link #tapSystemErrAndOutNormalized(Statement) tapSystemErrAndOutNormalized} + * allow you * to tap the text that is written to {@code System.err}/{@code System.out}. The * methods with the suffix {@code Normalized} normalize line breaks to * {@code \n} so that you can run tests with the same assertions on different @@ -131,6 +134,26 @@ * System.out.println("second line"); * }); * assertEquals(text, "first line\nsecond line\n"); + * } + * + * @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); + * } + * + * @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); * } * *

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); + } + } +}