-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods for tapping err and out simultaneously
The output of command-line applications is usually the output to the standard output stream and the error output stream intermixed. The new methods allow capturing the intermixed output. @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); }
- Loading branch information
1 parent
8903a1d
commit a203454
Showing
5 changed files
with
253 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutNormalizedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/test/java/com/github/stefanbirkner/systemlambda/TapSystemErrAndOutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |