The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Pod::Tests - Extracts embedded tests and code examples from POD

SYNOPSIS

LOOK AT Pod::Tests::Tutorial FIRST!

  use Pod::Tests;
  $p = Pod::Tests->new;

  $p->parse_file($file);
  $p->parse_fh($fh);
  $p->parse(@code);

  my @examples = $p->examples;
  my @tests    = $p->tests;

  foreach my $example (@examples) {
      print "The example:  '$example->{code}' was on line ".
            "$example->{line}\n";
  }

  my @test_code         = $p->build_tests(@tests);
  my @example_test_code = $p->build_examples(@examples);

DESCRIPTION

LOOK AT Pod::Tests::Tutorial FIRST!

This is a specialized POD viewer to extract embedded tests and code examples from POD. It doesn't do much more than that. pod2test does the useful work.

Embedded Tests

Embedding tests allows tests to be placed near the code its testing. This is a nice supplement to the traditional .t files.

A test is denoted using either "=for testing" or a "=begin/end testing" block.

   =item B<is_pirate>

        @pirates = is_pirate(@arrrgs);

    Go through @arrrgs and return a list of pirates.

    =begin testing

    my @p = is_pirate('Blargbeard', 'Alfonse', 'Capt. Hampton', 'Wesley');
    ok(@p == 2);

    =end testing

    =cut

    sub is_pirate {
        ....
    }

Code Examples EXPERIMENTAL

BIG FAT WARNING perldoc and the various pod2* reformatters are inconsistant in how they deal with =also. Some warn, some display it, some choke. So consider this to be a Highly Experimental Feature. Also, this interface is EXPERIMENTAL AND SUBJECT TO CHANGE!

Code examples in documentation are rarely tested. Pod::Tests provides a way to do some testing of your examples without repeating them.

A code example is denoted using either "=also for example" or an "=also begin/end example" block.

The =also tag provides that examples will both be extracted and displayed as documentation.

    =also for example
    print "Here is a fine example of something or other.";

    =also begin example

    use LWP::Simple;
    getprint "http://www.goats.com";

    =also end example

Using a normal =for example or =begin/end example block lets you add code to your example that won't get displayed. This is nice when you only want to show a code fragment, yet still want to ensure things work.

    =for example
    sub mygrep (&@) { }

    =also for example
    mygrep { $_ eq 'bar' } @stuff

The mygrep() call would be a syntax error were the routine not declared with the proper prototype. Pod::Tests will consider both pieces to the part of the same example for the purposes of testing, but will only display the mygrep {...} line. You can also put =for example blocks afterwards.

You can also put a =for example testing afterwards:

    =also for example
      my $result = 2 + 2;

    =for example_testing
      ok( $result == 4,         'addition works' );

It will work like any other embedded test. In this case the code will actually be run.

Finally, since many examples print their output, we trap that into $_STDOUT_ and $_STDERR_ variables to capture prints and warnings.

    =also for example
      print "Hello, world!\n";
      warn  "Beware the Ides of March!\n";

    =for example_testing
      ok( $_STDOUT_ eq "Hello, world!\n" );
      ok( $_STDERR_ eq "Beware the Ides of March!\n" );

Remember, this is all EXPERIMENTAL and SUBJECT TO CHANGE!

Formatting

The code examples and embedded tests are not translated from POD, thus all the C<> and B<> style escapes are not valid. Its literal Perl code.

Parsing

After creating a Pod::Tests object, you parse the POD by calling one of the available parsing methods documented below. You can call parse as many times as you'd like, all examples and tests found will stack up inside the object.

Testing

Once extracted, the tests can be built into stand-alone testing code using the build_tests() and build_examples() methods. However, it is recommended that you first look at the pod2test program before embarking on this.

Methods

new
  $parser = Pod::Tests->new;

Returns a new Pod::Tests object which lets you read tests and examples out of a POD document.

parse
  $parser->parse(@code);

Finds the examples and tests in a bunch of lines of Perl @code. Once run they're available via examples() and testing().

parse_fh
parse_file
  $parser->parse_file($filename);
  $parser->parse_fh($fh);

Just like parse() except it works on a file or a filehandle, respectively.

examples
testing
  @examples = $parser->examples;
  @testing  = $parser->tests;

Returns the examples and tests found in the parsed POD documents. Each element of @examples and @testing is a hash representing an individual testing block and contains information about that block.

  $test->{code}         actual testing code
  $test->{line}         line from where the test was taken

NOTE In the future, these will become full-blown objects.

build_tests
  my @code = $p->build_tests(@tests);

Returns a code fragment based on the given embedded @tests. This fragment is expected to print the usual "ok/not ok" (or something Test::Harness can read) or nothing at all.

Typical usage might be:

    my @code = $p->build_tests($p->tests);

This fragment is suitable for placing into a larger test script.

NOTE Look at pod2test before embarking on your own test building.

build_examples
  my @code = $p->build_examples(@examples);

Similar to build_tests(), it creates a code fragment which tests the basic validity of your example code. Essentially, it just makes sure it compiles.

If your example has an "example testing" block associated with it it will run the the example code and the example testing block.

EXAMPLES

Here's the simplest example, just finding the tests and examples in a single module.

  my $p = Pod::Tests->new;
  $p->parse_file("path/to/Some.pm");

And one to find all the tests and examples in a directory of files. This illustrates building a set of examples and tests through multiple calls to parse_file().

  my $p = Pod::Tests->new;
  opendir(PODS, "path/to/some/lib/") || die $!;
  while( my $file = readdir PODS ) {
      $p->parse_file($file);
  }
  printf "Found %d examples and %d tests in path/to/some/lib\n",
         scalar $p->examples, scalar $p->tests;

Finally, an example of parsing your own POD using the DATA filehandle.

  use Fcntl qw(:seek);
  my $p = Pod::Tests->new;

  # Seek to the beginning of the current code.
  seek(DATA, 0, SEEK_SET) || die $!;
  $p->parse_fh(\*DATA);

AUTHOR

Michael G Schwern <schwern@pobox.com>

NOTES and CAVEATS

This module is currently EXPERIMENTAL and only for use by pod2test. If you use it, the interface will change from out from under you.

It currently does not use Pod::Parser. I couldn't figure out how to use it. Instead, I use a simple, home-rolled parser. This will eventually be split out as Pod::Parser::Simple.

SEE ALSO

pod2test, Perl 6 RFC 183 http://dev.perl.org/rfc183.pod

Similar schemes can be found in SelfTest and Test::Unit.