1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
#!/usr/bin/perl -wT
use strict;
use warnings;
use lib 't/lib';
use Test::More tests => 227;
use TAP::Parser::ResultFactory;
use TAP::Parser::Result;
use constant RESULT => 'TAP::Parser::Result';
use constant PLAN => 'TAP::Parser::Result::Plan';
use constant TEST => 'TAP::Parser::Result::Test';
use constant COMMENT => 'TAP::Parser::Result::Comment';
use constant BAILOUT => 'TAP::Parser::Result::Bailout';
use constant UNKNOWN => 'TAP::Parser::Result::Unknown';
my $warning;
$SIG{__WARN__} = sub { $warning = shift };
#
# Note that the are basic unit tests. More comprehensive path coverage is
# found in the regression tests.
#
my $factory = TAP::Parser::ResultFactory->new;
my %inherited_methods = (
is_plan => '',
is_test => '',
is_comment => '',
is_bailout => '',
is_unknown => '',
is_ok => 1,
);
my $abstract_class = bless { type => 'no_such_type' },
RESULT; # you didn't see this
run_method_tests( $abstract_class, {} ); # check the defaults
can_ok $abstract_class, 'type';
is $abstract_class->type, 'no_such_type',
'... and &type should return the correct result';
can_ok $abstract_class, 'passed';
$warning = '';
ok $abstract_class->passed, '... and it should default to true';
like $warning, qr/^\Qpassed() is deprecated. Please use "is_ok()"/,
'... but it should emit a deprecation warning';
can_ok RESULT, 'new';
can_ok $factory, 'make_result';
eval { $factory->make_result( { type => 'no_such_type' } ) };
ok my $error = $@, '... and calling it with an unknown class should fail';
like $error, qr/^Could not determine class for.*no_such_type/s,
'... with an appropriate error message';
# register new Result types:
can_ok $factory, 'class_for';
can_ok $factory, 'register_type';
{
package MyResult;
use strict;
use warnings;
our $VERSION;
use base 'TAP::Parser::Result';
TAP::Parser::ResultFactory->register_type( 'my_type' => __PACKAGE__ );
}
{
my $r = eval { $factory->make_result( { type => 'my_type' } ) };
my $error = $@;
isa_ok( $r, 'MyResult', 'register custom type' );
ok( !$error, '... and no error' );
}
#
# test unknown tokens
#
run_tests(
{ class => UNKNOWN,
data => {
type => 'unknown',
raw => '... this line is junk ... ',
},
},
{ is_unknown => 1,
raw => '... this line is junk ... ',
as_string => '... this line is junk ... ',
type => 'unknown',
has_directive => '',
}
);
#
# test comment tokens
#
run_tests(
{ class => COMMENT,
data => {
type => 'comment',
raw => '# this is a comment',
comment => 'this is a comment',
},
},
{ is_comment => 1,
raw => '# this is a comment',
as_string => '# this is a comment',
comment => 'this is a comment',
type => 'comment',
has_directive => '',
}
);
#
# test bailout tokens
#
run_tests(
{ class => BAILOUT,
data => {
type => 'bailout',
raw => 'Bailout! This blows!',
bailout => 'This blows!',
},
},
{ is_bailout => 1,
raw => 'Bailout! This blows!',
as_string => 'This blows!',
type => 'bailout',
has_directive => '',
}
);
#
# test plan tokens
#
run_tests(
{ class => PLAN,
data => {
type => 'plan',
raw => '1..20',
tests_planned => 20,
directive => '',
explanation => '',
},
},
{ is_plan => 1,
raw => '1..20',
tests_planned => 20,
directive => '',
explanation => '',
has_directive => '',
}
);
run_tests(
{ class => PLAN,
data => {
type => 'plan',
raw => '1..0 # SKIP help me, Rhonda!',
tests_planned => 0,
directive => 'SKIP',
explanation => 'help me, Rhonda!',
},
},
{ is_plan => 1,
raw => '1..0 # SKIP help me, Rhonda!',
tests_planned => 0,
directive => 'SKIP',
explanation => 'help me, Rhonda!',
has_directive => 1,
}
);
#
# test 'test' tokens
#
my $test = run_tests(
{ class => TEST,
data => {
ok => 'ok',
test_num => 5,
description => '... and this test is fine',
directive => '',
explanation => '',
raw => 'ok 5 and this test is fine',
type => 'test',
},
},
{ is_test => 1,
type => 'test',
ok => 'ok',
number => 5,
description => '... and this test is fine',
directive => '',
explanation => '',
is_ok => 1,
is_actual_ok => 1,
todo_passed => '',
has_skip => '',
has_todo => '',
as_string => 'ok 5 ... and this test is fine',
is_unplanned => '',
has_directive => '',
}
);
can_ok $test, 'actual_passed';
$warning = '';
is $test->actual_passed, $test->is_actual_ok,
'... and it should return the correct value';
like $warning,
qr/^\Qactual_passed() is deprecated. Please use "is_actual_ok()"/,
'... but issue a deprecation warning';
can_ok $test, 'todo_failed';
$warning = '';
is $test->todo_failed, $test->todo_passed,
'... and it should return the correct value';
like $warning,
qr/^\Qtodo_failed() is deprecated. Please use "todo_passed()"/,
'... but issue a deprecation warning';
# TODO directive
$test = run_tests(
{ class => TEST,
data => {
ok => 'not ok',
test_num => 5,
description => '... and this test is fine',
directive => 'TODO',
explanation => 'why not?',
raw => 'not ok 5 and this test is fine # TODO why not?',
type => 'test',
},
},
{ is_test => 1,
type => 'test',
ok => 'not ok',
number => 5,
description => '... and this test is fine',
directive => 'TODO',
explanation => 'why not?',
is_ok => 1,
is_actual_ok => '',
todo_passed => '',
has_skip => '',
has_todo => 1,
as_string =>
'not ok 5 ... and this test is fine # TODO why not?',
is_unplanned => '',
has_directive => 1,
}
);
sub run_tests {
my ( $instantiated, $value_for ) = @_;
my $result = instantiate($instantiated);
run_method_tests( $result, $value_for );
return $result;
}
sub instantiate {
my $instantiated = shift;
my $class = $instantiated->{class};
ok my $result = $factory->make_result( $instantiated->{data} ),
'Creating $class results should succeed';
isa_ok $result, $class, '.. and the object it returns';
return $result;
}
sub run_method_tests {
my ( $result, $value_for ) = @_;
while ( my ( $method, $default ) = each %inherited_methods ) {
can_ok $result, $method;
if ( defined( my $value = delete $value_for->{$method} ) ) {
is $result->$method(), $value,
"... and $method should be correct";
}
else {
is $result->$method(), $default,
"... and $method default should be correct";
}
}
while ( my ( $method, $value ) = each %$value_for ) {
can_ok $result, $method;
is $result->$method(), $value, "... and $method should be correct";
}
}
|