/perl.p6 Secret
Created
January 7, 2016 19:10
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
unit module Test::Output; | |
my class IO::Bag { | |
has @.err-contents; | |
has @.out-contents; | |
has @.all-contents; | |
method err { @.err-contents.join: '' } | |
method out { @.out-contents.join: '' } | |
method all { @.all-contents.join: '' } | |
} | |
my class IO::Capture::Single is IO::Handle { | |
has Bool $.is-err = False ; | |
has IO::Bag $.bag is required; | |
method print-nl { self.print($.nl-out); } | |
method print (*@what) { | |
$.bag.all-contents.push: @what.join: ''; | |
$!is-err ?? $.bag.err-contents.push: @what.join: '' | |
!! $.bag.out-contents.push: @what.join: ''; | |
True; | |
} | |
} | |
sub output-is (&code, Regex $expected, $test-name = 'output matches' ) | |
is export | |
{ | |
my $bag = IO::Bag.new; | |
my $out = IO::Capture::Single.new: :$bag ; | |
my $err = IO::Capture::Single.new: :$bag :is-err; | |
{ | |
my $*OUT = $out; | |
my $*ERR = $err; | |
&code(); | |
} | |
say "STDOUT is {$bag.out}"; | |
say "STDERR is {$bag.err}"; | |
say "MERGED is {$bag.all}"; | |
return :out(~$bag.out), :err(~$bag.err), :all($bag.all); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment