Test::Able::Runner::Role::Meta::Class - metaclass role for test runners


Test-Able-Runner documentation Contained in the Test-Able-Runner distribution.

Index


Code Index:

NAME

Top

Test::Able::Runner::Role::Meta::Class - metaclass role for test runners

VERSION

Top

version 1.001

DESCRIPTION

Top

This class provides the real guts for loading the test objects to run. However, you probably don't need to use it directly unless you are doing something fancy. See Test::Able::Runner for the usual cases.

ATTRIBUTES

Top

base_package

This is set by the -base_package option sent to use_test_packages. A has_base_package predicate will tell you if this has been set.

test_packages

This is set by the -test_packages option sent to use_test_packages. A has_test_packages predicate will tell you if this has been set.

test_path

This is set by teh -test_path option sent to use_test_packages.

METHODS

Top

test_classes

This returns all the packages that will be loaded for testing. This does not filter classes out that have $NOT_A_TEST set.

This will search for test classes if base_package has been set or it return the contents of test_packages.

build_test_objects

This method returns all the test objects that should be run by this runner. It works by doing the following:

1

It retrieves a list of potential test classes using test_classes.

2

It checks each package and throws away those with a package global variable named $NOT_A_TEST that has been set to a true value.

3

It instantiates the test classes and returns an arrayref of those test objects.

setup_test_objects

Calls build_test_objects and sets the test_objects accessor from Test::Able::Role::Meta::Class.

AUTHOR

Top

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Top


Test-Able-Runner documentation Contained in the Test-Able-Runner distribution.
package Test::Able::Runner::Role::Meta::Class;
our $VERSION = '1.001';
use Moose::Role;

has base_package => (
    is        => 'rw',
    isa       => 'ArrayRef[Str] | Str',
    predicate => 'has_base_package',
);

has test_packages => (
    is        => 'rw',
    isa       => 'ArrayRef[Str]',
    predicate => 'has_test_packages',
);

has test_path => (
    is        => 'rw',
    isa       => 'ArrayRef[Str] | Str | Undef ',
    default   => sub { 't/lib' },
);

sub test_classes {
    my $meta = shift;

    # Use Module::Pluggable to find the test classes
    if ($meta->has_base_package) {
        my $base_package = $meta->base_package;
        $meta->search_path( 
            new => (ref $base_package ? @$base_package : $base_package) 
        );
        return $meta->test_modules;
    }

    # Use the exact list given
    elsif ($meta->has_test_packages) {
        return @{ $meta->test_packages };
    }

    # Probably shouldn't happen...
    return ();
}

sub build_test_objects {
    my $meta = shift;

    # Insert our test paths into the front of the @INC search path
    if (defined $meta->test_path) {
        my $test_path = $meta->test_path;
        unshift @INC, (ref $test_path ? @$test_path : $test_path);
    }

    # Load all the test objects
    my @test_objects;
    PACKAGE: for my $test_class ($meta->name, $meta->test_classes) {

        # Attempt to load the classes
        unless (Class::MOP::load_class($test_class)) {
            warn $@ if $@;
            warn "FAILED TO LOAD $test_class. Skipping.";
            next PACKAGE;
        }

        # Only Test::Able::Objects are tests we want
        next PACKAGE unless $test_class->isa('Test::Able::Object');

        # Make sure this test has not been excluded
        {
            no strict 'refs';
            next PACKAGE if ${$test_class."::NOT_A_TEST"};
        }

        # Instantiate and add the test to the list
        push @test_objects, $test_class->new;
    }

    # Return the tests
    return \@test_objects;
}

sub setup_test_objects {
    my $meta = shift;
    $meta->test_objects($meta->build_test_objects);
};

# Use Module::Pluggable to find tests for us
use Module::Pluggable sub_name => 'test_modules';
__PACKAGE__->meta->add_method(search_path  => \&search_path);
__PACKAGE__->meta->add_method(test_modules => \&test_modules);

1;