Class: Test::Mini::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/Test/Mini/Logger.pm

Overview

Output Logger Base Class.

Whether you’re using a tool that expects output in a certain format, or you just long for the familiar look and feel of another testing framework, this is what you’re looking for.

Direct Known Subclasses

TAP

Defined Under Namespace

Classes: TAP

Attribute Accessors (collapse)

Output Functions (collapse)

Callbacks (collapse)

Statistics (collapse)

Class Method Summary (collapse)

Class Method Details

+ new($class, %args)

Constructor.

Parameters:

  • (Hash) %args

    Initial state for the new instance.

Options Hash (%args):

  • (Object) verbose — default: 0

    Logger verbosity.

  • (IO) buffer — default: STDOUT

    Output buffer.



17
18
19
20
21
22
23
24
25
26
# File 'lib/Test/Mini/Logger.pm', line 17

sub new {
    my ($class, %args) = @_;
    return bless {
        verbose => 0,
        buffer  => *STDOUT{IO},
        %args,
        count   => {},
        times   => {},
    }, $class;
}

Instance Method Details

- begin_test($self, $tc, $test)

Called before each test is run.

Parameters:

  • (Class) $tc

    The test case owning the test method.

  • (String) $test

    The name of the test method being run.



88
89
90
91
# File 'lib/Test/Mini/Logger.pm', line 88

sub begin_test {
    my ($self, $tc, $test) = @_;
    $self->{times}->{"$tc#$test"} = -Time::HiRes::time();
}

- begin_test_case($self, $tc, @tests)

Called before each test case is run.

Parameters:

  • (Class) $tc

    The test case being run.

  • (Array<String>) @tests

    A list of tests to be run.



79
80
81
82
# File 'lib/Test/Mini/Logger.pm', line 79

sub begin_test_case {
    my ($self, $tc, @tests) = @_;
    $self->{times}->{$tc} = -Time::HiRes::time();
}

- begin_test_suite($self, %args)

Called before the test suite is run.

Parameters:

  • (Hash) %args

    Options the test suite was run with.

Options Hash (%args):

  • (String) filter

    Test name filter.

  • (String) seed

    Randomness seed.



70
71
72
73
# File 'lib/Test/Mini/Logger.pm', line 70

sub begin_test_suite {
    my ($self, %args) = @_;
    $self->{times}->{$self} = -Time::HiRes::time();
}

- (IO) buffer($self)

Output buffer.

Returns:

  • (IO)

    Output buffer.



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

sub buffer {
    my ($self) = @_;
    return $self->{buffer};
}

- (Hash) count - (Number) count($key)

Accessor for counters.

Overloads:

  • - (Hash) count

    The count hash.

    Returns:

    • (Hash)

      The count hash.

  • - (Number) count($key)

    The value for the given key.

    Parameters:

    • $key

      A key in the count hash.

    Returns:

    • (Number)

      The value for the given key.



179
180
181
182
# File 'lib/Test/Mini/Logger.pm', line 179

sub count {
    my ($self, $key) = @_;
    return ($key ? $self->{count}->{$key} : $self->{count}) || 0;
}

- error($self, $tc, $test, $e)

Called when a test dies with an error. Increments the error count.

Parameters:

  • (Class) $tc

    The test case owning the test method.

  • (String) $test

    The name of the test with an error.

  • (Test::Mini::Exception) $e

    The exception object.



164
165
166
167
# File 'lib/Test/Mini/Logger.pm', line 164

sub error {
    my ($self, $tc, $test, $e) = @_;
    $self->{count}->{error}++;
}

- fail($self, $tc, $test, $e)

Called when a test fails. Increments the failure count.

Parameters:

  • (Class) $tc

    The test case owning the test method.

  • (String) $test

    The name of the failed test.

  • (Test::Mini::Exception::Assert) $e

    The exception object.



153
154
155
156
# File 'lib/Test/Mini/Logger.pm', line 153

