Debian lenny version packages
[pkg-perl] / deb-src / libtest-simple-perl / libtest-simple-perl-0.80 / t / cmp_ok.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't';
6         @INC = ('../lib', 'lib');
7     }
8     else {
9         unshift @INC, 't/lib';
10     }
11 }
12
13 use strict;
14
15 require Test::Simple::Catch;
16 my($out, $err) = Test::Simple::Catch::caught();
17 local $ENV{HARNESS_ACTIVE} = 0;
18
19 require Test::Builder;
20 my $TB = Test::Builder->create;
21 $TB->level(0);
22
23 sub try_cmp_ok {
24     my($left, $cmp, $right) = @_;
25     
26     my %expect;
27     $expect{ok}    = eval "\$left $cmp \$right";
28     $expect{error} = $@;
29     $expect{error} =~ s/ at .*\n?//;
30
31     local $Test::Builder::Level = $Test::Builder::Level + 1;
32     my $ok = cmp_ok($left, $cmp, $right);
33     $TB->is_num(!!$ok, !!$expect{ok});
34     
35     my $diag = $err->read;
36     if( !$ok and $expect{error} ) {
37         $diag =~ s/^# //mg;
38         $TB->like( $diag, "/\Q$expect{error}\E/" );
39     }
40     elsif( $ok ) {
41         $TB->is_eq( $diag, '' );
42     }
43     else {
44         $TB->ok(1);
45     }
46 }
47
48
49 use Test::More;
50 Test::More->builder->no_ending(1);
51
52 my @Tests = (
53     [1, '==', 1],
54     [1, '==', 2],
55     ["a", "eq", "b"],
56     ["a", "eq", "a"],
57     [1, "+", 1],
58     [1, "-", 1],
59 );
60
61 # These don't work yet.
62 if( 0 ) {
63 #if( eval { require overload } ) {
64     require MyOverload;
65     
66     my $cmp = Overloaded::Compare->new("foo", 42);
67     my $ify = Overloaded::Ify->new("bar", 23);
68     
69     push @Tests, (
70         [$cmp, '==', 42],
71         [$cmp, 'eq', "foo"],
72         [$ify, 'eq', "bar"],
73         [$ify, "==", 23],
74     );
75 }
76
77 plan tests => scalar @Tests;
78 $TB->plan(tests => @Tests * 2);
79
80 for my $test (@Tests) {
81     try_cmp_ok(@$test);
82 }