-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathPerlbrewTestHelpers.pm
69 lines (57 loc) · 1.53 KB
/
PerlbrewTestHelpers.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package PerlbrewTestHelpers;
use Test2::V0;
use Test2::Plugin::IOEvents;
use Exporter 'import';
our @EXPORT_OK = qw(
read_file write_file
stderr_from stderr_is stderr_like
stdout_from stdout_is stdout_like
);
# Replacements of Test::Output made by using Test2::Plugin::IOEvents
sub ioevents_from {
my ($cb) = @_;
return grep { $_->tag eq 'STDOUT' || $_->tag eq 'STDERR' }
map { @{ $_->facets->{info} } }
@{ intercept(sub { $cb->() }) };
}
sub stderr_from (&) {
my ($cb) = @_;
join "", map { $_->details } grep { $_->tag eq 'STDERR' } ioevents_from($cb);
}
sub stderr_is (&$;$) {
my ($cb, $expected, $desc) = @_;
is(stderr_from(sub { $cb->() }), $expected, $desc);
}
sub stderr_like (&$;$) {
my ($cb, $re, $desc) = @_;
like(stderr_from(sub { $cb->() }), $re, $desc);
}
sub stdout_from (&) {
my ($cb) = @_;
return join "", map { $_->details } grep { $_->tag eq 'STDOUT' } ioevents_from($cb);
}
sub stdout_is (&$;$) {
my ($cb, $expected, $desc) = @_;
is(stdout_from(sub { $cb->() }), $expected, $desc);
}
sub stdout_like (&$;$) {
my ($cb, $re, $desc) = @_;
like(stdout_from(sub { $cb->() }), $re, $desc);
}
sub read_file {
my ($file) = @_;
open my $fh, '<', $file
or die "Cannot open $file for read: $!";
local $/ = undef;
my $content = <$fh>;
return $content;
}
sub write_file {
my ($file, $content) = @_;
open my $fh, '>', $file
or die "Cannot open $file for write: $!";
print $fh $content;
close $fh;
return;
}
1;