Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Test:: to match multiple lines against corresponding regular expressions

by Dallaylaen (Chaplain)
on Jun 24, 2018 at 12:05 UTC ( [id://1217317]=perlquestion: print w/replies, xml ) Need Help??

Dallaylaen has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear esteemed monks!

I'm looking for a way to test multiple lines to match corresponding regular expressions:

multi_like \@array, [ qr/regex1/, qr/regex2/, .... ], "Test name";

Now obviously I can do it with a subtest and like statements, plus one test for length:

subtest "All lines are like expected" => sub { like $array[0], qr/regex1/; like $array[1], qr/regex2/; .... is scalar @array, $nnn, "No extra lines"; };

And I can do it with one go with Test::Deep, although I haven't yet found out how to make it report the full difference and not just the first mismatch.

But maybe there's a module that does exactly that (matches a list of strings against a list of regexps)?

Thanks!

Replies are listed 'Best First'.
Re: Test:: to match multiple lines against corresponding regular expressions
by haukex (Archbishop) on Jun 24, 2018 at 12:11 UTC

    Tests are just functions, so you can call them in a loop (the subtest is optional):

    use warnings; use strict; use Test::More; my @regexen = ( qr/foo/, qr/bar/, qr/quz/, qr/baz/ ); my @lines = ( "foo!", "Bar!", "Quz!", "baz!" ); subtest "lines match" => sub { plan tests=>@regexen+1; is @lines, @regexen, "count"; like $lines[$_], $regexen[$_], "line ".($_+1) for 0..$#regexen; }; done_testing; __END__ # Subtest: lines match 1..5 ok 1 - count ok 2 - line 1 not ok 3 - line 2 # Failed test 'line 2' # at x.pl line 11. # 'Bar!' # doesn't match '(?^:bar)' not ok 4 - line 3 # Failed test 'line 3' # at x.pl line 11. # 'Quz!' # doesn't match '(?^:quz)' ok 5 - line 4 # Looks like you failed 2 tests of 5. not ok 1 - lines match # Failed test 'lines match' # at x.pl line 12. 1..1 # Looks like you failed 1 test of 1.

    Update: Added another test case and the subtest.

Re: Test:: to match multiple lines against corresponding regular expressions
by choroba (Cardinal) on Jun 24, 2018 at 17:13 UTC
    > And I can do it with one go with Test::Deep

    Do you mean

    use Test::Deep; cmp_deeply \@lines, bag(map re($_), @regexen);
    ?
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Yes, and even without bag because I need to preserve order. Still I'd like to have a message about all mismatches, not just the first one.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1217317]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-28 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found