| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Badger::Test; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 70 |  |  | 70 |  | 71862 | use Carp; | 
|  | 70 |  |  |  |  | 330 |  | 
|  | 70 |  |  |  |  | 4984 |  | 
| 4 | 70 |  |  | 70 |  | 19594 | use Badger; | 
|  | 70 |  |  |  |  | 169 |  | 
|  | 70 |  |  |  |  | 531 |  | 
| 5 |  |  |  |  |  |  | use Badger::Class | 
| 6 |  |  |  |  |  |  | version   => 0.01, | 
| 7 |  |  |  |  |  |  | base      => 'Badger::Base', | 
| 8 |  |  |  |  |  |  | import    => 'CLASS class', | 
| 9 |  |  |  |  |  |  | constants => 'ARRAY DELIMITER PKG', | 
| 10 |  |  |  |  |  |  | words     => 'DEBUG DEBUG_MODULES', | 
| 11 |  |  |  |  |  |  | exports   => { | 
| 12 |  |  |  |  |  |  | all   => 'plan ok is isnt like unlike pass fail | 
| 13 |  |  |  |  |  |  | skip_some skip_rest skip_all manager', | 
| 14 |  |  |  |  |  |  | after => \&_after_hook, | 
| 15 |  |  |  |  |  |  | hooks => { | 
| 16 |  |  |  |  |  |  | lib      => [\&_lib_hook,    1], | 
| 17 |  |  |  |  |  |  | skip     => [\&_skip_hook,   1], | 
| 18 |  |  |  |  |  |  | if_env   => [\&_if_env_hook, 1], | 
| 19 |  |  |  |  |  |  | debug    => \&_debug_hook, | 
| 20 | 70 |  |  |  |  | 444 | map { $_ => \&_export_hook } | 
|  | 350 |  |  |  |  | 1256 |  | 
| 21 |  |  |  |  |  |  | qw( summary colour color args tests ) | 
| 22 |  |  |  |  |  |  | }, | 
| 23 | 70 |  |  | 70 |  | 495 | }; | 
|  | 70 |  |  |  |  | 166 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 70 |  |  | 70 |  | 513 | use Badger::Debug; | 
|  | 70 |  |  |  |  | 181 |  | 
|  | 70 |  |  |  |  | 386 |  | 
| 26 | 70 |  |  | 70 |  | 443 | use Badger::Exception; | 
|  | 70 |  |  |  |  | 120 |  | 
|  | 70 |  |  |  |  | 844 |  | 
| 27 | 70 |  |  | 70 |  | 30446 | use Badger::Test::Manager; | 
|  | 70 |  |  |  |  | 2881 |  | 
|  | 70 |  |  |  |  | 16151 |  | 
| 28 |  |  |  |  |  |  | our $MANAGER   = 'Badger::Test::Manager'; | 
| 29 |  |  |  |  |  |  | our $DEBUGGER  = 'Badger::Debug'; | 
| 30 |  |  |  |  |  |  | our $EXCEPTION = 'Badger::Exception'; | 
| 31 |  |  |  |  |  |  | our ($ALL, $IF_ENV, $DEBUG, $DEBUG_MODULES); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | *color = \&colour; | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub _lib_hook { | 
| 37 | 3 |  |  | 3 |  | 16 | Badger->lib($_[3]); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub _skip_hook { | 
| 41 | 3 |  |  | 3 |  | 9 | $MANAGER->skip_all($_[3]); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub _if_env_hook { | 
| 45 | 2 |  |  | 2 |  | 4 | my $vars = $_[3]; | 
| 46 | 2 | 50 |  |  |  | 17 | $IF_ENV = $vars eq ARRAY | 
| 47 |  |  |  |  |  |  | ? $vars | 
| 48 |  |  |  |  |  |  | : [ split(DELIMITER, $vars) ] | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub _export_hook { | 
| 52 | 126 |  |  | 126 |  | 281 | my ($class, $target, $key, $symbols) = @_; | 
| 53 | 126 | 50 |  |  |  | 285 | croak "You didn't specify a value for the '$key' load option" | 
| 54 |  |  |  |  |  |  | unless @$symbols; | 
| 55 | 126 |  |  |  |  | 441 | $class->$key(shift @$symbols); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub _debug_hook { | 
| 59 | 64 |  |  | 64 |  | 184 | my ($class, $target, $key, $symbols, $import) = @_; | 
| 60 | 64 | 50 |  |  |  | 242 | croak "You didn't specify any values for the 'debug' load option.\n" | 
| 61 |  |  |  |  |  |  | unless @$symbols; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | # define $DEBUG in caller | 
| 65 | 70 |  |  | 70 |  | 661 | no strict 'refs'; | 
|  | 70 |  |  |  |  | 252 |  | 
|  | 70 |  |  |  |  | 74620 |  | 
| 66 | 64 |  |  |  |  | 140 | *{ $target.PKG.DEBUG } = \$DEBUG; | 
|  | 64 |  |  |  |  | 374 |  | 
| 67 | 64 |  |  | 0 |  | 253 | *{ $target.PKG.DEBUG } = sub { $DEBUG }; | 
|  | 64 |  |  |  |  | 207 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | # set $DEBUG_MODULE in this class to contain the argument passed - a list | 
| 70 |  |  |  |  |  |  | # of class names to enable $DEBUG in when/if debugging is enabled | 
| 71 | 64 |  |  |  |  | 152 | my $modules = shift @$symbols; | 
| 72 | 64 | 50 |  |  |  | 203 | return unless $modules;           # zero/false for no debugging | 
| 73 | 64 |  |  |  |  | 227 | $class->debug_modules($modules); | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | sub _after_hook { | 
| 77 | 98 |  |  | 98 |  | 239 | my ($class, $target) = @_; | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # See if we've got any constraints specified and assert that they're | 
| 80 |  |  |  |  |  |  | # met.  If the --all command line parameter is specified (which sets | 
| 81 |  |  |  |  |  |  | # $ALL) then we run the tests regardless | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 98 | 100 | 66 |  |  | 393 | if ($IF_ENV && ! $ALL) { | 
| 84 | 2 |  |  |  |  | 3 | my $run   = 0; | 
| 85 | 2 |  |  |  |  | 11 | my @names = @$IF_ENV; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 2 |  |  |  |  | 4 | foreach my $var (@names) { | 
| 88 | 3 | 100 |  |  |  | 20 | $run++, last if $ENV{$var}; | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 2 | 100 |  |  |  | 7 | unless ($run) { | 
| 91 | 1 |  |  |  |  | 1 | my $name = pop(@names); | 
| 92 | 1 |  |  |  |  | 3 | $name = join(' or ', join(', ', @names), $name); | 
| 93 | 1 |  |  |  |  | 5 | $MANAGER->skip_all("Tests only apply for $name"); | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub manager { | 
| 99 | 1795 |  |  | 1795 | 1 | 1889 | my $class = shift; | 
| 100 |  |  |  |  |  |  | return @_ | 
| 101 | 1795 | 50 |  |  |  | 5744 | ? ($MANAGER = shift) | 
| 102 |  |  |  |  |  |  | :  $MANAGER; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub colour { | 
| 106 | 0 |  |  | 0 | 1 | 0 | shift; | 
| 107 | 0 |  |  |  |  | 0 | manager->colour(@_); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub summary { | 
| 111 | 0 |  |  | 0 | 1 | 0 | shift; | 
| 112 | 0 |  |  |  |  | 0 | manager->summary(@_); | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub args { | 
| 116 | 67 |  |  | 67 | 1 | 147 | my $self = shift; | 
| 117 | 67 | 50 | 33 |  |  | 509 | my $args = @_ && ref $_[0] eq ARRAY ? shift : [ @_ ]; | 
| 118 | 67 |  |  |  |  | 120 | my $arg; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | # quick hack until Badger::Config is done | 
| 121 | 67 |  | 33 |  |  | 514 | while (@$args && $args->[0] =~ /^-/) { | 
| 122 | 0 |  |  |  |  | 0 | $arg =  shift @$args; | 
| 123 | 0 | 0 |  |  |  | 0 | if ($arg =~ /^(-c|--colou?r)$/) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 124 | 0 |  |  |  |  | 0 | $self->colour(1); | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  | elsif ($arg =~ /^(-d|--debug)$/) { | 
| 127 |  |  |  |  |  |  | # physically set $DEBUG in this package (required for exported | 
| 128 |  |  |  |  |  |  | # aliases) and also call debugging() for any subclasses to use | 
| 129 | 0 |  |  |  |  | 0 | $self->debugging( $DEBUG = 1 ); | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  | elsif ($arg =~ /^(-s|--summary)$/) { | 
| 132 | 0 |  |  |  |  | 0 | $self->summary(1); | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | elsif ($arg =~ /^(-t|--trace)$/) { | 
| 135 | 0 |  |  |  |  | 0 | $self->trace(1); | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  | elsif ($arg =~ /^(-a|--all)$/) { | 
| 138 | 0 |  |  |  |  | 0 | $self->all(1); | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  | elsif ($arg =~ /^(-h|--help)$/) { | 
| 141 | 0 |  |  |  |  | 0 | warn $self->help; | 
| 142 | 0 |  |  |  |  | 0 | exit; | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  | else { | 
| 145 | 0 |  |  |  |  | 0 | unshift(@$args, $arg); | 
| 146 | 0 |  |  |  |  | 0 | last; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | sub tests { | 
| 152 | 59 |  |  | 59 | 1 | 96 | shift; | 
| 153 | 59 |  |  |  |  | 205 | plan(@_); | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | sub debug_modules { | 
| 157 | 64 |  |  | 64 | 1 | 151 | my $self = shift; | 
| 158 | 64 |  |  |  |  | 249 | $self->class->var( DEBUG_MODULES => shift ); | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | sub debugging { | 
| 162 | 0 |  |  | 0 | 1 | 0 | my $self    = shift; | 
| 163 | 0 | 0 |  |  |  | 0 | my $flag    = $DEBUG = (@_ ? shift : 1); | 
| 164 | 0 |  | 0 |  |  | 0 | my $modules = $self->class->var(DEBUG_MODULES) || return; | 
| 165 | 0 |  |  |  |  | 0 | $DEBUGGER->debug_modules($modules); | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | sub trace { | 
| 169 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 170 | 0 | 0 |  |  |  | 0 | my $flag = @_ ? shift : 1; | 
| 171 | 0 |  |  |  |  | 0 | $EXCEPTION->trace($flag); | 
| 172 |  |  |  |  |  |  | } | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | sub all { | 
| 175 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 176 | 0 | 0 |  |  |  | 0 | $ALL = @_ ? shift : 1; | 
| 177 |  |  |  |  |  |  | } | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | sub help { | 
| 180 | 0 |  |  | 0 | 1 | 0 | return < | 
| 181 |  |  |  |  |  |  | Options: | 
| 182 |  |  |  |  |  |  | -a      --all               Run all tests (e.g. author/release tests) | 
| 183 |  |  |  |  |  |  | -d      --debug             Enable debugging | 
| 184 |  |  |  |  |  |  | -t      --trace             Enable stack tracing | 
| 185 |  |  |  |  |  |  | -c      --colour/--color    Enable colour output | 
| 186 |  |  |  |  |  |  | -s      --summary           Display summary of test results | 
| 187 |  |  |  |  |  |  | -h      --help              This help summary | 
| 188 |  |  |  |  |  |  | END_OF_HELP | 
| 189 |  |  |  |  |  |  | } | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | class->methods( | 
| 193 | 62 |  |  | 62 |  | 234 | plan      => sub ($;$)  { manager->plan(@_)      }, | 
| 194 | 508 |  |  | 508 |  | 12446 | ok        => sub ($;$)  { manager->ok(@_)        }, | 
| 195 | 1151 |  |  | 1151 |  | 5866 | is        => sub ($$;$) { manager->is(@_)        }, | 
| 196 | 5 |  |  | 5 |  | 19 | isnt      => sub ($$;$) { manager->isnt(@_)      }, | 
| 197 | 41 |  |  | 41 |  | 714 | like      => sub ($$;$) { manager->like(@_)      }, | 
| 198 | 2 |  |  | 2 |  | 13 | unlike    => sub ($$;$) { manager->unlike(@_)    }, | 
| 199 | 22 |  |  | 22 |  | 150 | pass      => sub (;$)   { manager->pass(@_)      }, | 
| 200 | 0 |  |  | 0 |  | 0 | fail      => sub (;$)   { manager->fail(@_)      }, | 
| 201 | 0 |  |  | 0 |  | 0 | skip      => sub (;$)   { manager->skip(@_)      }, | 
| 202 | 0 |  |  | 0 |  | 0 | skip_some => sub (;$$)  { manager->skip_some(@_) }, | 
| 203 | 0 |  |  | 0 |  | 0 | skip_rest => sub (;$)   { manager->skip_rest(@_) }, | 
| 204 | 4 |  |  | 4 |  | 275 | skip_all  => sub (;$)   { manager->skip_all(@_)  }, | 
| 205 |  |  |  |  |  |  | ); | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | 1; | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | __END__ |