File Coverage

blib/lib/Gherkin/Exceptions.pm
Criterion Covered Total %
statement 45 85 52.9
branch 0 2 0.0
condition 0 2 0.0
subroutine 15 30 50.0
pod 0 2 0.0
total 60 121 49.5


line stmt bran cond sub pod time code
1             ## no critic (ProhibitMultiplePackages, RequireExplicitPackage)
2              
3 2     2   12 use strict;
  2         3  
  2         62  
4 2     2   8 use warnings;
  2         2  
  2         240  
5              
6             package Gherkin::Exceptions;
7             $Gherkin::Exceptions::VERSION = '39.0.0';
8 0     0 0   sub stringify { my $self = shift; $self->message }
  0            
9 0     0 0   sub throw { my $class = shift; die $class->new(@_) }
  0            
10              
11             # Parent of single and composite exceptions
12             package Gherkin::Exceptions::Parser;
13             $Gherkin::Exceptions::Parser::VERSION = '39.0.0';
14 2     2   9 use base 'Gherkin::Exceptions';
  2         3  
  2         304  
15              
16             # Composite exceptions
17             package Gherkin::Exceptions::CompositeParser;
18             $Gherkin::Exceptions::CompositeParser::VERSION = '39.0.0';
19 2     2   9 use base 'Gherkin::Exceptions::Parser';
  2         4  
  2         1643  
20 2     2   14 use Class::XSAccessor accessors => [qw/errors/];
  2         2  
  2         34  
21              
22             sub new {
23 0     0     my ( $class, @errors ) = @_;
24 0           bless { errors => \@errors }, $class;
25             }
26              
27             sub message {
28 0     0     my $self = shift;
29             return join "\n",
30 0           ( 'Parser errors:', map { $_->message } @{ $self->errors } );
  0            
  0            
31             }
32              
33 0     0     sub throw { my $class = shift; die $class->new(@_) }
  0            
34              
35             #
36             # Various non-composite exceptions
37             #
38             package Gherkin::Exceptions::SingleParser;
39             $Gherkin::Exceptions::SingleParser::VERSION = '39.0.0';
40 2     2   740 use base 'Gherkin::Exceptions::Parser';
  2         3  
  2         504  
41 2     2   14 use Class::XSAccessor accessors => [qw/detailed_message location/];
  2         3  
  2         8  
42              
43             sub new {
44 0     0     my ( $class, %args ) = @_;
45 0           bless { %args }, $class;
46             }
47              
48             sub message {
49 0     0     my $self = shift;
50             return sprintf( '(%i:%i): %s',
51             $self->location->{'line'},
52 0   0       ( $self->location->{'column'} || 0 ),
53             $self->detailed_message );
54             }
55              
56             package Gherkin::Exceptions::NoSuchLanguage;
57             $Gherkin::Exceptions::NoSuchLanguage::VERSION = '39.0.0';
58 2     2   688 use base 'Gherkin::Exceptions::SingleParser';
  2         4  
  2         599  
59 2     2   14 use Class::XSAccessor accessors => [qw/language location/];
  2         2  
  2         67  
60              
61             sub new {
62 0     0     my ( $class, $language, $location ) = @_;
63 0           return bless { language => $language, location => $location }, $class;
64             }
65              
66             sub detailed_message {
67 0     0     my $self = shift;
68 0           return "Language not supported: " . $self->language;
69             }
70              
71             package Gherkin::Exceptions::AstBuilder;
72             $Gherkin::Exceptions::AstBuilder::VERSION = '39.0.0';
73 2     2   657 use base 'Gherkin::Exceptions::SingleParser';
  2         3  
  2         518  
74 2     2   11 use Class::XSAccessor accessors => [qw/location ast_message/];
  2         3  
  2         8  
75              
76             sub new {
77 0     0     my ( $class, $ast_message, $location ) = @_;
78 0           return bless { ast_message => $ast_message, location => $location },
79             $class;
80             }
81              
82             sub detailed_message {
83 0     0     my $self = shift;
84 0           return $self->ast_message;
85             }
86              
87             package Gherkin::Exceptions::UnexpectedEOF;
88             $Gherkin::Exceptions::UnexpectedEOF::VERSION = '39.0.0';
89 2     2   721 use base 'Gherkin::Exceptions::SingleParser';
  2         4  
  2         444  
90 2     2   11 use Class::XSAccessor accessors => [qw/location expected_token_types/];
  2         3  
  2         23  
91              
92             sub new {
93 0     0     my ( $class, $received_token, $expected_token_types ) = @_;
94 0           return bless {
95             location => $received_token->location,
96             received_token => $received_token,
97             expected_token_types => $expected_token_types
98             }, $class;
99             }
100              
101             sub detailed_message {
102 0     0     my $self = shift;
103             return "unexpected end of file, expected: " . join ', ',
104 0           @{ $self->expected_token_types };
  0            
105             }
106              
107             package Gherkin::Exceptions::UnexpectedToken;
108             $Gherkin::Exceptions::UnexpectedToken::VERSION = '39.0.0';
109 2     2   754 use base 'Gherkin::Exceptions::SingleParser';
  2         9  
  2         493  
110 2         10 use Class::XSAccessor accessors =>
111 2     2   28 [qw/location received_token_value expected_token_types state_comment/];
  2         4  
112              
113             sub new {
114 0     0     my ( $class, $received_token, $expected_token_types, $state_comment )
115             = @_;
116              
117 0           my $received_token_value = $received_token->token_value;
118 0           $received_token_value =~ s/^\s+//;
119 0           $received_token_value =~ s/\s+$//;
120              
121 0           my %location = %{ $received_token->location };
  0            
122             $location{'column'} = $received_token->line->indent + 1
123 0 0         unless defined $location{'column'};
124              
125 0           return bless {
126             location => \%location,
127             received_token_value => $received_token_value,
128             expected_token_types => $expected_token_types,
129             state_comment => $state_comment,
130             }, $class;
131             }
132              
133             sub detailed_message {
134 0     0     my $self = shift;
135             return sprintf(
136             "expected: %s, got '%s'",
137 0           ( join ', ', @{ $self->expected_token_types } ),
  0            
138             $self->received_token_value,
139             );
140             }
141              
142             1;