From: Vincent Pit Date: Tue, 28 Dec 2010 10:33:05 +0000 (+0100) Subject: Test cmp_ok() with overloading X-Git-Tag: v0.01~10 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=2264236b18e32bba8d1001a44c24671e3888ba8c;p=perl%2Fmodules%2FTest-Leaner.git Test cmp_ok() with overloading --- diff --git a/t/24-cmp_ok.t b/t/24-cmp_ok.t index ad880f3..106ca5f 100644 --- a/t/24-cmp_ok.t +++ b/t/24-cmp_ok.t @@ -3,7 +3,28 @@ use strict; use warnings; -use Test::Leaner tests => 4 * 10 + 7 + 10; +{ + package Test::Leaner::TestCmpNum; + + use overload '<=>' => sub { + my ($x, $y, $r) = @_; + + $x = $x->{num}; + $y = $y->{num} if ref $y; + + ($x, $y) = ($y, $x) if $r; + + return $x <=> $y; + }; + + sub new { + my $class = shift; + + bless { num => $_[0] }, $class + } +} + +use Test::Leaner tests => 7 * 10 + 4 * 7 + 10; my @num_tests = ( [ '1.0', '==', '1.0' ], @@ -20,10 +41,39 @@ my @num_tests = ( for my $t (@num_tests) { my ($x, $op, $y) = @$t; + cmp_ok $x, $op, $y; cmp_ok int($x), $op, $y; cmp_ok $x, $op, int($y); cmp_ok int($x), $op, int($y); + + my $ox = Test::Leaner::TestCmpNum->new($x); + my $oy = Test::Leaner::TestCmpNum->new($y); + + cmp_ok $ox, $op, $y; + cmp_ok $x, $op, $oy; + cmp_ok $ox, $op, $oy; +} + +{ + package Test::Leaner::TestCmpStr; + + use overload 'cmp' => sub { + my ($x, $y, $r) = @_; + + $x = $x->{str}; + $y = $y->{str} if ref $y; + + ($x, $y) = ($y, $x) if $r; + + return $x cmp $y; + }; + + sub new { + my $class = shift; + + bless { str => $_[0] }, $class + } } my @str_tests = ( @@ -38,7 +88,15 @@ my @str_tests = ( for my $t (@str_tests) { my ($x, $op, $y) = @$t; + cmp_ok $x, $op, $y; + + my $ox = Test::Leaner::TestCmpStr->new($x); + my $oy = Test::Leaner::TestCmpStr->new($y); + + cmp_ok $ox, $op, $y; + cmp_ok $x, $op, $oy; + cmp_ok $ox, $op, $oy; } my @logic_tests = (