Skip to content

Commit

Permalink
is_deeply: fix handling of VSTRING and LVALUE refs
Browse files Browse the repository at this point in the history
VSTRING and LVALUE are possible return types from ref, but they are
essentially special cases of SCALAR refs. As far as is_deeply is
concerned, they should be treated as equivalent.

Update the _type function to normalize VSTRING and LVALUE into SCALAR,
so that the deep checks properly treat them as equivalent.

This fixes errors from doing comparisons with LVALUE refs, as well as
fixing VSTRINGs comparing equal to an equivalent normal SCALAR string.
  • Loading branch information
haarg committed May 23, 2023
1 parent 68cad46 commit 3fbf766
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
19 changes: 17 additions & 2 deletions lib/Test/More.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1206,13 +1206,28 @@ sub _format_stack {
return $out;
}

my %_types = (
(map +($_ => $_), qw(
Regexp
ARRAY
HASH
SCALAR
REF
GLOB
CODE
)),
'LVALUE' => 'SCALAR',
'REF' => 'SCALAR',
'VSTRING' => 'SCALAR',
);

sub _type {
my $thing = shift;

return '' if !ref $thing;

for my $type (qw(Regexp ARRAY HASH REF SCALAR GLOB CODE VSTRING)) {
return $type if UNIVERSAL::isa( $thing, $type );
for my $type (keys %_types) {
return $_types{$type} if UNIVERSAL::isa( $thing, $type );
}

return '';
Expand Down
30 changes: 29 additions & 1 deletion t/Legacy/is_deeply_fail.t
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ package main;


my $TB = Test::Builder->create;
$TB->plan(tests => 102);
$TB->plan(tests => 110);

# Utility testing functions.
sub ok ($;$) {
Expand Down Expand Up @@ -428,3 +428,31 @@ ERR
ok !is_deeply( [\\$version1], [\\$version2], "version objects");
is( $out, "not ok 42 - version objects\n" );
}

{
my $version1 = v1.2.3;
my $version2 = '' . v1.2.3;
ok is_deeply( [\$version1], [\$version2], "version objects");
is( $out, "ok 43 - version objects\n" );
}

{
my $version1 = v1.2.3;
my $version2 = v1.2.3;
ok !is_deeply( [$version1], [\$version2], "version objects");
is( $out, "not ok 44 - version objects\n" );
}

{
my $string = "abc";
my $string2 = "b";
ok is_deeply( [\substr($string, 1, 1)], [\$string2], "lvalue ref");
is( $out, "ok 45 - lvalue ref\n" );
}

{
my $string = "b";
my $string2 = "b";
ok !is_deeply( [\substr($string, 1, 1)], ["b"], "lvalue ref");
is( $out, "not ok 46 - lvalue ref\n" );
}

0 comments on commit 3fbf766

Please sign in to comment.