-
-
Save zoffixznet/40062b484b67096ac0d8 to your computer and use it in GitHub Desktop.
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
use Test; | |
class IO::Captured is IO::Handle { | |
has @.w; | |
method say (*@w) { | |
@.w.push: @w.join: "" | |
}; | |
method Str {@.w.join: ""} | |
}; | |
sub output-capture (&code) { | |
my $capture = IO::Captured.new; | |
my $saved-out = $PROCESS::OUT; | |
my $saved-err = $PROCESS::ERR; | |
$PROCESS::OUT = $capture; | |
$PROCESS::ERR = $capture; | |
&code(); | |
$PROCESS::OUT = $saved-out; | |
$PROCESS::ERR = $saved-err; | |
return $capture.Str; | |
}; | |
#is 1, 1; | |
my $x = output-capture { EVAL "use Test; is 1, 2; done-testing" }; | |
say "Captured [[$x]]"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment