[libtest-bdd-cucumber-perl] 09/09: Removed Ouch

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


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

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

commit a76181a3490d829120ee356f21c5c37235ef7cf3
Author: Peter Sergeant <pete at clueball.com>
Date:   Sat Jun 7 12:59:26 2014 +0100

    Removed Ouch
---
 CHANGES                         |   6 ++
 TODO                            |   1 -
 dist.ini                        |   9 ++-
 lib/Test/BDD/Cucumber/Errors.pm | 129 ++++++++++++++++++++++++++++++++++++++
 lib/Test/BDD/Cucumber/Parser.pm |  27 ++++----
 t/500_error_formatter.t         | 135 ++++++++++++++++++++++++++++++++++++++++
 6 files changed, 288 insertions(+), 19 deletions(-)

diff --git a/CHANGES b/CHANGES
index 7c9151c..c74fe71 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,4 +1,10 @@
 -----
+0.24: 07 Jun 2014
+    - Replacing string `eval` with block `eval` for requiring test harnesses -
+      thanks Paul Cochrane
+    - Module metadata now points to repository and bug tracker - thanks Peter
+      Rabbitson
+    - Removed Ouch
 0.23: 05 Jun 2014
     - Another attempt to fix up the weird regex test bug
     - Remove our experiment with ShareDir
diff --git a/TODO b/TODO
index 1b6dc66..238603e 100644
--- a/TODO
+++ b/TODO
@@ -13,7 +13,6 @@
 - [ ] Add support for explicit outlines
 - [ ] Simplify Parser
 - [ ] Add a jUnit outputer
-- [ ] Remove Ouch
 - [ ] Fix indentation
 - [ ] Quoting in tables is broken
 - [ ] Placeholders in Pystrings
diff --git a/dist.ini b/dist.ini
index af7c009..de0d179 100644
--- a/dist.ini
+++ b/dist.ini
@@ -1,5 +1,5 @@
 name      = Test-BDD-Cucumber
-version   = 0.23
+version   = 0.24
 abstract  = Feature-complete Cucumber-style testing in Perl
 main_module = lib/Test/BDD/Cucumber.pm
 author    = ['Peter Sergeant <pete at clueball.com>','Ben Rogers <ben at bdr.org>']
@@ -7,6 +7,12 @@ license   = Perl_5
 is_trial  = 0
 copyright_holder = Peter Sergeant
 
+[MetaResources]
+bugtracker.web    = https://github.com/sheriff/test-bdd-cucumber-perl/issues
+repository.url    = https://github.com/sheriff/test-bdd-cucumber-perl.git
+repository.web    = https://github.com/sheriff/test-bdd-cucumber-perl
+repository.type   = git
+
 [@Basic]
 [PkgVersion]
 [PodVersion]
@@ -23,7 +29,6 @@ Getopt::Long = 0
 JSON::MaybeXS = 0
 Module::Runtime = 0
 Moose = 0
-Ouch = 0
 Path::Class = 0
 Storable = 0
 Term::ANSIColor = 3.00
