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 = '39.1.0';
3 2     2   193361 use strict;
  2         3  
  2         53  
4 2     2   7 use warnings;
  2         3  
  2         80  
5              
6 2     2   430 use Class::XSAccessor accessors => [qw/fh line_number/];
  2         2186  
  2         12  
7              
8 2     2   355 use Carp qw/croak/;
  2         3  
  2         97  
9 2     2   878 use Encode;
  2         26123  
  2         189  
10              
11 2     2   880 use Gherkin::Line;
  2         12  
  2         91  
12 2     2   865 use Gherkin::Token;
  2         6  
  2         892  
13              
14             sub new {
15 5     5 1 484880 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       21 if ( ref $path_or_str eq 'SCALAR' ) {
24 3         5 my $bytes = Encode::encode('UTF-8', ${ $path_or_str });
  3         31  
25 2     2   1624 open $fh, '<:encoding(UTF-8)', \$bytes;
  2         37  
  2         12  
  3         227  
26             } else {
27 2 50       82 open( $fh, '<', $path_or_str )
28             || croak "Can't open [$path_or_str] for reading";
29 2         15 $fh->binmode(':utf8');
30             }
31             ## use critic
32              
33 5         2342 return bless { fh => $fh, line_number => 0 }, $class;
34             }
35              
36             sub next_line {
37 42     42 0 47 my $self = shift;
38              
39 42 50       102 return (undef, $self->line_number) if not defined $self->fh;
40              
41 42         352 my $line = $self->fh->getline;
42 42         125 $self->line_number( $self->line_number + 1 );
43              
44 42 100       86 if (not defined $line) {
45 4         33 $self->fh->close;
46 4         88 $self->fh( undef );
47             }
48              
49 42         110 return ($line, $self->line_number);
50             }
51              
52             sub read {
53 42     42 1 71 my $self = shift;
54 42         80 my ($line, $line_number) = $self->next_line;
55              
56 42         103 my $location = { line => $line_number };
57 42         66 my $line_token = undef;
58 42 100       79 if (defined $line) {
59 38         90 $line =~ s/\r$//; # \n as well as \r\n are considered lines separators
60 38         160 $line_token =
61             Gherkin::Line->new(
62             { line_text => $line, line_number => $line_number }
63             );
64             }
65 42         269 return Gherkin::Token->new(line => $line_token, location => $location);
66             }
67              
68             1;
69              
70              
71             __END__