| 1 | package Badger::Test; | ||
| 2 | |||
| 3 | use Carp; | ||
| 4 | use Badger; | ||
| 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 | map { $_ => \&_export_hook } | ||
| 21 | qw( summary colour color args tests ) | ||
| 22 | }, | ||
| 23 | }; | ||
| 24 | |||
| 25 | use Badger::Debug; | ||
| 26 | use Badger::Exception; | ||
| 27 | use Badger::Test::Manager; | ||
| 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 | Badger->lib($_[3]); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _skip_hook { | ||
| 41 | $MANAGER->skip_all($_[3]); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _if_env_hook { | ||
| 45 | my $vars = $_[3]; | ||
| 46 | $IF_ENV = $vars eq ARRAY | ||
| 47 | ? $vars | ||
| 48 | : [ split(DELIMITER, $vars) ] | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _export_hook { | ||
| 52 | my ($class, $target, $key, $symbols) = @_; | ||
| 53 | croak "You didn't specify a value for the '$key' load option" | ||
| 54 | unless @$symbols; | ||
| 55 | $class->$key(shift @$symbols); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _debug_hook { | ||
| 59 | my ($class, $target, $key, $symbols, $import) = @_; | ||
| 60 | croak "You didn't specify any values for the 'debug' load option.\n" | ||
| 61 | unless @$symbols; | ||
| 62 | |||
| 63 | |||
| 64 | # define $DEBUG in caller | ||
| 65 | no strict 'refs'; | ||
| 66 | *{ $target.PKG.DEBUG } = \$DEBUG; | ||
| 67 | *{ $target.PKG.DEBUG } = sub { $DEBUG }; | ||
| 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 | my $modules = shift @$symbols; | ||
| 72 | return unless $modules; # zero/false for no debugging | ||
| 73 | $class->debug_modules($modules); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _after_hook { | ||
| 77 | 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 | if ($IF_ENV && ! $ALL) { | ||
| 84 | my $run = 0; | ||
| 85 | my @names = @$IF_ENV; | ||
| 86 | |||
| 87 | foreach my $var (@names) { | ||
| 88 | $run++, last if $ENV{$var}; | ||
| 89 | } | ||
| 90 | unless ($run) { | ||
| 91 | my $name = pop(@names); | ||
| 92 | $name = join(' or ', join(', ', @names), $name); | ||
| 93 | $MANAGER->skip_all("Tests only apply for $name"); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub manager { | ||
| 99 | my $class = shift; | ||
| 100 | return @_ | ||
| 101 | ? ($MANAGER = shift) | ||
| 102 | : $MANAGER; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub colour { | ||
| 106 | shift; | ||
| 107 | manager->colour(@_); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub summary { | ||
| 111 | shift; | ||
| 112 | manager->summary(@_); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub args { | ||
| 116 | my $self = shift; | ||
| 117 | my $args = @_ && ref $_[0] eq ARRAY ? shift : [ @_ ]; | ||
| 118 | my $arg; | ||
| 119 | |||
| 120 | # quick hack until Badger::Config is done | ||
| 121 | while (@$args && $args->[0] =~ /^-/) { | ||
| 122 | $arg = shift @$args; | ||
| 123 | if ($arg =~ /^(-c|--colou?r)$/) { | ||
| 124 | $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 | $self->debugging( $DEBUG = 1 ); | ||
| 130 | } | ||
| 131 | elsif ($arg =~ /^(-s|--summary)$/) { | ||
| 132 | $self->summary(1); | ||
| 133 | } | ||
| 134 | elsif ($arg =~ /^(-t|--trace)$/) { | ||
| 135 | $self->trace(1); | ||
| 136 | } | ||
| 137 | elsif ($arg =~ /^(-a|--all)$/) { | ||
| 138 | $self->all(1); | ||
| 139 | } | ||
| 140 | elsif ($arg =~ /^(-h|--help)$/) { | ||
| 141 | warn $self->help; | ||
| 142 | exit; | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | unshift(@$args, $arg); | ||
| 146 | last; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | sub tests { | ||
| 152 | shift; | ||
| 153 | plan(@_); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub debug_modules { | ||
| 157 | my $self = shift; | ||
| 158 | $self->class->var( DEBUG_MODULES => shift ); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub debugging { | ||
| 162 | my $self = shift; | ||
| 163 | my $flag = $DEBUG = (@_ ? shift : 1); | ||
| 164 | my $modules = $self->class->var(DEBUG_MODULES) || return; | ||
| 165 | $DEBUGGER->debug_modules($modules); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub trace { | ||
| 169 | my $self = shift; | ||
| 170 | my $flag = @_ ? shift : 1; | ||
| 171 | $EXCEPTION->trace($flag); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub all { | ||
| 175 | my $self = shift; | ||
| 176 | $ALL = @_ ? shift : 1; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub help { | ||
| 180 | return <<END_OF_HELP; | ||
| 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 | plan => sub ($;$) { manager->plan(@_) }, | ||
| 194 | ok => sub ($;$) { manager->ok(@_) }, | ||
| 195 | is => sub ($$;$) { manager->is(@_) }, | ||
| 196 | isnt => sub ($$;$) { manager->isnt(@_) }, | ||
| 197 | like => sub ($$;$) { manager->like(@_) }, | ||
| 198 | unlike => sub ($$;$) { manager->unlike(@_) }, | ||
| 199 | pass => sub (;$) { manager->pass(@_) }, | ||
| 200 | fail => sub (;$) { manager->fail(@_) }, | ||
| 201 | skip => sub (;$) { manager->skip(@_) }, | ||
| 202 | skip_some => sub (;$$) { manager->skip_some(@_) }, | ||
| 203 | skip_rest => sub (;$) { manager->skip_rest(@_) }, | ||
| 204 | skip_all => sub (;$) { manager->skip_all(@_) }, | ||
| 205 | ); | ||
| 206 | |||
| 207 | |||
| 208 | 1; | ||
| 209 | |||
| 210 | __END__ |