Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gherkin perl] general cleanup by upgrading the Perl dependency #1665

Merged
merged 9 commits into from
Jul 21, 2021
6 changes: 6 additions & 0 deletions gherkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt

### Changed

* [Perl] Minimum Perl version upgraded to 5.12 (from 5.10.1)
([#1665](https://github.com/cucumber/common/pull/1665) [ehuelsmann])

### Deprecated

### Removed

* [Perl] Dependency on `IO::Scalar` removed
([#1665](https://github.com/cucumber/common/pull/1665) [ehuelsmann])

### Fixed

## [20.0.1] - 2021-07-19
Expand Down
7 changes: 3 additions & 4 deletions gherkin/perl/cpanfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@

requires "perl", "5.10.1";
requires "perl", "5.12.0";
requires "Cpanel::JSON::XS";
requires "Class::XSAccessor";
requires "Data::UUID";
requires "Getopt::Long", "2.36";
requires "IO::Scalar";

on 'test' => sub {
requires "Path::Class";
requires "Test::Differences";
requires "Test::Exception";
requires "Test::More";
requires "Test2::V0";
requires "Test2::Tools::ClassicCompare";
};

on 'develop' => sub {
Expand Down
48 changes: 25 additions & 23 deletions gherkin/perl/lib/Gherkin/TokenScanner.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use warnings;

use Class::XSAccessor accessors => [qw/fh line_number/];

use IO::File;
use IO::Scalar;
use Carp qw/croak/;
use Encode;

use Gherkin::Line;
use Gherkin::Token;
Expand All @@ -20,10 +19,10 @@ sub new {
# a straight string is a path
my $fh;
if ( ref $path_or_str eq 'SCALAR' ) {
$fh = new IO::Scalar $path_or_str;
my $bytes = Encode::encode('UTF-8', ${ $path_or_str });
open $fh, '<:encoding(UTF-8)', \$bytes;
} else {
$fh = IO::File->new();
$fh->open( $path_or_str, '<' )
open( $fh, '<', $path_or_str )
|| croak "Can't open [$path_or_str] for reading";
$fh->binmode(':utf8');
}
Expand All @@ -33,31 +32,34 @@ sub new {

sub next_line {
my $self = shift;

return (undef, $self->line_number) if not defined $self->fh;

my $line = $self->fh->getline;
$self->line_number( $self->line_number + 1 );

if (not defined $line) {
$self->fh->close;
$self->fh( undef );
}

return ($line, $self->line_number);
}

sub read {
my $self = shift;
$self->next_line();
my $line = $self->fh->getline;
$line =~ s/\r$// if $line; # \n as well as \r\n are considered lines separators
return Gherkin::Token->new(
line => $line
? (
Gherkin::Line->new(
{ line_text => $line, line_number => $self->line_number }
)
)
: undef,
location => { line => $self->line_number }
);
}
my ($line, $line_number) = $self->next_line;

sub DESTROY {
my $self = shift;
if ( $self->fh ) {
$self->fh->close;
my $location = { line => $line_number };
my $line_token = undef;
if (defined $line) {
$line =~ s/\r$//; # \n as well as \r\n are considered lines separators
$line_token =
Gherkin::Line->new(
{ line_text => $line, line_number => $line_number }
);
}
return Gherkin::Token->new(line => $line_token, location => $location);
}

1;
3 changes: 2 additions & 1 deletion gherkin/perl/t/020_parser_instantiation.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use strict;
use warnings;

use Path::Class qw/file/;
use Test::More;
use Test2::V0;
use Test2::Tools::ClassicCompare qw/ is_deeply /;

use Gherkin::Parser;
use Gherkin::TokenScanner;
Expand Down
46 changes: 46 additions & 0 deletions gherkin/perl/t/030_tokenscanner.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!perl

use strict;
use warnings;

use Path::Class qw/file/;
use Test2::V0;

use Gherkin::TokenScanner;

## Test the token scanner to report empty lines correctly

my $feature = <<FEATURE;
# comment line
Feature: Testing an empty line

Scenario: Not an empty line...
FEATURE

my $scanner = Gherkin::TokenScanner->new( \$feature );
my $linecount = 0;

my $token;

while (1) {
$token = $scanner->read;
last if $token->is_eof;

$linecount++;
}

is( $linecount, 4, 'The token scanner should return exactly 4 lines' );

$feature =~ s/\n/\r\n/g;

$scanner = Gherkin::TokenScanner->new( \$feature );
$token = $scanner->read;

like $token->token_value, qr/\n$/, 'Token line ends in newline';
unlike $token->token_value, qr/\r\n$/,
'Token scanner stripped off carriage return';


done_testing;

1;