Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of uninitialized value $got in pattern match (m//) at Test/Deep/Regexp.pm line 57. #39

Open
szabgab opened this issue Mar 17, 2016 · 2 comments

Comments

@szabgab
Copy link

szabgab commented Mar 17, 2016

The above warning is generated by the following code:

use Test::More;
use Test::Deep;

plan tests => 1;

my @data = (
    {
        name => 'Foo',
    },
    {
        name => undef,
    },
);

cmp_deeply \@data, array_each({
    name => re('^.*$')
});

If undef is ineed an acceptable value then the test should be written
name => any(undef, re('^.*$')

but when the data we are checking is a lot more complex it is quite hard to find the problematic field and regex.
Would it be possible to make this a test failure? Maybe, in order to keep backward compatibility, depending on some parameter?

e.g. adding return 0 if not defined $got; on line 31 of Text/Deep/Regexp would have helped me.

@wolfsage
Copy link
Contributor

wolfsage commented Aug 9, 2016

I think maintaining backward compatibility is a good idea here.

Consider this contrived case:

cmp_deeply(
  [ { a => undef }, { a => 1 } ],
  set(
    { a => re(qr/\d/) }, { a => undef },
  ),
  "Ok!"
);

This also warns, but should IMO be perfectly valid.

@jlcooper
Copy link

I just encountered this issue when using bag(), which is another situation where the warning doesn't make sense.

Here's an example that shows it:

my $data = [
    { start => '2021-06-16',  end => undef },
    { start => '2021-06-16',  end => '2021-06-17' },
];

cmp_deeply(
    $data,
    bag(
        {
            start => re('^\d{4}-\d{2}-\d{2}$'),
            end   => re('^\d{4}-\d{2}-\d{2}$'),
        },{
            start => re('^\d{4}-\d{2}-\d{2}$'),
            end   => undef,
        },
    ),
    "The data should contain one entry with an end date and another entry with end undefined"
);

For those of you who have got here from Google and looking for a workaround then you can get rid of the warning by swapping the re() out for a code() that checks to see if the value is defined and matches your regular expression - e.g.

cmp_deeply(
    $data,
    bag(
        {
            start => re('^\d{4}-\d{2}-\d{2}$'),
            # Using code() rather than re() to avoid the undef warning bug
            #   see https://github.com/rjbs/Test-Deep/issues/39
            end   => code( sub { defined $_[0] && $_[0] =~ m/^\d{4}-\d{2}-\d{2}$/ } ),
        },{
            start => re('^\d{4}-\d{2}-\d{2}$'),
            end   => undef,
        },
    ),
    "The data should contain one entry with an end date and another entry with end undefined"
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants