From ea3c1689821f4cb566f220a4f052eb1472574c12 Mon Sep 17 00:00:00 2001 From: Tatsuhiko Miyagawa Date: Thu, 8 Nov 2012 19:07:46 -0800 Subject: [PATCH] Move the test aggregator to BEGIN so that it gets preload friendly --- lib/Test/Class.pm | 12 ++---------- t/WarnCatch.pm | 10 ++++++++++ t/override.t | 7 ++----- t/rt17264.t | 11 ----------- 4 files changed, 14 insertions(+), 26 deletions(-) create mode 100644 t/WarnCatch.pm mode change 100644 => 100755 t/override.t delete mode 100644 t/rt17264.t diff --git a/lib/Test/Class.pm b/lib/Test/Class.pm index 40bc80a..40946b6 100644 --- a/lib/Test/Class.pm +++ b/lib/Test/Class.pm @@ -13,12 +13,6 @@ use Test::Class::MethodInfo; our $VERSION = '0.37'; -my $Check_block_has_run; -{ - no warnings 'void'; - CHECK { $Check_block_has_run = 1 }; -} - use constant NO_PLAN => "no_plan"; use constant SETUP => "setup"; use constant TEST => "test"; @@ -92,7 +86,7 @@ sub _is_public_method { return 1; } -sub Test : ATTR(CODE,RAWDATA) { +sub Test : ATTR(CODE,RAWDATA,BEGIN) { my ($class, $symbol, $code_ref, $attr, $args) = @_; if ($symbol eq "ANON") { warn "cannot test anonymous subs - you probably loaded a Test::Class too late (after the CHECK block was run). See 'A NOTE ON LOADING TEST CLASSES' in perldoc Test::Class for more details\n"; @@ -105,7 +99,7 @@ sub Test : ATTR(CODE,RAWDATA) { }; }; -sub Tests : ATTR(CODE,RAWDATA) { +sub Tests : ATTR(CODE,RAWDATA,BEGIN) { my ($class, $symbol, $code_ref, $attr, $args) = @_; $args ||= 'no_plan'; Test( $class, $symbol, $code_ref, $attr, $args ); @@ -346,8 +340,6 @@ sub _test_classes { }; sub runtests { - die "Test::Class was loaded too late (after the CHECK block was run). See 'A NOTE ON LOADING TEST CLASSES' in perldoc Test::Class for more details\n" - unless $Check_block_has_run; my @tests = @_; if (@tests == 1 && !ref($tests[0])) { my $base_class = shift @tests; diff --git a/t/WarnCatch.pm b/t/WarnCatch.pm new file mode 100644 index 0000000..fd183f1 --- /dev/null +++ b/t/WarnCatch.pm @@ -0,0 +1,10 @@ +package t::WarnCatch; + +my $warning; +$SIG{__WARN__} = sub { $warning = "@_" }; + +sub Caught { $warning } + +1; + + diff --git a/t/override.t b/t/override.t old mode 100644 new mode 100755 index 74f1394..8cb432a --- a/t/override.t +++ b/t/override.t @@ -4,11 +4,8 @@ use strict; use warnings; use Test::More tests => 1; -my $warning; - BEGIN { - - $SIG{ __WARN__ } = sub { $warning = "@_" }; + use t::WarnCatch; # has to be use so that it runs earlier than BEGIN { package Base::Test; @@ -25,5 +22,5 @@ BEGIN { } } -like $warning, qr/overriding public method/, +like t::WarnCatch::Caught, qr/overriding public method/, 'cannot override a public method with a test method'; diff --git a/t/rt17264.t b/t/rt17264.t deleted file mode 100644 index 9d1e61d..0000000 --- a/t/rt17264.t +++ /dev/null @@ -1,11 +0,0 @@ -#! /usr/bin/perl - -use strict; -use warnings; -use Test::Exception tests => 1; - -use lib 't/rt17264'; -require 'Test/Class.pm'; - -throws_ok { Test::Class->runtests } qr/Test::Class was loaded too late/, - 'we figured out that we loaded Test::Class too late'; \ No newline at end of file