Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
more tests for Proc::Async
in particular:
* test .write and .print, open for writing
* .close-stdin
* more exceptions
  • Loading branch information
moritz committed Jan 25, 2015
1 parent e223c44 commit 67bb8b3
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions S17-procasync/basic.t
Expand Up @@ -2,7 +2,7 @@ use v6;

use Test;

plan 14;
plan 24;

my $pc = $*DISTRO.is-win
?? Proc::Async.new( 'cmd', </c echo Hello World> )
Expand All @@ -24,11 +24,11 @@ isa_ok $pm, Promise;

throws_like { $pc.start }, X::Proc::Async::AlreadyStarted;

throws_like { $pc.print("foo") }, X::Proc::Async::OpenForWriting, :method<print>;
throws_like { $pc.say("foo") }, X::Proc::Async::OpenForWriting, :method<say>;
throws_like { $pc.print("foo") }, X::Proc::Async::OpenForWriting, :method<print>;
throws_like { $pc.say("foo") }, X::Proc::Async::OpenForWriting, :method<say>;
throws_like { $pc.write(Buf.new(0)) }, X::Proc::Async::OpenForWriting, :method<write>;

throws_like { $pc.stdout.tap(&say) }, X::Proc::Async::TapBeforeSpawn, :handle<stdout>;
throws_like { $pc.stdout.tap(&say) }, X::Proc::Async::TapBeforeSpawn, :handle<stdout>;

my $ps = await $pm;
isa_ok $ps, Proc::Status;
Expand All @@ -37,3 +37,37 @@ is $ps.?exit, 0, 'is the status ok';

is $stdout, "Hello World\n", 'did we get STDOUT';
is $stderr, "", 'did we get STDERR';

# now test one for writing

$pc = $*DISTRO.is-win
?? Proc::Async.new( :w, 'cmd', </c type con> )
!! Proc::Async.new( :w, 'cat', );

throws_like { $pc.close-stdin }, X::Proc::Async::MustBeStarted, :method<close-stdin>;
throws_like { $pc.kill }, X::Proc::Async::MustBeStarted, :method<kill>;
throws_like { $pc.say(42) }, X::Proc::Async::MustBeStarted, :method<say>;
throws_like { $pc.print(42) }, X::Proc::Async::MustBeStarted, :method<print>;
throws_like { $pc.write(Buf.new(0)) }, X::Proc::Async::MustBeStarted, :method<write>;

$stdout = '';
$stderr = '';
$pc.stdout.act: { $stdout ~= $_.subst("\r", "", :g) };
$pc.stdout.act: { $stderr ~= $_.subst("\r", "", :g) };

throws_like { $pc.stdout(:bin) }, X::Proc::Async::CharsOrBytes, :handle<stdout>;

my $start-promise := $pc.start;

# "Perl" as hex:
my $write-promise = $pc.write(Buf.new(0x50, 0x65, 0x72, 0x6c));
isa_ok $write-promise, Promise, '.write returned a promise';
await $write-promise;
my $print-promise = $pc.print(' 6');
isa_ok $print-promise, Promise, '.print returned a promise';

is $start-promise.status, Planned, 'external program still running (stdin still open)';

$pc.close-stdin;

isa_ok $start-promise.result, Proc::Status, 'Can finish, return Proc::Statu';

0 comments on commit 67bb8b3

Please sign in to comment.