Posted on 2008-06-10 15:27:16-07 by ghendricks
Comparing undef and empty string
First off, thanks so much for Test::Deep. It is a life saver.

I am running into a problem that I hope you can point me in the right direction for solving. I am using Test::Deep to compare the results of a SOAP::Lite XMLRPC call to the original blessed object. The blessed objects come from a database in which some fields are null which become undef in Perl. The problem is that XMLRPC interprets these null fields as empty strings ('') and when I compare the two, it fails. What I am wondering is if there is a way to tell Test::Deep to treat empty string and undef as equal. For that matter it would be great if there was a way to compare any "falsy" value with another or any "truthy" value with another and have it pass.

I have pored over the documentation and have come up empty. Any suggestions?
Direct Responses: 8051 | Write a response
Posted on 2008-06-10 15:50:05-07 by fergal in response to 8050
Re: Comparing undef and empty string

Unfortunately I don't have a good suggestion right now. You can force Test::Deep to use stringwise comparison using str() but that'll only be useful you're constructing one side by hand. In your case both sides are constructed by things out of your control.

Just in case you, Test::More::is_deeply considers undef and "" to be equal but I assume since you're using Test::Deep, Test::More::is_deeply is not suitable.

You could do something a little drastic - intercept all the values and convert undef to "". Something like (untested)

{ my $old_descend = \&Test::Deep::descend; local *Test::Deep::descend = sub { my $d1, $d2 = @_; $d1 = "" unless defined $d1; $old_descend->($d1, $d2); } }

It's kinda sucky but descend() is part of Test::Deep's interface (although not documented).

Direct Responses: 8058 | Write a response
Posted on 2008-06-11 21:06:22-07 by ghendricks in response to 8051
Re: Comparing undef and empty string
Yes, my reason for using Test::Deep in the first place is that I have arrays of blessed objects. Test::Deep seemed perfect for this.

I tried the code snippet above, but did not have success. This might be due to the fact that I wasn't sure where to put it. I tried placing at the top of my test script and when I ran, it said that it was overwriting Test::Deep::decend, but then made no difference. Even when I inserted a bunch of print statements to find out what was happening, it did nothing.

My next trick was to edit the Test::Deep module itself in my /usr/lib/perl5 directory. This did output the print statements, but still had no luck with the undef comparison (behaved exactly as before). I am guessing this is because the one side is a blessed object or an array of blessed objects that I am calling with noclass(). Looking at the docs this appears to do a special comparison. In traversing the remaining modules in Test::Deep, I wasn't able to identify which was being used. I am rather new to Perl testing and only an intermediate Perl user, so I am sure I am missing something here.

If I can figure out what I am doing, I will try to implement it as I mentioned before, that is with some sort of option to allow these comparisons to act the same. I'd appreciate any additional pointers. Thanks for your help.

Greg
Direct Responses: 8059 | Write a response
Posted on 2008-06-11 21:28:05-07 by fergal in response to 8058
Re: Comparing undef and empty string

You might also try doing the unless defined for $d2 instead of/as well as $d1. Otherwise, you'll have to wait until I have time to look at this properly - it's not something that Test::Deep supports out of the box but it is something that I would like it to support.

Direct Responses: 8076 | Write a response
Posted on 2008-06-13 16:11:11-07 by ghendricks in response to 8059
Re: Comparing undef and empty string
Still no luck.

I was able to write a filter on my end to clean up the undef values on the objects before passing them to cmp_deeply. I still think it would be a good idea to have an option for this though. Is there an offical place to log such a request?

Thanks again for you help and patience.
Direct Responses: 8077 | Write a response
Posted on 2008-06-13 17:35:56-07 by fergal in response to 8076
Re: Comparing undef and empty string

Yes, all CPAN modules have a bug/feature tracking system. Just click on the "View and report bugs" link above.

Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.