summaryrefslogtreecommitdiff
path: root/cpan/Test-Simple/lib/Test/Stream/Block.pm
blob: 7f6bd683657d7092054ed4067d8cf64ffa66a3d6 (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
package Test::Stream::Block;
use strict;
use warnings;

use Scalar::Util qw/blessed reftype/;
use Test::Stream::Carp qw/confess carp/;

use Test::Stream::ArrayBase(
    accessors => [qw/name coderef params caller deduced _start_line _end_line/],
);

our %SUB_MAPS;

sub PACKAGE() { 0 };
sub FILE()    { 1 };
sub LINE()    { 2 };
sub SUBNAME() { 3 };

sub init {
    my $self = shift;

    confess "coderef is a mandatory field for " . blessed($self) . " instances"
        unless $self->[CODEREF];

    confess "caller is a mandatory field for " . blessed($self) . " instances"
        unless $self->[CALLER];

    confess "coderef must be a code reference"
        unless ref($self->[CODEREF]) && reftype($self->[CODEREF]) eq 'CODE';

    $self->deduce;

    $self->[PARAMS] ||= {};
}

sub deduce {
    my $self = shift;

    eval { require B; 1 } || return;

    my $code    = $self->[CODEREF];
    my $cobj    = B::svref_2object($code);
    my $pkg     = $cobj->GV->STASH->NAME;
    my $file    = $cobj->FILE;
    my $line    = $cobj->START->line;
    my $subname = $cobj->GV->NAME;

    $SUB_MAPS{$file}->{$line} = $self->[NAME];

    $self->[DEDUCED] = [$pkg, $file, $line, $subname];
    $self->[NAME] ||= $subname;
}

sub merge_params {
    my $self = shift;
    my ($new) = @_;
    my $old = $self->[PARAMS];

    # Use existing ref, merge in new ones, but old ones are kept since the
    # block can override the workflow.
    %$old = ( %$new, %$old );
}

sub package { $_[0]->[DEDUCED]->[PACKAGE] }
sub file    { $_[0]->[DEDUCED]->[FILE]    }
sub subname { $_[0]->[DEDUCED]->[SUBNAME] }

sub run {
    my $self = shift;
    my @args = @_;

    $self->[CODEREF]->(@args);
}

sub detail {
    my $self = shift;

    my $name = $self->[NAME];
    my $file = $self->file;

    my $start = $self->start_line;
    my $end   = $self->end_line;

    my $lines;
    if ($end && $end != $start) {
        $lines = "lines $start -> $end";
    }
    elsif ($end) {
        $lines = "line $start";
    }
    else {
        my ($dpkg, $dfile, $dline) = @{$self->caller};
        $lines = "line $start (declared in $dfile line $dline)";
    }

    my $known = "";
    if ($self->[DEDUCED]->[SUBNAME] ne '__ANON__') {
        $known = " (" . $self->[DEDUCED]->[SUBNAME] . ")";
    }

    return "${name}${known} in ${file} ${lines}";
}

sub start_line {
    my $self = shift;
    return $self->[_START_LINE] if $self->[_START_LINE];

    my $start = $self->[DEDUCED]->[LINE];
    my $end   = $self->end_line || 0;

    if ($start == $end || $start == 1) {
        $self->[_START_LINE] = $start;
    }
    else {
        $self->[_START_LINE] = $start - 1;
    }

    return $self->[_START_LINE];
}

sub end_line {
    my $self = shift;
    return $self->[_END_LINE] if $self->[_END_LINE];

    my $call = $self->[CALLER];
    my $dedu = $self->[DEDUCED];

    _map_package_file($dedu->[PACKAGE], $dedu->[FILE]);

    # Check if caller and deduced seem to be from the same place.
    my $match = $call->[PACKAGE] eq $dedu->[PACKAGE];
    $match &&= $call->[FILE] eq $dedu->[FILE];
    $match &&= $call->[LINE] >= $dedu->[LINE];
    $match &&= !_check_interrupt($dedu->[FILE], $dedu->[LINE], $call->[LINE]);

    if ($match) {
        $self->[_END_LINE] = $call->[LINE];
        return $call->[LINE];
    }

    # Uhg, see if we can figure it out.
    my @lines = sort { $a <=> $b } keys %{$SUB_MAPS{$dedu->[FILE]}};
    for my $line (@lines) {
        next if $line <= $dedu->[LINE];
        $self->[_END_LINE] = $line;
        $self->[_END_LINE] -= 2 unless $SUB_MAPS{$dedu->[FILE]}->{$line} eq '__EOF__';
        return $self->[_END_LINE];
    }

    return undef;
}

sub _check_interrupt {
    my ($file, $start, $end) = @_;
    return 0 if $start == $end;

    my @lines = sort { $a <=> $b } keys %{$SUB_MAPS{$file}};

    for my $line (@lines) {
        next if $line <= $start;
        return $line <= $end;
    }

    return 0;
}

my %MAPPED;
sub _map_package_file {
    my ($pkg, $file) = @_;

    return if $MAPPED{$pkg}->{$file}++;

    require B;

    my %seen;
    my @symbols = do { no strict 'refs'; %{"$pkg\::"} };
    for my $sym (@symbols) {
        my $code = $pkg->can($sym) || next;
        next if $seen{$code}++;

        my $cobj = B::svref_2object($code);

        # Skip imported subs
        my $pname = $cobj->GV->STASH->NAME;
        next unless $pname eq $pkg;

        my $f = $cobj->FILE;
        next unless $f eq $file;

        # Skip XS/C Files
        next if $file =~ m/\.c$/;
        next if $file =~ m/\.xs$/;

        my $line = $cobj->START->line;
        $SUB_MAPS{$file}->{$line} ||= $sym;
    }

    if (open(my $fh, '<', $file)) {
        my $length = () = <$fh>;
        close($fh);
        $SUB_MAPS{$file}->{$length} = '__EOF__';
    }
}

1;