Class List Method List File List

# Class: Test::Mini::TestCase

Inherits:
  Object
  - Object
  - Test::Mini::TestCase
  show all
Defined in:
  lib/Test/Mini/TestCase.pm

## Overview

Base class for Test::Mini test cases.

### See Also:

- Runner

## Class Method Summary (collapse)

- + new($class, %args)

  Constructor.

## Instance Method Summary (collapse)

- - run

  Runs the test specified at construction time.

- - setup($self)

  Test setup behavior, automatically invoked prior to each test.

- - teardown($self)

  Test teardown behavior, automatically invoked following each test.

## Class Method Details

+ new($class, %args)

Constructor.

### Parameters:

- (`Hash`) %args —

  Initial state for the new instance.

### Options Hash (`%args`):

- (`Object`) name —

  The specific test this instance should run.

    17
    18
    19
    20
    # File 'lib/Test/Mini/TestCase.pm', line 17

    sub new {
        my ($class, %args) = @_;
        return bless { %args, passed => 0 }, $class;
    }

## Instance Method Details

- run

Runs the test specified at construction time. This method is responsible for invoking the setup and teardown advice for the method, in addition to ensuring that any fatal errors encountered by the program are suitably handled. Appropriate diagnostic information should be sent to the supplied `$runner`.

### Parameters:

- (`Test::Mini::Runner`) $runner

### Returns:

- The number of assertions called by this test.

    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
    # File 'lib/Test/Mini/TestCase.pm', line 70

    sub run {
        my ($self, $runner) = @_;
        my $e;
        my $test = $self->{name};

        eval {
            local $SIG{__DIE__} = sub {
                # Package declaration for isolating the callstack.
                # @api private
                package Test::Mini::SIGDIE;

                die $@ if UNIVERSAL::isa($@, 'Test::Mini::Exception');

                (my $msg = "@_") =~ s/ at .*? line \d+\.\n$//;
                my $error = Test::Mini::Exception->new(
                    message        => "$msg\n",
                    ignore_package => [qw/ Test::Mini::SIGDIE Carp /],
                );

                my $me = $error->trace->frame(0);
                if ($me->{subroutine} eq 'Test::Mini::TestCase::__ANON__') {
                    $me->{subroutine} = 'die';
                    $me->{args} = [ $msg ];
                }

                die $error;
            };

            $self->setup();
            $self->$test();
            $self->{passed} = 1;

            die 'No assertions called' unless count_assertions();
        };

        if ($e = Exception::Class->caught()) {
            $self->{passed} = 0;

            if ($e = Exception::Class->caught('Test::Mini::Exception::Skip')) {
                $runner->skip(ref $self, $test, $e);
            }
            elsif ($e = Exception::Class->caught('Test::Mini::Exception::Assert')) {
                $runner->fail(ref $self, $test, $e);
            }
            elsif ($e = Exception::Class->caught('Test::Mini::Exception')) {
                $runner->error(ref $self, $test, $e);
            }
        }

        eval {
            $self->teardown();
            $runner->pass(ref $self, $self->{name}) if $self->{passed};
        };
        if ($e = Exception::Class->caught()) {
            $runner->error(ref $self, $test, $e);
        }

        return reset_assertions();
    }

    1;

- setup($self)

Test setup behavior, automatically invoked prior to each test. Intended to be overridden by subclasses.

### Examples:

####

    package TestSomething;
    use base 'Test::Mini::TestCase';

    use Something;

    sub setup { $obj = Something->new(); }

    sub test_can_foo {
        assert_can($obj, 'foo');
    }

### See Also:

- #teardown

    38
    39
    40
    # File 'lib/Test/Mini/TestCase.pm', line 38

    sub setup {
        my ($self) = @_;
    }

- teardown($self)

Test teardown behavior, automatically invoked following each test. Intended to be overridden by subclasses.

### Examples:

####

    package Test;
    use base 'Test::Mini::TestCase';

    sub teardown { unlink 'foo.bar' }

    sub test_touching_files {
        `touch foo.bar`;
        assert(-f 'foo.bar');
    }

### See Also:

- #setup

    57
    58
    59
    # File 'lib/Test/Mini/TestCase.pm', line 57

    sub teardown {
        my ($self) = @_;
    }