diff --git a/lib/Test/BDD/Cucumber/Errors.pm b/lib/Test/BDD/Cucumber/Errors.pm
new file mode 100644
index 0000000..33128ed
--- /dev/null
+++ b/lib/Test/BDD/Cucumber/Errors.pm
@@ -0,0 +1,129 @@
+package Test::BDD::Cucumber::Errors;
+
+use strict;
+use warnings;
+
+require Exporter;
+our @ISA = qw(Exporter);
+our @EXPORT_OK = qw(parse_error_from_line);
+
+=head1 NAME
+
+Test::BDD::Cucumber::Errors - Consistently formatted errors
+
+=head1 DESCRIPTION
+
+Consistently formatted errors
+
+=head1 NOTE
+
+This module is not intended to help throw error classes, simply to provide
+helpers for consistently formatting certain errors. Most of the errors thrown in
+practice are errors with the input test scenarios, and it's helpful to have the
+location of the error and context when debugging those. Perhaps in the future
+these can return error objects.
+
+=head1 SYNOPSIS
+
+  use Test::BDD::Cucumber::Errors qw/parse_error_from_line/;
+
+  parse_error_from_line(
+    "Your input was bad",
+    $line
+  );
+
+=head1 PARSER ERRORS
+
+=head2 parse_error_from_line
+
+Generates a parser error from a L<Test::BDD::Cucumber::Model::Line> object, and
+error reason:
+
+  parse_error_from_line(
+    "Your input was bad",
+    $line
+  );
+
+=cut
+
+sub parse_error_from_line {
+    my ( $message, $line ) = @_;
+
+    my $error  = "-- Parse Error --\n\n $message\n";
+       $error .= "  at [%s] line %d\n";
+       $error .= "  thrown by: [%s] line %d\n\n";
+       $error .= "-- [%s] --\n\n";
+       $error .= "%s";
+       $error .= "\n%s\n";
+
+    # Get the caller data
+    my ( $caller_filename, $caller_line ) = (caller())[1,2];
+
+    # Get the simplistic filename and line number it occured on
+    my $feature_filename = $line->document->filename || "(no filename)";
+    my $feature_line = $line->number;
+
+    # Get the context lines
+    my ( $start_line, @lines ) =
+        _get_context_range( $line->document, $feature_line );
+
+    my $formatted_lines;
+    for ( 0 .. 4 ) {
+        my $actual_line = $start_line + $_;
+        my $mark = ($feature_line == $actual_line) ? '*' : '|';
+        $formatted_lines .=
+            sprintf("% 3d%s    %s\n",
+                $actual_line, $mark, $lines[$_]
+            );
+    }
+
+    return(
+        sprintf( $error,
+            $feature_filename, $feature_line,
+            $caller_filename, $caller_line,
+            $feature_filename, $formatted_lines,
+            ('-' x ((length $feature_filename) + 8))
+        )
+    );
+}
+
+sub _get_context_range {
+    my ( $document, $number ) = @_;
+
+    # Context range
+    my $min_range = 1;
+    my $max_range = (scalar @{$document->lines});
+
+    my @range = (
+        $number - 2, $number - 1,
+        $number,
+        $number + 1, $number + 2
+    );
+
+    # Push the range higher if needed
+    while ( $range[0] < $min_range ) {
+        @range = map { $_+1 } @range;
+    }
+
+    # Push the range lower if needed
+    while ( $range[4] > $max_range ) {
+        @range = map { $_-1 } @range;
+    }
+
+    # Then cut it off
+    @range = grep { $_ >= $min_range } @range;
+
+    return( $range[0], map { $document->lines->[$_ - 1]->raw_content } @range );
+}
+
+=head1 AUTHOR
+
+Peter Sergeant C<pete at clueball.com>
+
+=head1 LICENSE
+
+Copyright 2014, Peter Sergeant; Licensed under the same terms as Perl
+
+=cut
+
+1;
diff --git a/lib/Test/BDD/Cucumber/Parser.pm b/lib/Test/BDD/Cucumber/Parser.pm
index f3e5a4f..2b4e2b9 100644
--- a/lib/Test/BDD/Cucumber/Parser.pm
+++ b/lib/Test/BDD/Cucumber/Parser.pm
@@ -28,7 +28,6 @@ L<Test::BDD::Cucumber::Model::Feature> object on success.
 use strict;
 use warnings;
 
-use Ouch;
 use File::Slurp;
 
 use Test::BDD::Cucumber::Model::Document;
@@ -37,10 +36,13 @@ use Test::BDD::Cucumber::Model::Scenario;
 use Test::BDD::Cucumber::Model::Step;
 use Test::BDD::Cucumber::Model::TagSpec;
 use Test::BDD::Cucumber::I18n qw(langdef);
+use Test::BDD::Cucumber::Errors qw/parse_error_from_line/;
 
 # https://github.com/cucumber/cucumber/wiki/Multiline-Step-Arguments
 # https://github.com/cucumber/cucumber/wiki/Scenario-outlines
 
