X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FTest-Leaner.git;a=blobdiff_plain;f=t%2F24-cmp_ok.t;h=106ca5f0507fc23dad092d2cae2c7aaf2e8b4e6f;hp=ad880f35b5995b6206190ef679ecfe9a5d1d7ea4;hb=2264236b18e32bba8d1001a44c24671e3888ba8c;hpb=6c548b9b29eaa5e43616cb1c9422d0fd6b85121c 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 = (