| 1 | package Test::Unit::Procedural; | ||
| 2 | BEGIN { | ||
| 3 | 3 | ▶ | $Test::Unit::Procedural::VERSION = '0.25_1325'; # added by dist-tools/SetVersion.pl |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | } | ||
| 5 | |||
| 6 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 3 | ▶ | use Test::Unit::TestSuite; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Test::Unit::TestRunner; | ||
| 10 | |||
| 11 | use base 'Exporter'; | ||
| 12 | |||
| 13 | use vars qw(@EXPORT); | ||
| 14 | @EXPORT = qw(assert create_suite run_suite add_suite); | ||
| 15 | |||
| 16 | # Helper classes | ||
| 17 | use Devel::Symdump; | ||
| 18 | use Class::Inner; | ||
| 19 | |||
| 20 | # Exception handling | ||
| 21 | use Error qw/:try/; | ||
| 22 | use Test::Unit::Exception; | ||
| 23 | use Test::Unit::Failure; | ||
| 24 | |||
| 25 | # private | ||
| 26 | |||
| 27 | my $test_suite = Test::Unit::TestSuite->empty_new("Test::Unit"); | ||
| 28 | my %suites = (); | ||
| 29 | %suites = ('Test::Unit' => $test_suite); | ||
| 30 | |||
| 31 | sub add_to_suites { | ||
| 32 | my $suite_holder = shift; | ||
| 33 | if (not exists $suites{$suite_holder}) { | ||
| 34 | my $test_suite = Test::Unit::TestSuite->empty_new($suite_holder); | ||
| 35 | $suites{$suite_holder} = $test_suite; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | # public | ||
| 40 | |||
| 41 | sub assert ($;$) { | ||
| 42 | my($condition, $message) = @_; | ||
| 43 | my($asserter,$file,$line) = caller(1); | ||
| 44 | |||
| 45 | add_to_suites($asserter); | ||
| 46 | try { | ||
| 47 | $suites{$asserter}->assert($condition, $message); | ||
| 48 | } | ||
| 49 | catch Test::Unit::Exception with { | ||
| 50 | my $e = shift; | ||
| 51 | $e->throw_new( | ||
| 52 | -package => $asserter, | ||
| 53 | -file => $file, | ||
| 54 | -line => $line); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub create_suite { | ||
| 59 | my ($test_package_name) = @_; | ||
| 60 | $test_package_name = caller() unless defined($test_package_name); | ||
| 61 | add_to_suites($test_package_name); | ||
| 62 | |||
| 63 | no strict 'refs'; | ||
| 64 | |||
| 65 | my $set_up_func = sub {}; | ||
| 66 | my $tear_down_func = sub {}; | ||
| 67 | |||
| 68 | my $st = Devel::Symdump->new($test_package_name); | ||
| 69 | my @set_up_candidates = grep /::set_up$/, $st->functions; | ||
| 70 | $set_up_func = \&{$set_up_candidates[0]} if @set_up_candidates; | ||
| 71 | |||
| 72 | my @tear_down_candidates = grep /::tear_down$/, $st->functions; | ||
| 73 | $tear_down_func = \&{$tear_down_candidates[0]} if @tear_down_candidates; | ||
| 74 | |||
| 75 | for my $test_method (grep /::test[^:]*$/, $st->functions) { | ||
| 76 | my($method_name) = $test_method =~ /::(test[^:]*)/; | ||
| 77 | my $subref = \&{$test_method}; | ||
| 78 | my $test_case = Class::Inner->new | ||
| 79 | (parent => 'Test::Unit::TestCase', | ||
| 80 | methods => {set_up => $set_up_func, | ||
| 81 | tear_down => $tear_down_func, | ||
| 82 | $method_name => $subref, | ||
| 83 | }, | ||
| 84 | args => [$method_name],); | ||
| 85 | $suites{$test_package_name}->add_test($test_case); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub run_suite { | ||
| 90 | my ($test_package_name, $filehandle) = @_; | ||
| 91 | $test_package_name = caller() unless defined($test_package_name); | ||
| 92 | my $test_runner = Test::Unit::TestRunner->new($filehandle); | ||
| 93 | $test_runner->do_run($suites{$test_package_name}); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub add_suite { | ||
| 97 | my ($to_be_added, $to_add_to) = @_; | ||
| 98 | $to_add_to = caller() unless defined($to_add_to); | ||
| 99 | die "Error: no suite '$to_be_added'" unless exists $suites{$to_be_added}; | ||
| 100 | die "Error: no suite '$to_add_to'" unless exists $suites{$to_add_to}; | ||
| 101 | $suites{$to_add_to}->add_test($suites{$to_be_added}); | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 | __END__ | ||