File Coverage

blib/lib/Gherkin/TokenScanner.pm
Criterion Covered Total %
statement 49 49 100.0
branch 8 10 80.0
condition n/a
subroutine 11 11 100.0
pod 2 3 66.6
total 70 73 95.8


line stmt bran cond sub pod time code
1             package Gherkin::TokenScanner;
2             $Gherkin::TokenScanner::VERSION = '41.0.0';
3 2     2   177939 use strict;
  2         4  
  2         67  
4 2     2   7 use warnings;
  2         3  
  2         118  
5              
6 2     2   347 use Class::XSAccessor accessors => [qw/fh line_number/];
  2         2095  
  2         10  
7              
8 2     2   410 use Carp qw/croak/;
  2         2  
  2         80  
9 2     2   915 use Encode;
  2         25766  
  2         159  
10              
11 2     2   830 use Gherkin::Line;
  2         9  
  2         63  
12 2     2   848 use Gherkin::Token;
  2         7  
  2         708  
13              
14             sub new {
15 5     5 1 348422 my ( $class, $path_or_str ) = @_;
16              
17             # Perl convention is that a string reference is the string itself, but that
18             # a straight string is a path
19             ## no critic (RequireBriefOpen)
20             # the file is opened in this object constructor,
21             # and will be closed in the method next_line at the end
22 5         9 my $fh;
23 5 100       17 if ( ref $path_or_str eq 'SCALAR' ) {
24 3         4 my $bytes = Encode::encode('UTF-8', ${ $path_or_str });
  3         24  
25 2     2   1235 open $fh, '<:encoding(UTF-8)', \$bytes;
  2         26  
  2         10  
  3         177  
26             } else {
27 2 50       73 open( $fh, '<', $path_or_str )
28             || croak "Can't open [$path_or_str] for reading";
29 2         11 $fh->binmode(':utf8');
30             }
31             ## use critic
32              
33 5         1763 return bless { fh => $fh, line_number => 0 }, $class;
34             }
35              
36             sub next_line {
37 42     42 0 38 my $self = shift;
38              
39 42 50       78 return (undef, $self->line_number) if not defined $self->fh;
40              
41 42         277 my $line = $self->fh->getline;
42 42         116 $self->line_number( $self->line_number + 1 );
43              
44 42 100       71 if (not defined $line) {
45 4         27 $self->fh->close;
46 4         69 $self->fh( undef );
47             }
48              
49 42         83 return ($line, $self->line_number);
50             }
51              
52             sub read {
53 42     42 1 59 my $self = shift;
54 42         62 my ($line, $line_number) = $self->next_line;
55              
56 42         65 my $location = { line => $line_number };
57 42         47 my $line_token = undef;
58 42 100       74 if (defined $line) {
59 38         61 $line =~ s/\r$//; # \n as well as \r\n are considered lines separators
60 38         115 $line_token =
61             Gherkin::Line->new(
62             { line_text => $line, line_number => $line_number }
63             );
64             }
65 42         214 return Gherkin::Token->new(line => $line_token, location => $location);
66             }
67              
68             1;
69              
70              
71             __END__