From d781f229364d0e5c264e113f8ae754782892188e Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 26 May 2023 10:38:08 -0400 Subject: [PATCH 1/2] support qr// regexps in --- lib/Expect.pm | 8 ++++++-- t/01-test.t | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Expect.pm b/lib/Expect.pm index 00941c6..9a62a92 100644 --- a/lib/Expect.pm +++ b/lib/Expect.pm @@ -500,7 +500,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 @@ -2098,8 +2101,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) 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 diff --git a/t/01-test.t b/t/01-test.t index 85938a2..d2291a4 100644 --- a/t/01-test.t +++ b/t/01-test.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 15; use File::Temp qw(tempdir); use Expect; use Config; @@ -444,6 +444,14 @@ subtest respawn => sub { like $@, qr/^Cannot reuse an object with an already spawned command/; }; +subtest "regexp ref" => sub { + plan tests => 1; + + my $exp = Expect->spawn("$Perl -v"); + $exp->log_user(0); + is( $exp->expect( 10, qr/L.*[WH]all/ ), 1 ); +}; + use Test::Builder; my $Test = Test::Builder->new; From b762eb29fe2869f3944c7845590566406fbdd0a6 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Fri, 23 Feb 2024 16:17:07 -0500 Subject: [PATCH 2/2] Update 01-test.t --- t/01-test.t | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/t/01-test.t b/t/01-test.t index 6b0dfdf..b6373a9 100644 --- a/t/01-test.t +++ b/t/01-test.t @@ -453,21 +453,6 @@ subtest "regexp ref" => sub { is( $exp->expect( 10, qr/L.*[WH]all/ ), 1 ); }; -subtest "implicit timeout" => sub { - diag "Basic tests..."; - plan tests => 4; - - 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" ) ); -}; - - use Test::Builder; my $Test = Test::Builder->new; diag <<__EOT__ if ( not $Test->is_passing );