Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created January 7, 2016 19:10
Show Gist options
  • Save zoffixznet/8f9675bc3fb3cf8ef0c7 to your computer and use it in GitHub Desktop.
Save zoffixznet/8f9675bc3fb3cf8ef0c7 to your computer and use it in GitHub Desktop.
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