Re: [code-review] Please review String::FlexMatch

Marcel GrĂ¼nauer <[email protected]> Sat, 6 Sep 2003 15:52:29 +0200
Newsgroups gmane.comp.lang.perl.code-review-ladder
Message-ID <[email protected]>
On Samstag, September 6, 2003, at 11:52  Uhr, Fergal Daly wrote:

> I'd say you should be using Test::Deep instead of putting overloaded 
> objects
> into is_deeply (at the end I've rewritten one of your tests using it). 
> For
> me, the fact that it used to work previously is a bug. I haven't heard
> Michael's opinion about it yet though.

First of all, I agree with Tony's response. Even Schwern said it:

    The test should not ignore stringification [...]
    When writing the test we should not have to be aware that they
    are really objects with stringification overloaded [...]
    overload::Method($thing, '""');".

His position seems clear enough to me.

So I believe either the 'xor' in _deep_check should be removed, or the
test for stringification using overload::Method I proposed should be 
added.

I've had a look at Test::Deep and like it. I've designed 
String::FlexMatch
to be used for data-driven testing and to be transparent; if I want to 
use
Test::Deep for data-driven tests, I have to walk the expected structure
first and convert it somewhat:

   $exp = Load do { local $/; <DATA> };
   prepare_deep($exp);

   sub prepare_deep {
     if    (ref $_[0] eq 'HASH')  { prepare_deep($_) for values %{$_[0]} 
}
     elsif (ref $_[0] eq 'ARRAY') { prepare_deep($_) for @{$_[0]} }
     elsif (ref $_[0] eq 'deeptest::regex') { $_[0] = re($_[0]->{val}) }
     elsif (ref $_[0] eq 'deeptest::code')  { $_[0] = code(eval 
$_[0]->{val}) }
   }

   __DATA__
   errors:
     attr1: A pure string
     attr2: !perl/deeptest::regex
       val: '.*/lib/Foo/Bar.pm'
     attr3: !perl/deeptest::code
       val: sub { $_[0] eq 'foo' }

Now $exp can be used with Test::Deep. I've found one (for me) very 
annoying
thing in that the expected structure has to be passed as the second 
argument
in cmp_deeply and eq_deeply. That is,

   cmp_deeply($hash, $exp, 'testname')

will work but

   cmp_deeply($exp, $hash, 'testname')

will not. That's another nice thing about String::FlexMatch - it's 
transparent
in this respect as well (if used with ok(eq_hash($foo, $bar)) ).

But overall, I find both modules, Test::Deep and String::FlexMatch, 
useful
(even though after reading more about Perl 6 I now believe that
"String::SmartMatch" might have been a better name).

By the way, code() doesn't seem to be documented in Test::Deep. And 
eq_deeply()
is said to need a test name as the third argument, when just below in 
the docs
it says that it doesn't output diagnostics.

Marcel