From 79bebc7fbebd67e226b6441b7f50457b40eb4247 Mon Sep 17 00:00:00 2001 From: Erik Huelsmann Date: Fri, 13 Oct 2023 23:03:24 +0200 Subject: [PATCH] Migrate token and AST testing into 'dzil test' --- perl/Makefile | 13 +----- perl/bin/gherkin-generate-tokens | 23 ----------- perl/cpanfile | 3 +- perl/t/010-good-features.t | 70 ++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 35 deletions(-) delete mode 100755 perl/bin/gherkin-generate-tokens create mode 100644 perl/t/010-good-features.t diff --git a/perl/Makefile b/perl/Makefile index e5a7838b6..a2aba92cd 100644 --- a/perl/Makefile +++ b/perl/Makefile @@ -43,7 +43,8 @@ clean: ## Remove all build artifacts and files generated by the acceptance tests .DELETE_ON_ERROR: -acceptance: .built lib/Gherkin/Generated/Languages.pm $(TOKENS) $(ASTS) $(PICKLES) $(SOURCES) $(ERRORS) ## Build acceptance test dir and compare results with reference +acceptance: .built lib/Gherkin/Generated/Languages.pm $(TOKENS) $(ASTS) $(PICKLES) $(SOURCES) $(ERRORS) + dzil test .built: perl5 $(SOURCE_FILES) touch $@ @@ -57,16 +58,6 @@ lib/Gherkin/Generated/Languages.pm: $(GHERKIN_PARSER): $(GHERKIN_RAZOR) ../gherkin.berp berp -g ../gherkin.berp -t $< -o $@ --noBOM -acceptance/testdata/%.tokens: ../testdata/% ../testdata/%.tokens - mkdir -p $(@D) - $(GHERKIN_GENERATE_TOKENS) $< > $@ - diff --unified $<.tokens $@ - -acceptance/testdata/%.ast.ndjson: ../testdata/% ../testdata/%.ast.ndjson - mkdir -p $(@D) - $(GHERKIN) --no-source --no-pickles --predictable-ids $< | jq --sort-keys --compact-output "." > $@ - diff --unified <(jq "." $<.ast.ndjson) <(jq "." $@) - acceptance/testdata/%.pickles.ndjson: ../testdata/% ../testdata/%.pickles.ndjson mkdir -p $(@D) $(GHERKIN) --no-source --no-ast --predictable-ids $< | jq --sort-keys --compact-output "." > $@ diff --git a/perl/bin/gherkin-generate-tokens b/perl/bin/gherkin-generate-tokens deleted file mode 100755 index 4f2b8de5b..000000000 --- a/perl/bin/gherkin-generate-tokens +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env perl - -use strict; -use warnings; -use lib 'lib'; - -use Gherkin::Parser; -use Gherkin::TokenFormatterBuilder; - -package App::GherkinGenerateTokens; - -sub run { - my ( $class, $fh, @file_list ) = @_; - - my $parser - = Gherkin::Parser->new( Gherkin::TokenFormatterBuilder->new() ); - - print $fh join "\n", @{ $parser->parse($_) } for @file_list; - print $fh "\n"; - -} - -__PACKAGE__->run(\*STDOUT, @ARGV) unless caller; diff --git a/perl/cpanfile b/perl/cpanfile index 23731e1ea..3b894c7a2 100644 --- a/perl/cpanfile +++ b/perl/cpanfile @@ -15,5 +15,6 @@ on 'test' => sub { }; on 'develop' => sub { - # there are no specific development dependencies... + requires "File::Find::Rule"; + requires "Text::Diff"; }; diff --git a/perl/t/010-good-features.t b/perl/t/010-good-features.t new file mode 100644 index 000000000..406eb7f14 --- /dev/null +++ b/perl/t/010-good-features.t @@ -0,0 +1,70 @@ +#!perl + +use strict; +use warnings; + +BEGIN { + use Test2::V0; + + skip_all 'No AUTHOR_TESTING' + unless $ENV{AUTHOR_TESTING}; + skip_all 'Missing FEATURE_FILES_BASEDIR' + unless $ENV{FEATURE_FILES_BASEDIR}; +} + +use File::Find::Rule; +use JSON::PP; + +use App::gherkin; +use Gherkin::Parser; +use Gherkin::TokenFormatterBuilder; + +use Test2::Tools::ClassicCompare qw/ is_deeply /; +use Text::Diff; + +my @good_features = + File::Find::Rule + ->file() + ->name('*.feature') + ->in( File::Spec->catfile( $ENV{FEATURE_FILES_BASEDIR}, 'good' ) ); + +skip_all 'No test files found' + unless @good_features; + + +# Test tokens +do { + my $tp = Gherkin::Parser->new( Gherkin::TokenFormatterBuilder->new() ); + for my $feature (@good_features) { + subtest "$feature tokens" => sub { + my $tokens = $tp->parse( $feature ); + my $str = join( "\n", @{ $tokens } ) . "\n"; + # compare $tokens string content with content of file "$feature.tokens" + my $diff = diff \$str, "$feature.tokens"; + + ok( not $diff ) + or diag $diff; + }; + } +}; + +# Test AST +do { + my $app = App::gherkin->new; + $app->parse_options( qw/--no-source --no-pickles --predictable-ids/ ); + for my $feature (@good_features) { + subtest "$feature AST" => sub { + my $content; + open my $fh, '>:encoding(UTF-8)', \$content; + local $app->{out_handle} = $fh; + $app->run( $feature ); + close $fh; + my $diff = diff \$content, "$feature.ast.ndjson"; + + ok( not $diff ) + or diag $diff; + }; + } +}; + +done_testing;