Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete this pull request. Made to wrong branch. #10

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.precomp
.idea
*.iml
.idea
2 changes: 1 addition & 1 deletion META6.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Test::Output",
"description": "Test the output to STDOUT and STDERR your program generates",
"version": "1.001003",
"version": "1.001004",
"perl": "6.d",
"authors": [
"Zoffix Znet",
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Test::Output - Test the output to STDOUT and STDERR your program generates

# TABLE OF CONTENTS

- [NAME](#name)
- [SYNOPSIS](#synopsis)
- [DESCRIPTION](#description)
Expand Down Expand Up @@ -237,6 +238,31 @@ Same as [`output-from`](#output-from), except captures STDERR only.

----

### `test-output-verbosity`

![][sub-signature]
```raku
sub test-output-verbosity (Bool :$on, Bool :$off) returns Str;
```

![][sub-usage-example]
```raku
# turn verbosity on
test-output-verbosity(:on);

my $output = output-from { do-something-interactive() };
# test output will now displayed during the test

# turn verbosity off
test-output-verbosity(:off);
```

Display the code's output while the test code is executed. This can be
very useful for author tests that require you to enter input based on
the output.

----

# REPOSITORY

Fork this module on GitHub:
Expand All @@ -258,4 +284,5 @@ The Artistic License 2.0. See the `LICENSE` file included in this
distribution for complete details.

[sub-signature]: _chromatin/sub-signature.png

[sub-usage-example]: _chromatin/sub-usage-example.png
32 changes: 24 additions & 8 deletions lib/Test/Output.pm6
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
unit module Test::Output;
use Test;

my Bool $verbosity;

sub test-output-verbosity(Bool :$on = True, Bool :$off = False) is export {
$verbosity = $on ?? True !! False;
$verbosity = $off ?? False !! True;
}

my class IO::Bag {
has @.err-contents;
has @.out-contents;
Expand All @@ -14,13 +21,22 @@ my class IO::Bag {
my class IO::Capture::Single is IO::Handle {
has Bool $.is-err = False ;
has IO::Bag $.bag is required;

has IO::Handle $.orig-handle;

submethod TWEAK {
self.encoding: 'utf8'; # set up encoder/decoder
}

method WRITE( IO::Handle:D: Blob:D \data --> Bool:D ) {
my $str = data.decode();

if $verbosity {
my $saved-out = $!is-err ?? $PROCESS::ERR !! $PROCESS::OUT;
$!is-err ?? ($PROCESS::ERR = $!orig-handle) !! ($PROCESS::OUT = $!orig-handle);
say $str.chomp;
$!is-err ?? ($PROCESS::ERR = $saved-out) !! ($PROCESS::OUT = $saved-out);
}

$.bag.all-contents.push: $str;
$!is-err ?? $.bag.err-contents.push: $str
!! $.bag.out-contents.push: $str;
Expand All @@ -30,20 +46,20 @@ my class IO::Capture::Single is IO::Handle {
}

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 $orig-out = $PROCESS::OUT;
my $orig-err = $PROCESS::ERR;

my $saved-out = $PROCESS::OUT;
my $saved-err = $PROCESS::ERR;
my $bag = IO::Bag.new;
my $out = IO::Capture::Single.new: :$bag, orig-handle => $orig-out;
my $err = IO::Capture::Single.new: :$bag, orig-handle => $orig-err, :is-err;

$PROCESS::OUT = $out;
$PROCESS::ERR = $err;

&code();

$PROCESS::OUT = $saved-out;
$PROCESS::ERR = $saved-err;
$PROCESS::OUT = $orig-out;
$PROCESS::ERR = $orig-err;

return {:out($bag.out), :err($bag.err), :all($bag.all)};
}
Expand Down
21 changes: 21 additions & 0 deletions t/01-capture.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,25 @@ is output-from( &test-code ), "42{$nl}warning!{$nl}After warning{$nl}",
is stdout-from( &test-code ), "42{$nl}After warning{$nl}", 'stdout-from works';
is stderr-from( &test-code ), "warning!{$nl}", 'stderr-from works';


test-output-verbosity(:on);
output-is &test-code, "42{$nl}warning!{$nl}warning!{$nl}After warning{$nl}", 'verbosity testing output-is';

output-like &test-code, /42.+warning '!' "{$nl}" warning.+After/, 'verbosity testing output-like';

stdout-is &test-code, "42{$nl}warning!{$nl}After warning{$nl}";

stdout-like &test-code, /42 "{$nl}" warning '!'/;

stderr-is &test-code, "warning!{$nl}";

stderr-like &test-code, /^ "warning!{$nl}" $/;

is output-from( &test-code ), "42{$nl}warning!{$nl}warning!{$nl}After warning{$nl}",
'verbosity output-from works';

is stdout-from( &test-code ), "42{$nl}warning!{$nl}After warning{$nl}", 'stdout-from works';

is stderr-from( &test-code ), "warning!{$nl}", 'stderr-from works';

done-testing;