From: Vincent Pit Date: Tue, 28 Dec 2010 10:13:37 +0000 (+0100) Subject: Test ok() return value, with overloading, and failing X-Git-Tag: v0.01~11 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FTest-Leaner.git;a=commitdiff_plain;h=6c548b9b29eaa5e43616cb1c9422d0fd6b85121c Test ok() return value, with overloading, and failing --- diff --git a/MANIFEST b/MANIFEST index 6475c44..b3a5263 100644 --- a/MANIFEST +++ b/MANIFEST @@ -20,8 +20,9 @@ t/18-skip.t t/19-comments.t t/80-threads.t t/20-ok.t -t/21-is.t -t/22-cmp_ok.t +t/21-ok-failing.t +t/22-is.t +t/24-cmp_ok.t t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t diff --git a/t/20-ok.t b/t/20-ok.t index 9bc35ad..e6fcafe 100644 --- a/t/20-ok.t +++ b/t/20-ok.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::Leaner tests => 4 + 1; +use Test::Leaner tests => 4 + 1 + 2 + 1; ok 1; ok !!1, 'ok() test with a description'; @@ -12,3 +12,18 @@ ok +{}, 'a hash ref is fine too'; my @array = (undef); ok @array, 'ok() forces scalar context'; + +my $ret = ok 1; +ok $ret, 'ok(true) returns true'; + +{ + package Test::Leaner::TestOverload::AlwaysFalse; + + use overload 'bool' => sub { 1 }; + + sub new { bless { }, shift } +} + +my $z = Test::Leaner::TestOverload::AlwaysFalse->new; + +ok $z, 'ok($overloaded_true)'; diff --git a/t/21-ok-failing.t b/t/21-ok-failing.t new file mode 100644 index 0000000..c00ab83 --- /dev/null +++ b/t/21-ok-failing.t @@ -0,0 +1,69 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More; + +use Test::Leaner (); + +use lib 't/lib'; +use Test::Leaner::TestHelper; + +my $buf; +capture_to_buffer $buf + or plan skip_all =>'perl 5.8 required to test ok() failing'; + +plan tests => 3 * 5; + +reset_buffer { + local $@; + my $ret = eval { Test::Leaner::ok(0) }; + is $@, '', 'ok(0) does not croak'; + ok !$ret, 'ok(0) returns false'; + is $buf, "not ok 1\n", 'ok(0) produces the correct TAP code'; +}; + +reset_buffer { + local $@; + my $ret = eval { Test::Leaner::ok(undef) }; + is $@, '', 'ok(undef) does not croak'; + ok !$ret, 'ok(undef) returns false'; + is $buf, "not ok 2\n", 'ok(undef) produces the correct TAP code'; +}; + +reset_buffer { + local $@; + my $ret = eval { Test::Leaner::ok(!1) }; + is $@, '', 'ok(false) does not croak'; + ok !$ret, 'ok(false) returns false'; + is $buf, "not ok 3\n", 'ok(false) produces the correct TAP code'; +}; + +reset_buffer { + local $@; + my $ret = eval { Test::Leaner::ok(0, 'this is a comment') }; + is $@, '', 'ok(0, "comment") does not croak'; + ok !$ret, 'ok(0, "comment") returns false'; + is $buf, "not ok 4 - this is a comment\n", + 'ok(0, "comment") produces the correct TAP code'; +}; + +{ + package Test::Leaner::TestOverload::AlwaysFalse; + + use overload 'bool' => sub { !1 }; + + sub new { bless { }, shift } +} + +my $z = Test::Leaner::TestOverload::AlwaysFalse->new; + +reset_buffer { + local $@; + my $ret = eval { Test::Leaner::ok($z) }; + is $@, '', 'ok($overloaded_false) does not croak'; + ok !$ret, 'ok($overloaded_false) returns false'; + is $buf, "not ok 5\n", 'ok($overloaded_false) produces the correct TAP code'; +}; + diff --git a/t/21-is.t b/t/22-is.t similarity index 100% rename from t/21-is.t rename to t/22-is.t diff --git a/t/22-cmp_ok.t b/t/24-cmp_ok.t similarity index 100% rename from t/22-cmp_ok.t rename to t/24-cmp_ok.t