+
+
 sub parse_string {
 	my ( $class, $string, $tag_scheme ) = @_;
 
@@ -59,13 +61,13 @@ sub parse_file   {
 
 sub _construct {
 	my ( $class, $document, $tag_scheme ) = @_;
-	
+
 	my $feature = Test::BDD::Cucumber::Model::Feature->new({ document => $document });
         my @lines = $class->_remove_next_blanks( @{ $document->lines } );
 
 	$feature->language($class->_extract_language(\@lines));
 
-	my $self = { langdef => 
+	my $self = { langdef =>
 		     langdef($feature->language) };
         bless $self, $class;
 
@@ -120,7 +122,7 @@ sub _extract_feature_name {
 			push( @feature_tags, @tags );
 
 		} else {
-			ouch 'parse_error', "Malformed feature line", $line;
+			die parse_error_from_line( "Malformed feature line", $line );
 		}
 	}
 
@@ -159,8 +161,8 @@ sub _extract_scenarios {
 
             # Only one background section, and it must be the first
             if ( $scenarios++ && $type =~ m/^($langdef->{background})/ ) {
-                ouch 'parse_error', "Background not allowed after scenarios",
-                  $line;
+                die parse_error_from_line( "Background not allowed after scenarios",
+                  $line);
             }
 
             # Create the scenario
@@ -189,7 +191,7 @@ sub _extract_scenarios {
             push( @scenario_tags, @tags );
 
         } else {
-            ouch 'parse_error', "Malformed scenario line", $line;
+            die parse_error_from_line( "Malformed scenario line", $line );
         }
     }
 
@@ -241,8 +243,7 @@ m/^((?:$langdef->{given})|(?:$langdef->{and})|(?:$langdef->{when})|(?:$langdef->
             return $self->_extract_table( 6, $scenario,
                 $self->_remove_next_blanks(@lines) );
         } else {
-            warn $line->content;
-            ouch 'parse_error', "Malformed step line", $line;
+            die parse_error_from_line( "Malformed step line", $line );
         }
     }
 
@@ -309,7 +310,7 @@ sub _extract_table {
 		}
 
 		if ( @columns ) {
-			ouch 'parse_error', "Inconsistent number of rows in table", $line
+			die parse_error_from_line( "Inconsistent number of rows in table", $line )
 				unless @rows == @columns;
             $target->columns( [ @columns ] ) if $target->can('columns');
 			my $i = 0;
@@ -332,12 +333,6 @@ sub _pipe_array {
 
 1;
 
-=head1 ERROR HANDLING
-
-L<Test::BDD::Cucumber> uses L<Ouch> for exception handling. Errors originating in this
-class tend to have a code of C<parse_error> and a L<Test::BDD::Cucumber::Model::Line>
-object for data.
-
 =head1 AUTHOR
 
 Peter Sergeant C<pete at clueball.com>
diff --git a/t/500_error_formatter.t b/t/500_error_formatter.t
new file mode 100644
index 0000000..8dd7dbc
--- /dev/null
+++ b/t/500_error_formatter.t
@@ -0,0 +1,135 @@
+#!perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::BDD::Cucumber::Parser;
+use Test::BDD::Cucumber::Errors qw/parse_error_from_line/;
+
+my $feature = Test::BDD::Cucumber::Parser->parse_string( join '', (<DATA>) );
+
+# Test different offsets
+my @tests = (
+    [ 0 =>
+        1,
+        "# Somehow I don't see this replacing the other tests this module has...",
+        "Feature: Simple tests of Digest.pm",
+        "  As a developer planning to use Digest.pm",
+        "",
+        "  Background:",
+    ],
+    [ 1 =>
+        1,
+        "# Somehow I don't see this replacing the other tests this module has...",
+        "Feature: Simple tests of Digest.pm",
+        "  As a developer planning to use Digest.pm",
+        "",
+        "  Background:",
+    ],
+    [ 2 =>
+        1,
+        "# Somehow I don't see this replacing the other tests this module has...",
+        "Feature: Simple tests of Digest.pm",
+        "  As a developer planning to use Digest.pm",
+        "",
+        "  Background:",
+    ],
+    [ 4 =>
+        2,
+        "Feature: Simple tests of Digest.pm",
+        "  As a developer planning to use Digest.pm",
+        "",
+        "  Background:",
+        '    Given a usable "Digest" class',
+    ],
+    [ 10 =>
+        8,
+        '  Scenario: Check MD5',
+        '    Given a Digest MD5 object',
+        '    When I\'ve added "foo bar baz" to the object',
+        '    And I\'ve added "bat ban shan" to the object',
+        '    Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5"',
+    ],
+    [ 12 =>
+        9,
+        '    Given a Digest MD5 object',
+        '    When I\'ve added "foo bar baz" to the object',
+        '    And I\'ve added "bat ban shan" to the object',
+        '    Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5"',
+        '    Then the base64 output is "1B2M2Y8AsgTpgAmY7PhCfg"',
+    ],
+    [ 13 =>
+        9,
+        '    Given a Digest MD5 object',
+        '    When I\'ve added "foo bar baz" to the object',
+        '    And I\'ve added "bat ban shan" to the object',
+        '    Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5"',
+        '    Then the base64 output is "1B2M2Y8AsgTpgAmY7PhCfg"',
+    ],
+    [ 14 =>
+        9,
+        '    Given a Digest MD5 object',
+        '    When I\'ve added "foo bar baz" to the object',
+        '    And I\'ve added "bat ban shan" to the object',
+        '    Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5"',
+        '    Then the base64 output is "1B2M2Y8AsgTpgAmY7PhCfg"',
+    ],
+);
+
+for ( @tests ) {
+    my ( $offset, $expected_offset, @lines ) = @$_;
+    is_deeply(
+        [
+            Test::BDD::Cucumber::Errors::_get_context_range(
+                $feature->document, $offset )
+        ],
+        [
+            $expected_offset,
+            @lines
+        ],
+        "Offset $offset works"
+    )
+}
+
+my $make_error = parse_error_from_line(
+    "Foo bar baz", $feature->document->lines->[1]
+);
+
+is( $make_error,
+"-- Parse Error --
+
+ Foo bar baz
+  at [(no filename)] line 2
+  thrown by: [t/500_error_formatter.t] line 95
+
+-- [(no filename)] --
+
+  1|    # Somehow I don't see this replacing the other tests this module has...
+  2*    Feature: Simple tests of Digest.pm
+  3|      As a developer planning to use Digest.pm
+  4|    "."
+  5|      Background:
+
+---------------------
+",
+    "Error example matches"
+);
+
+
+done_testing();
+
+__DATA__
+# Somehow I don't see this replacing the other tests this module has...
+Feature: Simple tests of Digest.pm
+  As a developer planning to use Digest.pm
+
+  Background:
+    Given a usable "Digest" class
+
+  Scenario: Check MD5
+    Given a Digest MD5 object
+    When I've added "foo bar baz" to the object
+    And I've added "bat ban shan" to the object
+    Then the hex output is "bcb56b3dd4674d5d7459c95e4c8a41d5"
+    Then the base64 output is "1B2M2Y8AsgTpgAmY7PhCfg"
\ No newline at end of file

-- 
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