From: Vincent Pit Date: Tue, 28 Dec 2010 09:48:47 +0000 (+0100) Subject: Factor the capturing logic into Test::Leaner::TestHelper X-Git-Tag: v0.01~12 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=e63601e117e265834dc41b4001bcacfa73d270df;p=perl%2Fmodules%2FTest-Leaner.git Factor the capturing logic into Test::Leaner::TestHelper --- diff --git a/MANIFEST b/MANIFEST index 1d2acad..6475c44 100644 --- a/MANIFEST +++ b/MANIFEST @@ -26,3 +26,4 @@ t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t t/99-kwalitee.t +t/lib/Test/Leaner/TestHelper.pm diff --git a/t/05-pass.t b/t/05-pass.t index f999747..ecc6df1 100644 --- a/t/05-pass.t +++ b/t/05-pass.t @@ -5,37 +5,27 @@ use warnings; use Test::More; -plan skip_all => 'perl 5.8 required to test pass()' unless $] >= 5.008; +use Test::Leaner (); -my $buf = ''; -open my $memory_stream, '>', \$buf - or plan skip_all => 'could not create the in-memory file'; - -plan tests => 5; +use lib 't/lib'; +use Test::Leaner::TestHelper; -require Test::Leaner; +my $buf = ''; +capture_to_buffer $buf or plan skip_all => 'perl 5.8 required to test pass()'; -{ - local $@; - eval { Test::Leaner::tap_stream($memory_stream) }; - is $@, '', 'tap_stream($fh) does not croak'; -} +plan tests => 4; -{ +reset_buffer { local $@; - $buf = ''; - seek $memory_stream, 0, 0; eval { Test::Leaner::pass() }; is $@, '', 'pass() does not croak'; is $buf, "ok 1\n", 'pass() produces the correct TAP code'; -} +}; -{ +reset_buffer { local $@; - $buf = ''; - seek $memory_stream, 0, 0; eval { Test::Leaner::pass('this is a comment') }; is $@, '', 'pass("comment") does not croak'; is $buf, "ok 2 - this is a comment\n", 'pass("comment") produces the correct TAP code'; -} +}; diff --git a/t/06-fail.t b/t/06-fail.t index a3ed9e0..2f1fddf 100644 --- a/t/06-fail.t +++ b/t/06-fail.t @@ -5,37 +5,28 @@ use warnings; use Test::More; -plan skip_all => 'perl 5.8 required to test fail()' unless $] >= 5.008; +use Test::Leaner (); -my $buf = ''; -open my $memory_stream, '>', \$buf - or plan skip_all => 'could not create the in-memory file'; +use lib 't/lib'; +use Test::Leaner::TestHelper; -plan tests => 5; +my $buf = ''; +capture_to_buffer $buf or plan skip_all => 'perl 5.8 required to test fail()'; -require Test::Leaner; -{ - local $@; - eval { Test::Leaner::tap_stream($memory_stream) }; - is $@, '', 'tap_stream($fh) does not croak'; -} +plan tests => 4; -{ +reset_buffer { local $@; - $buf = ''; - seek $memory_stream, 0, 0; eval { Test::Leaner::fail() }; is $@, '', 'fail() does not croak'; is $buf, "not ok 1\n", 'fail() produces the correct TAP code'; -} +}; -{ +reset_buffer { local $@; - $buf = ''; - seek $memory_stream, 0, 0; eval { Test::Leaner::fail('this is a comment') }; is $@, '', 'fail("comment") does not croak'; is $buf, "not ok 2 - this is a comment\n", 'fail("comment") produces the correct TAP code'; -} +}; diff --git a/t/07-BAIL_OUT.t b/t/07-BAIL_OUT.t index cde9694..b1fe00b 100644 --- a/t/07-BAIL_OUT.t +++ b/t/07-BAIL_OUT.t @@ -5,49 +5,43 @@ use warnings; use Test::More; -plan skip_all => 'perl 5.8 required to test BAIL_OUT()' unless $] >= 5.008; +our $status; +BEGIN { + *CORE::GLOBAL::exit = *CORE::GLOBAL::exit = sub { + my $caller = caller; + if ($caller eq 'Test::Leaner') { + $status = $_[0] || 0; + } else { + CORE::exit $_[0]; + } + }; +} -my $buf = ''; -open my $memory_stream, '>', \$buf - or plan skip_all => 'could not create the in-memory file'; +use Test::Leaner (); -plan tests => 7; +use lib 't/lib'; +use Test::Leaner::TestHelper; -our $status; -*CORE::GLOBAL::exit = *CORE::GLOBAL::exit = sub { - my $caller = caller; - if ($caller eq 'Test::Leaner') { - $status = $_[0] || 0; - } else { - CORE::exit $_[0]; - } -}; +my $buf = ''; +capture_to_buffer $buf + or plan skip_all => 'perl 5.8 required to test BAIL_OUT()'; -require Test::Leaner; -{ - local $@; - eval { Test::Leaner::tap_stream($memory_stream) }; - is $@, '', 'tap_stream($fh) does not croak'; -} +plan tests => 6; -{ +reset_buffer { local ($@, $status); - $buf = ''; - seek $memory_stream, 0, 0; eval { Test::Leaner::BAIL_OUT() }; is $@, '', 'BAIL_OUT() does not croak'; is $buf, "Bail out!\n", 'BAIL_OUT() produces the correct TAP code'; is $status, 255, 'BAIL_OUT() exits with the correct status'; -} +}; -{ +reset_buffer { local ($@, $status); - $buf = ''; - seek $memory_stream, 0, 0; eval { Test::Leaner::BAIL_OUT('this is a comment') }; is $@, '', 'BAIL_OUT("comment") does not croak'; is $buf, "Bail out! this is a comment\n", 'BAIL_OUT("comment") produces the correct TAP code'; is $status, 255, 'BAIL_OUT("comment") exits with the correct status'; -} +}; diff --git a/t/lib/Test/Leaner/TestHelper.pm b/t/lib/Test/Leaner/TestHelper.pm new file mode 100644 index 0000000..fffb001 --- /dev/null +++ b/t/lib/Test/Leaner/TestHelper.pm @@ -0,0 +1,44 @@ +package Test::Leaner::TestHelper; + +use strict; +use warnings; + +my $memory_stream; +my $buf_ref; + +sub capture_to_buffer { + return unless $] >= 5.008; + + die "Can't call capture_to_buffer twice" if $memory_stream; + + $buf_ref = \$_[0]; + open $memory_stream, '>', $buf_ref + or die 'could not create the in-memory file'; + + Test::Leaner::tap_stream($memory_stream); + + return 1; +} + +# The ";&" prototype does not work well with perl 5.6 +sub reset_buffer (&) { + my $code = shift; + + die "The memory stream has not been initialized" unless $memory_stream; + + $$buf_ref = ''; + seek $memory_stream, 0, 0; + + goto $code if $code; +} + +our @EXPORT = qw< + capture_to_buffer + reset_buffer +>; + +use Exporter (); + +sub import { goto &Exporter::import } + +1;