develooper Front page | perl.perl5.porters | Postings from April 2006

Re: [PATCH] t/op/pat.t using test.pl

Thread Previous | Thread Next
From:
demerphq
Date:
April 27, 2006 08:02
Subject:
Re: [PATCH] t/op/pat.t using test.pl
Message ID:
9b18b3110604270802l7a89f1det7a6464f2d8a23042@mail.gmail.com
On 4/27/06, David Landgren <david@landgren.net> wrote:
> Jim Cromie wrote:
> > David Landgren wrote:
> >>
> >> In a further iteration I'd like to break out all the
> >>
> >>   ok(/(expr)/ && $1 eq 'foo', 'my test')
> >>
> >> tests into
> >>
> >>   ok(/(expr)/, 'my test match');
> >>   cmp_ok($1, 'eq', 'foo', 'my test result');
> >>
> >>
> > does it matter that $1 is only set on a match (IIRC), and that an old value
> > could accidentally match the expected  ?  ('foo' being as common as it is)
> > I imagine the tests avoided that potential..
>
> You are quite right, they do, so some defensive coding would be required.
>
> perl -le '$_="ab";/(a)(b)/; print "X$1$2"; /(c)/; print "Y$1$2"'
> Xab
> Yab
>
> perl -le '$_="ab";/(a)(b)/; print "X$1$2"; undef=~/()/; /(c)/; print
> "Y$1$2"'
> Xab
> Y
>
> Or through local
>
> perl -le '$_="ab";{local $1,$2;/(a)(b)/; print "X$1$2"} {local $1,$2;
> /(c)/; print "Y$1$2"}'
> Xab
> Y
>
> Unless there's a better way to reset the capture variables, especially
> in the context of "what are we really trying to test here?".
>
> Perhaps the idea is unwise. It started out with the notion that if a
> combo-test fails you have to figure out which part of the test failed.
> It could be that the match succeeded, but the result was wrong, or that
> the match failed. With two tests, you would know that immediately.
>
> > <insane>
> >    is there a string equivalent to NaN, that doent match any string, inc
> > (another copy of) itself ?
> > </>
>
> I think that would require tied magic, thus more trouble than it's worth?

If I was doing this id do it something like:

  if (ok(/PAT/,"$test matches")) {
    ok($1 eq $expected,"$test captured correctly");
  } else {
    fail("$test captured correctly");
  }

Cheers,
Yves
--
perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About