package Module::Install::StandardTests;

use warnings;
use strict;
use File::Spec;

use base 'Module::Install::Base';

our $VERSION = '0.05';

sub use_standard_tests {
    my ($self, %specs) = @_;
    my %with = map { $_ => 1 } qw/compile pod pod_coverage perl_critic/;
    if (exists $specs{without}) {
        $specs{without} = [ $specs{without} ] unless ref $specs{without};
        delete $with{$_} for @{ $specs{without} };


    # Unlike other tests, this is mandatory.

    $self->write_standard_test_compile;    # no if; this is mandatory
    $self->write_standard_test_pod          if $with{pod};
    $self->write_standard_test_pod_coverage if $with{pod_coverage};
    $self->write_standard_test_perl_critic  if $with{perl_critic};

sub write_test_file {
    my ($self, $filename, $code) = @_;
    $filename = File::Spec->catfile('t', $filename);

    # Outdent the code somewhat. Remove first empty line, if any. Then
    # determine the indent of the first line. Throw that amount of indenting
    # away from any line. This allows you to indent the code so it's visually
    # clearer (see methods below) while creating output that's indented more
    # or less correctly. Smoke result HTML pages link to the .t files, so it
    # looks neater.

    $code =~ s/^ *\n//;
    (my $indent = $code) =~ s/^( *).*/$1/s;
    $code =~ s/^$indent//gm;

    print "Creating $filename\n";
    open(my $fh, ">$filename") or die "can't create $filename $!";

    my $perl = $^X;
    print $fh <<TEST;
#!$perl -w

use strict;
use warnings;


    close $fh or die "can't close $filename $!\n";

sub write_standard_test_compile {
    my $self = shift;
    $self->write_test_file('000_standard__compile.t', q/
        BEGIN {
            use Test::More;
            eval "use Test::Compile";
                "Test::Compile required for testing compilation") if $@;

sub write_standard_test_pod {
    my $self = shift;
    $self->write_test_file('000_standard__pod.t', q/
        use Test::More;
        eval "use Test::Pod";
        plan skip_all => "Test::Pod required for testing POD" if $@;

sub write_standard_test_pod_coverage {
    my $self = shift;
    $self->write_test_file('000_standard__pod_coverage.t', q/
        use Test::More;
        eval "use Test::Pod::Coverage";
        plan skip_all =>
            "Test::Pod::Coverage required for testing POD coverage" if $@;

sub write_standard_test_perl_critic {
    my $self = shift;
    $self->write_test_file('000_standard__perl_critic.t', q/
        use FindBin '$Bin';
        use File::Spec;
        use UNIVERSAL::require;
        use Test::More;

        plan skip_all =>
            'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.'
            unless $ENV{TEST_AUTHOR};

        my %opt;
        my $rc_file = File::Spec->catfile($Bin, 'perlcriticrc');
        $opt{'-profile'} = $rc_file if -r $rc_file;

        if (Perl::Critic->require('1.078') &&
            Test::Perl::Critic->require &&
            Test::Perl::Critic->import(%opt)) {

        } else {
            plan skip_all => $@;



=head1 NAME

Module::Install::StandardTests - generate standard tests for installation


  use inc::Module::Install;
  name 'Class-Null';
  all_from 'lib/Class/';


Writes a few standard test files to the test directory C<t/>.


=over 4

=item use_standard_tests

  use_standard_tests(without => 'pod_coverage');
  use_standard_tests(without => [ qw/pod_coverage perl_critic/ ]);

Adds a few requirements to the build process, then simply calls the
C<write_standard_test_*> methods one after the other.

If you pass a named argument called C<without>, the the tests corresponding to
the value (as a string) or values (as an array reference) are omitted. Possible values are:

=over 4

=item compile

=item pod

=item pod_coverage

=item perl_critic


=item write_standard_test_compile

Writes the C<t/000_standard__compile.t> file, which uses L<Test::Compile> to
check that all perl module files compile. If L<Test::Compile> is not
available, the tests are skipped.

=item write_standard_test_perl_critic

Writes the C<t/000_standard__perl_critic.t> file, which uses
L<Test::Perl::Critic> to criticise Perl source code for best practices. If
L<Test::Perl::Critic> is not available, the tests are skipped.

If there is a C<t/perlcriticrc> file, it is used as the Perl::Critic

=item write_standard_test_pod

Writes the C<t/000_standard__pod.t> file, which uses L<Test::Pod> to check for
POD errors in files. If L<Test::Pod> is not available, the tests are skipped.

=item write_standard_test_pod_coverage

Writes the C<t/000_standard__pod_coverage.t> file, which uses
L<Test::Pod::Coverage> to check for POD coverage in the distribution. If
L<Test::Pod::Coverage> is not available, the tests are skipped.

=item write_test_file($filename, $code)

  $self->write_test_file('000_standard__perl_critic.t', q/.../);

Writes the code into the specified file inside the C<t/> directory. The
shebang line, together with C<use warnings;> and C<use strict;> are prepended
to the code.



No bugs have been reported.

Please report any bugs or feature requests to
C<>, or through the web interface
at L<>.


See perlmodinstall for information and options on installing Perl modules.


The latest version of this module is available from the Comprehensive Perl
Archive Network (CPAN). Visit <> to find a CPAN
site near you. Or see <>.

=head1 AUTHOR

Marcel GrE<uuml>nauer, C<< <> >>


Copyright 2007 by Marcel GrE<uuml>nauer

This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.