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

support qr// regexps in expect() #11

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/Expect.pm
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,10 @@ sub expect {
print STDERR ("expect(): handling param '$parm'...\n")
if $Expect::Debug;
if ( ref($parm) ) {
if ( ref($parm) eq 'ARRAY' ) {
if ( ref($parm) eq 'Regexp' ) {
push @pattern_list, [ $parm_nr, '-re', $parm, undef ];
}
elsif ( ref($parm) eq 'ARRAY' ) {
my $err = _add_patterns_to_list(
\@pattern_list, \@timeout_list,
$parm_nr, $parm
Expand Down Expand Up @@ -2105,8 +2108,9 @@ makes the pty transparently act like a bidirectional pipe.

Given $timeout in seconds Expect will wait for $object's handle to produce
one of the match_patterns, which are matched exactly by default. If you
want a regexp match, prefix the pattern with '-re'.
want a regexp match, use a regexp object (C<qr//>) or prefix the pattern with '-re'.

$object->expect(15, 'match me exactly', qr/match\s+me\s+exactly/);
$object->expect(15, 'match me exactly','-re','match\s+me\s+exactly');

Due to o/s limitations $timeout should be a round number. If $timeout
Expand Down
15 changes: 4 additions & 11 deletions t/01-test.t
Original file line number Diff line number Diff line change
Expand Up @@ -445,20 +445,13 @@ subtest respawn => sub {
like $@, qr/^Cannot reuse an object with an already spawned command/;
};

subtest "implicit timeout" => sub {
diag "Basic tests...";
plan tests => 4;
subtest "regexp ref" => sub {
plan tests => 1;

my $exp = Expect->spawn("$Perl -v");
$exp->timeout(10);

ok( defined $exp );
$exp->log_user(0);
is( $exp->expect( "krzlbrtz", "Copyright" ), 2 );
is( $exp->expect( "Larry Wall", "krzlbrtz" ), 1 );
ok( not $exp->expect( "Copyright" ) );
};

is( $exp->expect( 10, qr/L.*[WH]all/ ), 1 );
};

use Test::Builder;
my $Test = Test::Builder->new;
Expand Down
Loading