Skip to content

Commit

Permalink
For issue Test-More#141, compare cmp_ok with a blacklist of bad opera…
Browse files Browse the repository at this point in the history
…tors.
  • Loading branch information
wolfsage committed Apr 1, 2012
1 parent 99ca621 commit 426c2ec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/Test/Builder.pm
Expand Up @@ -946,9 +946,16 @@ Works just like Test::More's C<cmp_ok()>.

my %numeric_cmps = map { ( $_, 1 ) } ( "<", "<=", ">", ">=", "==", "!=", "<=>" );

# Bad, these are not comparison operators. Should we include more?
my %cmp_ok_bl = map { ( $_, 1 ) } ( "=", "+=", ".=", "x=", "^=", "|=", "||=", "&&=", "...");

sub cmp_ok {
my( $self, $got, $type, $expect, $name ) = @_;

if ($cmp_ok_bl{$type}) {
$self->croak("$type is not a valid comparison operator in cmp_ok()");
}

my $test;
my $error;
{
Expand Down
23 changes: 19 additions & 4 deletions t/cmp_ok.t
Expand Up @@ -21,18 +21,31 @@ my $TB = Test::Builder->create;
$TB->level(0);

sub try_cmp_ok {
my($left, $cmp, $right) = @_;
my($left, $cmp, $right, $error) = @_;

my %expect;
$expect{ok} = eval "\$left $cmp \$right";
$expect{error} = $@;
$expect{error} =~ s/ at .*\n?//;
$expect{error} = $error;

if (!$expect{error}) {
$expect{error} = $@;
$expect{error} =~ s/ at .*\n?//;
}

local $Test::Builder::Level = $Test::Builder::Level + 1;
my $ok = cmp_ok($left, $cmp, $right, "cmp_ok");

my $ok;
eval { $ok = cmp_ok($left, $cmp, $right, "cmp_ok"); };

$TB->is_num(!!$ok, !!$expect{ok}, " right return");

my $diag = $err->read;

if ($@) {
$diag = $@;
$diag =~ s/ at .*\n?//;
}

if( !$ok and $expect{error} ) {
$diag =~ s/^# //mg;
$TB->like( $diag, qr/\Q$expect{error}\E/, " expected error" );
Expand Down Expand Up @@ -65,6 +78,8 @@ my @Tests = (
[$cmp, 'eq', "foo"],
[$ify, 'eq', "bar"],
[$ify, "==", 23],

[1, "=", 0, "= is not a valid comparison operator in cmp_ok()"],
);

plan tests => scalar @Tests;
Expand Down

0 comments on commit 426c2ec

Please sign in to comment.