sub fail {
    my ($self, $tc, $test, $e) = @_;
    $self->{count}->{fail}++;
}

- finish_test($self, $tc, $test, $assertions)

Called after each test is run. Increments the test and assertion counts, and finalizes the test’s timing.

Parameters:

  • (Class) $tc

    The test case owning the test method.

  • (String) $test

    The name of the test method just run.

  • (Integer) $assertions

    The number of assertions called.



99
100
101
102
103
104
# File 'lib/Test/Mini/Logger.pm', line 99

sub finish_test {
    my ($self, $tc, $test, $assertions) = @_;
    $self->{count}->{test}++;
    $self->{count}->{assert} += $assertions;
    $self->{times}->{"$tc#$test"} += Time::HiRes::time();
}

- finish_test_case($self, $tc, @tests)

Called after each test case is run. Increments the test case count, and finalizes the test case’s timing.

Parameters:

  • (Class) $tc

    The test case just run.

  • (Array<String>) @tests

    A list of tests run.



111
112
113
114
115
# File 'lib/Test/Mini/Logger.pm', line 111

sub finish_test_case {
    my ($self, $tc, @tests) = @_;
    $self->{count}->{test_case}++;
    $self->{times}->{$tc} += Time::HiRes::time();
}

- finish_test_suite($self, $exit_code)

Called after each test suite is run. Finalizes the test suite timing.

Parameters:

  • (Integer) $exit_code

    Status the tests finished with.



121
122
123
124
# File 'lib/Test/Mini/Logger.pm', line 121

sub finish_test_suite {
    my ($self, $exit_code) = @_;
    $self->{times}->{$self} += Time::HiRes::time();
}

- pass($self, $tc, $test)

Called when a test passes. Increments the pass count.

Parameters:

  • (Class) $tc

    The test case owning the test method.

  • (String) $test

    The name of the passing test.



131
132
133
134
# File 'lib/Test/Mini/Logger.pm', line 131

sub pass {
    my ($self, $tc, $test) = @_;
    $self->{count}->{pass}++;
}

Write output to the #buffer. Lines will be output without added newlines.

Parameters:

  • @msg

    The message(s) to be printed; will be handled as per print.



48
49
50
51
# File 'lib/Test/Mini/Logger.pm', line 48

sub print {
    my ($self, @msg) = @_;
    print { $self->buffer() } @msg;
}

- say($self, @msg)

Write output to the #buffer. Lines will be output with appended newlines.

Parameters:

  • @msg

    The message(s) to be printed; newlines will be appended to each message, before being passed to #print.



58
59
60
61
# File 'lib/Test/Mini/Logger.pm', line 58

sub say {
    my ($self, @msg) = @_;
    $self->print(join("\n", @msg), "\n");
}

- skip($self, $tc, $test, $e)

Called when a test is skipped. Increments the skip count.

Parameters:

  • (Class) $tc

    The test case owning the test method.

  • (String) $test

    The name of the skipped test.

  • (Test::Mini::Exception::Skip) $e

    The exception object.



142
143
144
145
# File 'lib/Test/Mini/Logger.pm', line 142

sub skip {
    my ($self, $tc, $test, $e) = @_;
    $self->{count}->{skip}++;
}

- (Number) time($self, $key)

Accessor for the timing data.

Parameters:

  • $key

    The key to look up timings for. Typical values are:

    $self

    Time for test suite

    “TestCase”

    Time for the test case

    “TestCase#test”

    Time for the given test

    Times for units that have not finished should not be relied upon.

Returns:

  • (Number)

    The time taken by the given argument, in seconds.



192
193
194
195
# File 'lib/Test/Mini/Logger.pm', line 192

sub time {
    my ($self, $key) = @_;
    return $self->{times}->{$key};
}

- verbose($self)

Logger verbosity.

Returns:

  • Logger verbosity.



31
32
33
34
# File 'lib/Test/Mini/Logger.pm', line 31

sub verbose {
    my ($self) = @_;
    return $self->{verbose};
}