File Coverage

blib/lib/Sentry/SourceFileRegistry/ContextLine.pm
Criterion Covered Total %
statement 37 37 100.0
branch 8 8 100.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Sentry::SourceFileRegistry::ContextLine;
2 8     8   261619 use Mojo::Base -base, -signatures;
  8         14  
  8         62  
3              
4 8     8   5919 use List::Util qw(min max uniq);
  8         86  
  8         730  
5 8     8   63 use Mojo::Util 'dumper';
  8         15  
  8         4933  
6              
7             has content => undef;
8             has line_count => 5;
9             has _lines => sub ($self) { [split(/\n/, $self->content // '')] };
10              
11 13     13   415649 sub _get_lower_bound ($self, $line) {
  13         28  
  13         18  
  13         13  
12 13 100       39 return [] if $line < 0;
13 12         34 my $lower_bound = max(0, $line - $self->line_count);
14 12         56 my $from = $lower_bound - 1;
15 12         24 my $to = $line - 2;
16              
17 12 100       59 return [] if $to < 0;
18              
19 5 100       10 my @line_range = uniq(map { $_ >= 0 ? $_ : 0 } ($from .. $to));
  10         33  
20 5         8 return [grep {defined} @{ $self->_lines }[@line_range]];
  9         65  
  5         12  
21             }
22              
23 11     11   2035 sub _get_upper_bound ($self, $line) {
  11         14  
  11         15  
  11         40  
24 11 100       61 return [] if $line < 0;
25 10         24 my $upper_bound = min($line + $self->line_count, scalar $self->_lines->@*);
26 10         73 return [@{ $self->_lines }[$line .. $upper_bound - 1]];
  10         15  
27             }
28              
29 6     6 0 727 sub get ($self, $line) {
  6         9  
  6         26  
  6         8  
30 6         17 my $line_count = $self->line_count;
31             return {
32 6         34 pre_context => $self->_get_lower_bound($line),
33             context_line => $self->_lines->[$line - 1],
34             post_context => $self->_get_upper_bound($line),
35             };
36             }
37              
38             1;