summaryrefslogtreecommitdiff
path: root/cpan/Test-Simple/lib/Test/Builder/Event/Ok.pm
blob: 075fc003d783ed9a4ef37a6cea25f8c04ed70705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package Test::Builder::Event::Ok;
use strict;
use warnings;

use base 'Test::Builder::Event';

use Carp qw/confess/;
use Scalar::Util qw/blessed reftype/;
use Test::Builder::Util qw/accessors/;

accessors qw/bool real_bool name todo skip/;

sub init_order {
    my $self = shift;
    my @attrs = @_;

    my @out;
    my $diag;
    for my $i (@attrs) {
        if ($i eq 'diag') {
            $diag++;
            next;
        }

        push @out => $i;
    }

    push @out => 'diag' if $diag;

    return @out;
}

sub pre_init {
    my $self = shift;
    my ($params) = @_;

    return if $params->{real_bool} || ($params->{skip} && $params->{todo});

    my $msg    = $params->{in_todo}   ? "Failed (TODO)" : "Failed";
    my $prefix = $ENV{HARNESS_ACTIVE} ? "\n"            : "";

    my ($pkg, $file, $line) = $params->{trace}->report->call;

    if (defined $params->{name}) {
        my $name = $params->{name};
        $msg = qq[$prefix  $msg test '$name'\n  at $file line $line.\n];
    }
    else {
        $msg = qq[$prefix  $msg test at $file line $line.\n];
    }

    $params->{diag} ||= [];
    unshift @{$params->{diag}} => $msg;
}

sub to_tap {
    my $self = shift;
    my ($num) = @_;

    my $out = "";
    $out .= "not " unless $self->real_bool;
    $out .= "ok";
    $out .= " $num" if defined $num;

    if (defined $self->name) {
        my $name = $self->name;
        $name =~ s|#|\\#|g;    # # in a name can confuse Test::Harness.
        $out .= " - " . $name;
    }

    if (defined $self->skip && defined $self->todo) {
        my $why = $self->skip;

        unless ($why eq $self->todo) {
            require Data::Dumper;
            confess "2 different reasons to skip/todo: " . Data::Dumper::Dumper($self);
        }

        $out .= " # TODO & SKIP $why";
    }
    elsif (defined $self->skip) {
        $out .= " # skip";
        $out .= " " . $self->skip if length $self->skip;
    }
    elsif($self->in_todo) {
        $out .= " # TODO " . $self->todo if $self->in_todo;
    }

    $out =~ s/\n/\n# /g;

    $out .= "\n";

    return $out;
}

sub clear_diag {
    my $self = shift;
    my @out = @{delete $self->{diag} || []};
    $_->linked(undef) for @out;
    return @out;
}

sub diag {
    my $self = shift;

    for my $i (@_) {
        next unless $i;
        my $type = reftype $i || "";

        my $array = $type eq 'ARRAY' ? $i : [$i];
        for my $d (@$array) {
            if (ref $d) {
                confess "Only Diag objects can be linked to events."
                    unless blessed($d) && $d->isa('Test::Builder::Event::Diag');

                confess "Diag argument '$d' is already linked to a event."
                    if $d->linked;
            }
            else {
                $d = Test::Builder::Event::Diag->new( message => $d );
            }

            for (qw/trace pid depth in_todo source/) {
                $d->$_($self->$_) unless $d->$_;
            }

            $d->linked($self);
            push @{$self->{diag}} => $d;
        }
    }

    return $self->{diag};
}

1;

__END__

=head1 NAME

Test::Builder::Event::Ok - Ok event type

=head1 DESCRIPTION

The ok event type.

=head1 METHODS

See L<Test::Builder::Event> which is the base class for this module.

=head2 CONSTRUCTORS

=over 4

=item $r = $class->new(...)

Create a new instance

=back

=head2 SIMPLE READ/WRITE ACCESSORS

=over 4

=item $r->bool

True if the test passed, or if we are in a todo/skip

=item $r->real_bool

True if the test passed, false otherwise, even in todo.

=item $r->name

Name of the test.

=item $r->todo

Reason for todo (may be empty, even in a todo, check in_todo().

=item $r->skip

Reason for skip

=item $r->trace

Get the test trace info, including where to report errors.

=item $r->pid

PID in which the event was created.

=item $r->depth

Builder depth of the event (0 for normal, 1 for subtest, 2 for nested, etc).

=item $r->in_todo

True if the event was generated inside a todo.

=item $r->source

Builder that created the event, usually $0, but the name of a subtest when
inside a subtest.

=item $r->constructed

Package, File, and Line in which the event was built.

=item $r->diag

Either undef, or an arrayref of L<Test::Builder::Event::Diag> objects. These
objects will be linked to this Ok event. Calling C<< $diag->linked >> on them
will return this Ok object. References here are strong references, references
to this object from the linked Diag objects are weakened to avoid cycles.

You can push diag objects into the arrayref by using them as arguments to this
method. Objects will be validated to ensure that they are Diag objects, and not
already linked to a event. As well C<linked> will be set on them.

=item $r->clear_diag

Remove all linked Diag objects, also removes the link within the Diags. Returns
a list of the objects.

=back

=head2 INFORMATION

=over 4

=item $r->to_tap

Returns the TAP string for the plan (not indented).

=item $r->type

Type of event. Usually this is the lowercased name from the end of the
package. L<Test::Builder::Event::Ok> = 'ok'.

=item $r->indent

Returns the indentation that should be used to display the event ('    ' x
depth).

=back

=head1 AUTHORS

=over 4

=item Chad Granum E<lt>exodist@cpan.orgE<gt>

=back

=head1 SOURCE

The source code repository for Test::More can be found at
F<http://github.com/Test-More/test-more/>.

=head1 COPYRIGHT

Copyright 2014 Chad Granum E<lt>exodist7@gmail.comE<gt>.

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

See F<http://www.perl.com/perl/misc/Artistic.html>