Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created January 7, 2016 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/6a956467679815dcfe44 to your computer and use it in GitHub Desktop.
Save zoffixznet/6a956467679815dcfe44 to your computer and use it in GitHub Desktop.
unit module Test::Output;
use Test;
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;
}
}
my sub capture (&code) {
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();
}
return {:out($bag.out), :err($bag.err), :all($bag.all)};
}
sub output-is (*@args) is export { test |<all is>, &?ROUTINE.name, |@args }
sub output-like (*@args) is export { test |<all like>, &?ROUTINE.name, |@args }
sub output-from (*@args) is export { test |<all from>, &?ROUTINE.name, |@args }
sub stdout-is (*@args) is export { test |<out is>, &?ROUTINE.name, |@args }
sub stdout-like (*@args) is export { test |<out like>, &?ROUTINE.name, |@args }
sub stdout-from (*@args) is export { test |<out from>, &?ROUTINE.name, |@args }
sub stderr-is (*@args) is export { test |<err is>, &?ROUTINE.name, |@args }
sub stderr-like (*@args) is export { test |<err like>, &?ROUTINE.name, |@args }
sub stderr-from (*@args) is export { test |<err from>, &?ROUTINE.name, |@args }
sub test (
Str:D $output-type where { any <all err out> },
Str:D $op-name where { any <is like from> },
Str:D $routine-name,
&code,
Str:D $expected,
Str $test-name? is copy
)
{
$test-name //= "$routine-name on line {callframe(4).line}";
if ( $op-name == 'from' ) {
return capture(&code){ $output-type };
}
else {
return &::($op-name)(
capture(&code){ $output-type },
$expected,
$test-name,
);
}
}
@zoffixznet
Copy link
Author

Error: $ perl6 t/01-stdout.t
Cannot convert string to number: base-10 number must begin with valid digits or '.' in '⏏is' (indicated by ⏏)
in sub test at /home/zoffix/Desktop/CPANPRC/perl6-Test-Output/lib/Test/Output.pm6 line 61
in sub output-is at /home/zoffix/Desktop/CPANPRC/perl6-Test-Output/lib/Test/Output.pm6 line 41
in block at t/01-stdout.t line 7

Actually thrown at:
in sub test at /home/zoffix/Desktop/CPANPRC/perl6-Test-Output/lib/Test/Output.pm6 line 61
in sub output-is at /home/zoffix/Desktop/CPANPRC/perl6-Test-Output/lib/Test/Output.pm6 line 41
in block at t/01-stdout.t line 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment