In reformatting the code in the POD's SYNOPSIS section of a CPAN module I'm writing, it struck me that it's easy to accidentally break the code. Nice to have at least the code in the SYNOPSIS run! So I created a synopsis.t.

UPDATE: Fixed two bugs. synopsis.t no longer reports success if it can't open the POD file. And it no longer depends on the file being in a specific location.

enjoy, Jeffrey Kegler

use strict; use warnings; use English; use Test::More tests => 2; # Module specific stuff here -- setup code use Scalar::Util qw(weaken isweak); BEGIN { use_ok('Test::Weaken') }; package Module::Test_me1; sub new { bless [], (shift); } package Module::Test_me2; sub new { bless [], (shift); } package main; # slurp in the code my $filename = $INC{"Test/Weaken.pm"}; unless (open(CODE, $filename)) { fail("Cannot open $filename"); exit(1); } $RS = undef; my $code = <CODE>; # remove stuff before and after the SYNOPSIS $code =~ s/.*^=head1\s*SYNOPSIS\s*$//xms; $code =~ s/^=cut.*\z//xms; # remove POD text $code =~ s/^\S[^\n]*$//xmsg; # compute line count -- don't include whitespace lines $code =~ s/^\s*$//xmsg; my @lines = split(/\n/, $code); my $line_count = @lines; # check for absence of code if ($code =~ /\A\s*\z/xms) { fail("No code in synopsis"); exit(1); } # Try the code and see what happens eval $code; # Report the results if ($@) { fail("Synopsis code failed: $@"); } else { pass("Synopsis has $line_count lines of good code"); }

In reply to Test that a module's SYNOPSIS code really runs by Jeffrey Kegler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.