Move Test::Simple from lib to ext.
[p5sagit/p5-mst-13.2.git] / ext / Test-Simple / t / cmp_ok.t
CommitLineData
6b38a9b9 1#!/usr/bin/perl -w
2
3BEGIN {
4 if( $ENV{PERL_CORE} ) {
5 chdir 't';
eaa08151 6 @INC = ('../lib', 'lib', '../lib/Test/Simple/t/lib');
6b38a9b9 7 }
8 else {
9 unshift @INC, 't/lib';
10 }
11}
12
13use strict;
14
15require Test::Simple::Catch;
16my($out, $err) = Test::Simple::Catch::caught();
17local $ENV{HARNESS_ACTIVE} = 0;
18
19require Test::Builder;
20my $TB = Test::Builder->create;
21$TB->level(0);
22
23sub 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;
82d700dc 32 my $ok = cmp_ok($left, $cmp, $right, "cmp_ok");
33 $TB->is_num(!!$ok, !!$expect{ok}, " right return");
6b38a9b9 34
35 my $diag = $err->read;
36 if( !$ok and $expect{error} ) {
37 $diag =~ s/^# //mg;
82d700dc 38 $TB->like( $diag, qr/\Q$expect{error}\E/, " expected error" );
6b38a9b9 39 }
40 elsif( $ok ) {
82d700dc 41 $TB->is_eq( $diag, '', " passed without diagnostic" );
6b38a9b9 42 }
43 else {
82d700dc 44 $TB->ok(1, " failed without diagnostic");
6b38a9b9 45 }
46}
47
48
49use Test::More;
50Test::More->builder->no_ending(1);
51
82d700dc 52require MyOverload;
53my $cmp = Overloaded::Compare->new("foo", 42);
54my $ify = Overloaded::Ify->new("bar", 23);
55
6b38a9b9 56my @Tests = (
57 [1, '==', 1],
58 [1, '==', 2],
59 ["a", "eq", "b"],
60 ["a", "eq", "a"],
61 [1, "+", 1],
62 [1, "-", 1],
6b38a9b9 63
82d700dc 64 [$cmp, '==', 42],
65 [$cmp, 'eq', "foo"],
66 [$ify, 'eq', "bar"],
67 [$ify, "==", 23],
68);
6b38a9b9 69
70plan tests => scalar @Tests;
71$TB->plan(tests => @Tests * 2);
72
73for my $test (@Tests) {
74 try_cmp_ok(@$test);
75}