[libtest-bdd-cucumber-perl] 21/52: added --i18n option to pherkin

Intrigeri intrigeri at moszumanska.debian.org
Thu Jun 19 10:18:33 UTC 2014


This is an automated email from the git hooks/post-receive script.

intrigeri pushed a commit to annotated tag 0.19
in repository libtest-bdd-cucumber-perl.

commit 9dc0d9b9f5ff2263a735a14e1f26326d47c0136c
Author: glauschwuffel <glauschwuffel at nomaden.org>
Date:   Sun May 25 18:29:21 2014 +0200

    added --i18n option to pherkin
---
 lib/App/pherkin.pm              | 53 ++++++++++++++++++++++++---
 lib/Test/BDD/Cucumber/I18n.pm   | 79 ++++++++++++++++++++++++++++++++++++++---
 lib/Test/BDD/Cucumber/Parser.pm | 32 ++++++++---------
 3 files changed, 139 insertions(+), 25 deletions(-)

diff --git a/lib/App/pherkin.pm b/lib/App/pherkin.pm
index 3f2b56e..f7fc094 100644
--- a/lib/App/pherkin.pm
+++ b/lib/App/pherkin.pm
@@ -4,6 +4,8 @@ use strict;
 use warnings;
 use FindBin::libs;
 use Getopt::Long;
+use Test::BDD::Cucumber::I18n qw(languages langdef);
+use List::Util qw(max);
 
 use Moose;
 has 'tags' => ( is => 'rw', isa => 'ArrayRef', required => 0 );
