/usr/share/perl5/Test/Inline/Section.pm is in libtest-inline-perl 2.213-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 | package Test::Inline::Section;
=pod
=head1 NAME
Test::Inline::Section - Implements a section of tests
=head1 DESCRIPTION
This class implements a single section of tests. That is, a section of POD
beginning with C<=begin test> or C<=begin testing>.
=head2 Types of Sections
There are two types of code sections. The first, beginning with
C<=begin testing ...>, contains a set of tests and other code to be executed
at any time (within a set of specifyable constraints). The second, labelled
C<=begin testing SETUP>, contains code to be executed at the beginning of the
test script, before any of the other sections are executed. This allows
any needed variables or environment to be set up before the tests are run.
You can have more than one setup section, and they will be written to the
test file in order of appearance.
=head2 Test Section Header Syntax
Some examples of the different types of test headers are as follows.
# Normal anonymous test
=begin testing
ok( $foo == $bar, 'This is a test' );
=end testing
# A named test. Also provides the number of tests to run.
# Any test section can specify the number of tests.
=begin testing my_method 1
ok( $foo->my_method, '->my_method returns true' );
=end testing
# A named test with pre-requisites.
# Note that ONLY named tests can have pre-requisites
=begin testing this after my_method foo bar other_method Other::Class
ok( $foo->this, '->this returns true' );
=end testing
The first example shows a normal anonymous test. All anonymous test sections
are considered low priority, and we be run, in order of appearance, AFTER all
named tests have been run.
Any and all arguments used after "testing" must be in the form of simple
space separated words. The first word is considered the "name" of the test.
The intended use for these is generally to create one named test section for
each function or method, but you can name them as you please. Test names
B<must> be unique, and B<are> case sensitive.
After the name, you can provide the word "after" and provide a list of other
named tests that must be completed first in order to run this test. This is
provided so that when errors are encounted, they are probably the result of
this method or set of tests, and not in some other method that this one
relies on. It makes debugging a lot easier. The word after is only a
keyword when after the test name, so you can use a test name of after as well.
The following are both legal
=begin testing after after that
=begin testing this after after
The easiest and recommended way of labeling the tests is simple to name all
tests after their methods, and put as a pre-requisite any other methods that
the method you are testing calls. Test::Inline will take care of writing the
tests to the test script in the correct order. Please note you can NOT define
circular relationships in the prerequisites, or an error will occur.
If a number is provided as the last value, it will be taken to mean the
number of actual tests that will occur during the test section. While
preparing to write the test files, the processor will try to use these
to try to determine the number of files to write. If ALL test sections to
be written to a particular file have a test count, then the script will
use the total of these as a basic for providing Test::More with a plan.
If ANY test sections to be written to a file do not have a test count, the
test file with use C<no_plan>.
Finally, Test::Inline will try to be forgiving in it's parsing of the tests.
any missing prerequisites will be ignored. Also, as long as it does not
break a prerequisite, all named tests will be attempted to be run in their
order of appearance.
=head1 METHODS
=cut
use strict;
use List::Util ();
use Params::Util qw{_ARRAY};
use Algorithm::Dependency::Item ();
use vars qw{$VERSION @ISA $errstr};
BEGIN {
$VERSION = '2.213';
@ISA = 'Algorithm::Dependency::Item';
$errstr = '';
}
#####################################################################
# Constructor and Parsing
=pod
=head2 new
my $Section = Test::Inline::Section->new( $pod );
The C<new> constructor takes a string of POD, which must be a single section
of relevant pod ( preferably produced by L<Test::Inline::ExtractHandler> ),
and creates a new section object for it.
Returns a new C<Test::Inline::Section> object if passed POD in the form
C<=begin testing ...>. Returns C<undef> on error.
=cut
my $RE_begin = qr/=begin\s+(?:test|testing)/;
my $RE_example = qr/=for\s+example\s+begin/;
sub new {
$errstr = '';
my $class = shift;
my $pod = $_[0] =~ /^(?:$RE_begin|$RE_example)\b/ ? shift :
return $class->_error("Test section does not begin with =begin test[ing]");
my $context = shift;
# Split into lines
my @lines = split /(?:\015{1,2}\012|\015|\012)/, $pod;
# Handle =for example separately
if ( $pod =~ /^$RE_example\b/ ) {
return $class->_example( \@lines, $context );
}
# Get the begin paragraph ( yes, paragraph. NOT line )
my $begin = '';
while ( @lines and $lines[0] !~ /^\s*$/ ) {
$begin .= ' ' if $begin;
$begin .= shift @lines;
}
# Remove the trailing end tag
if ( @lines and $lines[-1] =~ /^=end\s+(?:test|testing)\b/o ) {
pop @lines;
}
# Do some cleaning up and checking
$class->_trim_empty_lines( \@lines );
$class->_check_nesting( \@lines, $begin ) or return undef;
# Create the basic object
my $self = bless {
begin => $begin,
content => join( '', map { "$_\n" } @lines ),
setup => '', # Is this a setup section
example => '', # Is this an example section
context => $context, # Package context
name => undef, # The name of the test
tests => undef, # undef means unknown test count
after => {}, # Other named methods this should be after
classes => {}, # Other classes this should be after
}, $class;
# Start processing the begin line
my @parts = split /\s+/, $begin;
# Remove the =begin
shift @parts;
# If the line contains a number then this is part of the tests
foreach my $i ( 0 .. $#parts ) {
next unless $parts[$i] =~ /^(0|[1-9]\d*)$/;
$self->{tests} = splice @parts, $i, 1;
last;
}
# Handle setup sections via =begin test setup or =begin testing SETUP
if ( @parts == 2 and $parts[0] eq 'test' and $parts[1] eq 'setup' ) {
$self->{setup} = 1;
}
if ( @parts >= 2 and $parts[0] eq 'testing' and $parts[1] eq 'SETUP' ) {
$self->{setup} = 1;
}
# Any other form of =begin test is not allowed
if ( $parts[0] eq 'test' and ! $self->{setup} ) {
# Unknown =begin test line
return $class->_error("Unsupported '=begin test' line '$begin'");
}
# Remove the "testing" word
shift @parts;
# If there are no remaining parts, we are anonymous,
# and can just return as is.
return $self unless @parts;
# Make sure all remaining parts are only words
if ( grep { ! /^[\w:]+$/ } @parts ) {
return $class->_error("Found something other than words: $begin");
}
# The first word is our name and must match the perl
# format for a method name.
if ( $self->{setup} ) {
shift @parts;
} else {
$self->{name} = shift @parts;
unless ( $self->{name} =~ /^[^\W\d]\w*$/ ) {
return $class->_error("'$self->{name}' is not a valid test name: $begin");
}
}
return $self unless @parts;
# The next word MUST be "after"
unless ( shift @parts eq 'after' ) {
return $class->_error("Word after test name is something other than 'after': $begin");
}
# The remaining words are our dependencies.
# Simple words chunks are method dependencies, and anything
# containing :: (including at the end) is a dependency on
# another module that should be part of the testing process.
foreach my $part ( @parts ) {
if ( $part =~ /^[^\W\d]\w*$/ ) {
if ( $self->setup ) {
return $class->_error("SETUP sections can only have class dependencies");
}
$self->{after}->{$part} = 1;
} elsif ( $part =~ /::/ ) {
$part =~ s/::$//; # Strip trailing ::
$self->{classes}->{$part} = 1;
} else {
return $class->_error("Unknown dependency '$part' in begin line: $begin");
}
}
$self;
}
# Handle the creation of example sections
sub _example {
my $class = shift;
my @lines = @{shift()};
my $context = shift;
# Get the begin paragraph ( yes, paragraph. NOT line )
my $begin = '';
while ( @lines and $lines[0] !~ /^\s*$/ ) {
$begin .= ' ' if $begin;
$begin .= shift @lines;
}
# Remove the trailing end tag
if ( @lines and $lines[-1] =~ /^=for\s+example\s+end\b/o ) {
pop @lines;
}
# Remove any leading and trailing empty lines
$class->_trim_empty_lines( \@lines );
$class->_check_nesting( \@lines, $begin ) or return undef;
# Create the basic object
my $self = bless {
begin => $begin,
content => join( '', map { "$_\n" } @lines ),
setup => '', # Is this a setup section
example => 1, # Is this an example section
context => $context, # Package context
name => undef, # Examples arn't named
tests => 1, # An example always consumes 1 test
after => {}, # Other named methods this should be after
classes => {}, # Other classes this should be after
}, $class;
$self;
}
sub _error {
$errstr = join ': ', @_;
undef;
}
sub _short {
my $either = shift;
my $string = shift;
chomp $string;
$string =~ s/\n/ /g;
if ( length($string) > 30 ) {
$string = substr($string, 27);
$string =~ s/\s+$//;
$string .= '...';
}
$string;
}
sub _check_nesting {
my ($class, $lines, $begin) = @_;
# In the remaining lines there shouldn't be any lines
# that look like a POD tag. If there is there is probably
# a nesting problem.
my $bad_line = List::Util::first { /^=\w+/ } @$lines;
if ( $bad_line ) {
$bad_line = $class->_short($bad_line);
$begin = $class->_short($begin);
return $class->_error(
"POD statement '$bad_line' illegally nested inside of section '$begin'"
);
}
1;
}
sub _trim_empty_lines {
my $lines = $_[1];
while ( @$lines and $lines->[0] eq '' ) { shift @$lines }
while ( @$lines and $lines->[-1] eq '' ) { pop @$lines }
1;
}
#####################################################################
# Main Methods
=pod
=head2 parse
my $SectionList = Test::Inline::Section( @elements );
Since version 1.50 L<Test::Inline> has been extracting package statements
so that as the sections are extracted, we can determine which sections
belong to which packages, and separate them accordingly.
The C<parse> method takes B<all> of the elements from a file, and returns
all of the Sections. By doing it here, we can track the package context
and set it in the Sections.
=cut
sub parse {
$errstr = '';
my $class = shift;
my $elements = _ARRAY(shift) or return undef;
my @Sections = ();
# Iterate over the elements and maintain package contexts
my $context = '';
foreach my $element ( @$elements ) {
if ( $element =~ /^package\s+([\w:']+)/ ) {
$context = $1;
next;
}
# Handle weird unexpected elements
unless ( $element =~ /^=/ ) {
return $class->_error("Unexpected element '$element'");
}
# Hand off to the Section constructor
my $Section = Test::Inline::Section->new( $element, $context ) or return undef;
push @Sections, $Section;
}
@Sections ? \@Sections : undef;
}
=pod
=head2 setup
my $run_first = $Section->setup;
The C<setup> accessor indicates that this section is a "setup" section,
to be run at the beginning of the generated test script.
Returns true if this is a setup section, false otherwise.
=cut
sub setup { $_[0]->{setup} }
=pod
=head2 example
my $just_compile = $Section->example;
The C<example> accessor indicates that this section is an "example"
section, to be compile-tested instead of run.
Returns true if this is an example section, false otherwise.
=cut
sub example { $_[0]->{example} }
=pod
=head2 context
The C<context> method returns the package context of the unit test section,
or false if the unit test section appeared out of context.
=cut
sub context { $_[0]->{context} }
=pod
=head2 name
The C<name> method returns the name of the test section,
or false if the test if anonymous.
=cut
sub name { defined $_[0]->{name} and $_[0]->{name} }
=pod
=head2 tests
The C<tests> method returns the number of Test::Builder-compatible
tests that will run within the test section. Returns C<undef> if the
number of tests is unknown.
=cut
sub tests { $_[0]->{tests} }
=pod
=head2 begin
For use mainly in debugging, the C<begin> method returns the literal string
of the begin line/paragraph.
=cut
sub begin { $_[0]->{begin} }
=pod
=head2 anonymous
my $is_anonymous = $Section->anonymous;
The C<anonymous> method returns true if the test section is an unnamed
anonymous section, or false if it is a named section or a setup section.
=cut
sub anonymous {
my $self = shift;
! (defined $self->{name} or $self->{setup});
}
=pod
=head2 after
my @names = $Section->after;
The C<after> method returns the list of other named tests that this
test section says it should be run after.
Returns a list of test name, or the null list C<()> if the test does
not have to run after any other named tests.
=cut
sub after {
keys %{$_[0]->{after}};
}
=pod
=head2 classes
my @classes = $Section->classes;
The C<classes> method returns the list of test classes that the test depends
on, and should be run before the tests. These values are used to determine the
set of class-level dependencies for the entire test file.
Returns a list of class names, or the null list C<()> if the test does
not have any class-level dependencies.
=cut
sub classes {
keys %{$_[0]->{classes}};
}
=pod
=head2 content
my $code = $Section->content;
The C<content> method returns the actual testing code contents of the
section, with the leading C<=begin> and trailing C<=end> removed.
Returns a string containing the code, or the null string C<""> if the
section was empty.
=cut
sub content { $_[0]->{content} }
#####################################################################
# Implementing the Algorithm::Dependency::Item interface
# The ->depends method we have works the same as for
# Algorithm::Dependency::Item already, so we just need to implement
# it's ->id method, which is the same as our ->name method
sub id { $_[0]->name }
sub depends { $_[0]->after }
1;
=pod
=head1 SUPPORT
See the main L<SUPPORT|Test::Inline/SUPPORT> section.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>, L<http://ali.as/>
=head1 COPYRIGHT
Copyright 2004 - 2013 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|