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