@@ -80,12 +82,19 @@ sub _process_arguments {
     my $includes = [];
     my $tags = [];
     GetOptions(
-        'I=s@'   => \$includes,
-        'l|lib'  => \(my $add_lib),
-        'b|blib' => \(my $add_blib),
+        'I=s@'       => \$includes,
+        'l|lib'      => \(my $add_lib),
+        'b|blib'     => \(my $add_blib),
         'o|output=s' => \(my $harness),
-        't|tags=s@' => \$tags,
+	't|tags=s@'  => \$tags,
+	'i18n=s'     => \(my $i18n)
     );
+
+    if ($i18n) {
+        _print_langdef($i18n) unless $i18n eq 'help';
+        _print_languages();
+    };
+
     unshift @$includes, 'lib'                   if $add_lib;
     unshift @$includes, 'blib/lib', 'blib/arch' if $add_blib;
 
@@ -136,6 +145,42 @@ sub _process_tags {
     return $tag_scheme;
 }
 
+sub _print_languages {
+
+    my @languages=languages();
+
+    my $max_code_length   = max map { length } @languages;
+    my $max_name_length   = max map { length(langdef($_)->{name}) } @languages;
+    my $max_native_length = max map { length(langdef($_)->{native}) } @languages;
+
+    my $format= "| %-${max_code_length}s | %-${max_name_length}s | %-${max_native_length}s |\n";
+
+    for my $language (sort @languages) {
+        binmode STDOUT, ':utf8';
+        my $langdef=langdef($language);
+	printf $format, $language, $langdef->{name}, $langdef->{native};
+    }
+    exit;
+}
+
+sub _print_langdef {
+    my ($language)=@_;
+
+    my $langdef=langdef($language);
+
+    my @keywords= qw(feature background scenario scenario_outline examples
+		     given when then and but);
+    my $max_length = max map { length $langdef->{$_} } @keywords;
+
+    my $format= "| %-16s | %-${max_length}s |\n";
+
+    for my $keyword (qw(feature background scenario scenario_outline
+			examples given when then and but )) {
+        binmode STDOUT, ':utf8';
+        printf $format, $keyword, $langdef->{$keyword};
+    }
+    exit;
+}
 
 =head1 AUTHOR
 
diff --git a/lib/Test/BDD/Cucumber/I18n.pm b/lib/Test/BDD/Cucumber/I18n.pm
index 0eb6e4d..e1fbf14 100644
--- a/lib/Test/BDD/Cucumber/I18n.pm
+++ b/lib/Test/BDD/Cucumber/I18n.pm
@@ -1,5 +1,28 @@
 package Test::BDD::Cucumber::I18n;
 
+=head1 NAME
+
+Test::BDD::Cucumber::I18N - Internationalization
+
+=head1 DESCRIPTION
+
+Internationalization of feature files and step definitions.
+
+=head1 SYNOPSIS
+
+use Test::BDD::Cucumber::I18N qw(languages has_language langdef);
+
+# get codes of supported languages
+my @supported_languages = languages();
+
+# look up if a language is supported
+my $language_is_supported = has_language('de');
+
+# get definition of a language
+my $langdef = langdef('de');
+
+=cut
+
 use strict;
 use warnings;
 
@@ -7,27 +30,73 @@ use Encode qw(encode);
 use JSON::XS;
 use utf8;
 
+use base 'Exporter';
+
+our @EXPORT_OK=qw(languages langdef has_language);
+
 # Parse keywords hash for all supported languages from the DATA segment
 my $json      = join '', (<DATA>);
 my $json_utf8 = encode('UTF-8', $json);
-my $keywords  = decode_json( $json_utf8 );
+my $langdefs  = decode_json( $json_utf8 );
 
 sub languages {
-    return keys $keywords;
+    return keys $langdefs;
 }
 
 sub has_language {
     my ($language) = @_;
-    exists $keywords->{$language};
+    exists $langdefs->{$language};
 }
 
-sub keywords {
+sub langdef {
     my ($language) = @_;
 
     return unless has_language($language);
-    return $keywords->{$language};
+    return $langdefs->{$language};
 }
 
+=head1 LANGUAGES
+
+Languages are defined in a JSON-based hash in the __DATA__ section of this file.
+That hash is based on the i18n.json of the Gherkin project (the parser for
+features that the original Cucumber tool uses).
+
+Gherkin can be found at L<https://github.com/cucumber/gherkin>,
+its i18n.json at L<https://github.com/cucumber/gherkin/blob/master/lib/gherkin/i18n.json>.
+
+=head1 AUTHOR
+
+Gregor Goldbach C<glauschwuffel at nomaden.org>
+(based on the works of Pablo Duboue)
+
+=head1 LICENSE
+
+Copyright 2014, Gregor Goldbach; Licensed under the same terms as Perl
+
+Definition of languages based on data from Gherkin.
+Copyright (c) 2009-2013 Mike Sassak, Gregory Hnatiuk, Aslak Hellesøy
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+=cut
+
 __DATA__
 {
 	"en": {
diff --git a/lib/Test/BDD/Cucumber/Parser.pm b/lib/Test/BDD/Cucumber/Parser.pm
index 1e974b5..6dbf254 100644
--- a/lib/Test/BDD/Cucumber/Parser.pm
+++ b/lib/Test/BDD/Cucumber/Parser.pm
@@ -36,7 +36,7 @@ use Test::BDD::Cucumber::Model::Feature;
 use Test::BDD::Cucumber::Model::Scenario;
 use Test::BDD::Cucumber::Model::Step;
 use Test::BDD::Cucumber::Model::TagSpec;
-use Test::BDD::Cucumber::I18n;
+use Test::BDD::Cucumber::I18n qw(langdef);
 
 # https://github.com/cucumber/cucumber/wiki/Multiline-Step-Arguments
 # https://github.com/cucumber/cucumber/wiki/Scenario-outlines
@@ -65,8 +65,8 @@ sub _construct {
 
 	$feature->language($class->_extract_language(\@lines));
 
-	my $self = { keywords => 
-		     Test::BDD::Cucumber::I18n::keywords($feature->language) };
+	my $self = { langdef => 
+		     langdef($feature->language) };
         bless $self, $class;
 
 	$self->_extract_scenarios(
@@ -107,7 +107,7 @@ sub _extract_feature_name {
 		next if $line->is_comment;
 		last if $line->is_blank;
 
-		if ( $line->content =~ m/^(?:$self->{keywords}->{feature}): (.+)/ ) {
+		if ( $line->content =~ m/^(?:$self->{langdef}->{feature}): (.+)/ ) {
 			$feature->name( $1 );
 			$feature->name_line( $line );
 			$feature->tags( \@feature_tags );
@@ -133,7 +133,7 @@ sub _extract_conditions_of_satisfaction {
 	while ( my $line = shift( @lines ) ) {
 		next if $line->is_comment || $line->is_blank;
 
-		if ( $line->content =~ m/^((?:$self->{keywords}->{background}):|(?:$self->{keywords}->{scenario}):|@)/ ) {
+		if ( $line->content =~ m/^((?:$self->{langdef}->{background}):|(?:$self->{langdef}->{scenario}):|@)/ ) {
 			unshift( @lines, $line );
 			last;
 		} else {
@@ -152,11 +152,11 @@ sub _extract_scenarios {
 	while ( my $line = shift( @lines ) ) {
 		next if $line->is_comment || $line->is_blank;
 
-		if ( $line->content =~ m/^((?:$self->{keywords}->{background})|(?:$self->{keywords}->{scenario}))(?: Outline)?: ?(.+)?/ ) {
+		if ( $line->content =~ m/^((?:$self->{langdef}->{background})|(?:$self->{langdef}->{scenario}))(?: Outline)?: ?(.+)?/ ) {
 			my ( $type, $name ) = ( $1, $2 );
 
 			# Only one background section, and it must be the first
-			if ( $scenarios++ && $type =~ m/^($self->{keywords}->{background})/ ) {
+			if ( $scenarios++ && $type =~ m/^($self->{langdef}->{background})/ ) {
 				ouch 'parse_error', "Background not allowed after scenarios",
 					$line;
 			}
@@ -164,7 +164,7 @@ sub _extract_scenarios {
 			# Create the scenario
 			my $scenario = Test::BDD::Cucumber::Model::Scenario->new({
 				( $name ? ( name => $name ) : () ),
-				background => $type =~ m/^($self->{keywords}->{background})/ ? 1 : 0,
+				background => $type =~ m/^($self->{langdef}->{background})/ ? 1 : 0,
 				line       => $line,
 				tags       => [@{$feature->tags}, @scenario_tags]
 			});
@@ -173,7 +173,7 @@ sub _extract_scenarios {
 			# Attempt to populate it
 			@lines = $self->_extract_steps( $feature, $scenario, @lines );
 
-			if ( $type =~ m/^($self->{keywords}->{background})/ ) {
+			if ( $type =~ m/^($self->{langdef}->{background})/ ) {
 				$feature->background( $scenario );
 			} else {
 				push( @{ $feature->scenarios }, $scenario );
@@ -195,7 +195,7 @@ sub _extract_scenarios {
 sub _extract_steps {
 	my ( $self, $feature, $scenario, @lines ) = @_;
 
-        my @givens = split( /\|/, $self->{keywords}->{given} );
+        my @givens = split( /\|/, $self->{langdef}->{given} );
 	my $last_verb = $givens[-1];
 
 	while ( my $line = shift( @lines ) ) {
@@ -203,13 +203,13 @@ sub _extract_steps {
 		last if $line->is_blank;
 
 		# Conventional step?
-		if ( $line->content =~ m/^((?:$self->{keywords}->{given})|(?:$self->{keywords}->{and})|(?:$self->{keywords}->{when})|(?:$self->{keywords}->{then})|(?:$self->{keywords}->{but})) (.+)/ ) {
+		if ( $line->content =~ m/^((?:$self->{langdef}->{given})|(?:$self->{langdef}->{and})|(?:$self->{langdef}->{when})|(?:$self->{langdef}->{then})|(?:$self->{langdef}->{but})) (.+)/ ) {
 			my ( $verb, $text ) = ( $1, $2 );
 			my $original_verb = $verb;
-			$verb = 'Given' if $verb =~ m/($self->{keywords}->{given})/;
-			$verb = 'When' if  $verb =~ m/($self->{keywords}->{when})/;
-			$verb = 'Then' if  $verb =~ m/($self->{keywords}->{then})/;
-			$verb = $last_verb if $verb =~ m/^($self->{keywords}->{and})/ or $verb =~ m/^($self->{keywords}->{but})/;
+			$verb = 'Given' if $verb =~ m/($self->{langdef}->{given})/;
+			$verb = 'When' if  $verb =~ m/($self->{langdef}->{when})/;
+			$verb = 'Then' if  $verb =~ m/($self->{langdef}->{then})/;
+			$verb = $last_verb if $verb =~ m/^($self->{langdef}->{and})/ or $verb =~ m/^($self->{langdef}->{but})/;
             $last_verb = $verb;
 
 			my $step = Test::BDD::Cucumber::Model::Step->new({
@@ -225,7 +225,7 @@ sub _extract_steps {
 			push( @{ $scenario->steps }, $step );
 
 		# Outline data block...
-		} elsif ( $line->content =~ m/^($self->{keywords}->{examples}):$/ ) {
+		} elsif ( $line->content =~ m/^($self->{langdef}->{examples}):$/ ) {
 			return $self->_extract_table( 6, $scenario,
 			    $self->_remove_next_blanks( @lines ));
 		} else {

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libtest-bdd-cucumber-perl.git



More information about the Pkg-perl-cvs-commits mailing list