summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@derf.homelinux.org>2010-08-06 22:08:14 +0200
committerDaniel Friesel <derf@derf.homelinux.org>2010-08-06 22:08:14 +0200
commita30be50b746055a1474b90b8bdf01a34776f535d (patch)
tree6e6fd847e72cfd8e01939fd776c17bfc09b98ee6
parentee38a4b7cdf0032e35e8249eb45d255480895119 (diff)
Fix internal error handling + tests for it
-rwxr-xr-xbin/efa10
-rw-r--r--lib/WWW/Efa.pm35
-rw-r--r--lib/WWW/Efa/Error.pm2
-rw-r--r--t/60-bin-efa.t177
4 files changed, 128 insertions, 96 deletions
diff --git a/bin/efa b/bin/efa
index 55695f9..d6f7cef 100755
--- a/bin/efa
+++ b/bin/efa
@@ -88,7 +88,7 @@ $efa = WWW::Efa->new(
via => (@via ? [@via, $via_type] : undef),
arrive => $opt->{'arrive'},
- depart => $opt->{'depart'},
+ depart => $opt->{'depart'} // $opt->{'time'},
date => $opt->{'date'},
exclude => $opt->{'exclude'},
prefer => $opt->{'prefer'},
@@ -100,6 +100,14 @@ $efa = WWW::Efa->new(
max_interchanges => $opt->{'max-change'},
);
+if ($efa->{'error'}) {
+ die $efa->{'error'}->as_string();
+}
+
+if (ref($efa) eq 'WWW::Efa::Error') {
+ die($efa->as_string);
+}
+
if ($opt->{'test-parse'}) {
local $/ = undef;
$efa->{'html_reply'} = <STDIN>;
diff --git a/lib/WWW/Efa.pm b/lib/WWW/Efa.pm
index ecd3880..adbf5c7 100644
--- a/lib/WWW/Efa.pm
+++ b/lib/WWW/Efa.pm
@@ -7,6 +7,7 @@ use 5.010;
use base 'Exporter';
use XML::LibXML;
+use WWW::Efa::Error;
use WWW::Mechanize;
our @EXPORT_OK = ();
@@ -26,7 +27,7 @@ sub post_time {
}
if ($time !~ / ^ [0-2]? \d : [0-5]? \d $ /x) {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf', ['time', $time, 'Must match HH:MM']
);
}
@@ -37,7 +38,7 @@ sub post_date {
my ($post, $date) = @_;
if ($date !~ /^ [0-3]? \d \. [01]? \d \. (?: \d{4} )? $/x) {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf', ['date', $date, 'Must match DD.MM.[YYYY]']
);
}
@@ -61,7 +62,7 @@ sub post_exclude {
}
}
if (not $ok) {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf',
[
'exclude',
@@ -74,14 +75,14 @@ sub post_exclude {
}
sub post_prefer {
- my ($post, $prefer) = (@_);
+ my ($post, $prefer) = @_;
- given($prefer) {
- when('speed') { $post->{'routeType'} = 'LEASTTIME' }
- when('nowait') { $post->{'routeType'} = 'LEASTINTERCHANGE' }
- when('nowalk') { $post->{'routeType'} = 'LEASTWALKING' }
+ given ($prefer) {
+ when ('speed') { $post->{'routeType'} = 'LEASTTIME' }
+ when ('nowait') { $post->{'routeType'} = 'LEASTINTERCHANGE' }
+ when ('nowalk') { $post->{'routeType'} = 'LEASTWALKING' }
default {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf',
['prefer', $prefer, 'Must be either speed, nowait or nowalk']
);
@@ -97,7 +98,7 @@ sub post_include {
when ('ic') { $post->{'lineRestriction'} = 401 }
when ('ice') { $post->{'lineRestriction'} = 400 }
default {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf',
['include', $include, 'Must be one of local/ic/ice']
);
@@ -112,7 +113,7 @@ sub post_walk_speed {
$post->{'changeSpeed'} = $walk_speed;
}
else {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf',
['walk_speed', $walk_speed, 'Must be normal, fast or slow']
);
@@ -123,9 +124,9 @@ sub post_place {
my ($post, $which, $place, $stop, $type) = @_;
if (not ($place and $stop)) {
- return WWW::Efa::Error->new(
+ die WWW::Efa::Error->new(
'internal', 'conf',
- ['place', "'$place' '$stop'", "Need at least two elements for ${which}"]
+ ['place', $which, "Need at least two elements"]
);
}
@@ -296,7 +297,13 @@ sub new {
my $ref = {};
$ref->{'config'} = \%conf;
- $ref->{'post'} = create_post(\%conf);
+
+ eval {
+ $ref->{'post'} = create_post(\%conf);
+ };
+ if ($@ and ref($@) eq 'WWW::Efa::Error') {
+ $ref->{'error'} = $@;
+ }
return bless($ref, $obj);
}
diff --git a/lib/WWW/Efa/Error.pm b/lib/WWW/Efa/Error.pm
index 1f390bf..a5fbf96 100644
--- a/lib/WWW/Efa/Error.pm
+++ b/lib/WWW/Efa/Error.pm
@@ -28,7 +28,7 @@ sub as_string {
if ($self->{'source'} eq 'internal') {
$ret = sprintf(
- "WWW::Efa config error: Wrong args for option %s. %s\n",
+ "WWW::Efa config error: Wrong arg for option %s: %s\n%s\n",
@{$self->{'data'}}
);
}
diff --git a/t/60-bin-efa.t b/t/60-bin-efa.t
index d14c399..6bb9f5d 100644
--- a/t/60-bin-efa.t
+++ b/t/60-bin-efa.t
@@ -3,7 +3,7 @@ use strict;
use warnings;
use 5.010;
-use Test::Command tests => (27);
+use Test::Command tests => 85;
my $efa = 'bin/efa';
my $testarg = "E HBf MH HBf";
@@ -11,91 +11,108 @@ my $test_parse = "--test-parse $testarg";
my $EMPTY = '';
-my $re_usage = qr{Insufficient to/from arguments, see \S*efa --help for usage};
my $re_version = qr{\S*efa version \S+};
-my $err_exclude = "exclude: Invalid argument: invalid\n";
-my $err_prefer = "prefer: Invalid argument. Use speed|nowait|nowalk\n";
-my $err_include = "include: Invalid argument. Use local|ic|ice\n";
-my $err_time = "time: Invalid argument. Use HH:MM\n";
-my $err_date = "date: Invalid argument: Use DD.MM.[YYYY]\n";
-my $err_walk_speed = "walk-speed: Invalid argument. Use normal|fast|slow\n";
-my $err_common = "Please see bin/efa --help\n";
+sub mk_err {
+ my ($arg, $value, $message) = @_;
+ return sprintf(
+ "WWW::Efa config error: Wrong arg for option %s: %s\n%s\n",
+ $arg, $value, $message
+ );
+}
# Usage on invalid invocation
my $cmd = Test::Command->new(cmd => "$efa");
-#$cmd->exit_isnt_num(0);
-#$cmd->stdout_is_eq($EMPTY);
-#$cmd->stderr_like($re_usage);
-#
-#$cmd = Test::Command->new(cmd => "$efa E HBf MH");
-#
-#$cmd->exit_isnt_num(0);
-#$cmd->stdout_is_eq($EMPTY);
-#$cmd->stderr_like($re_usage);
-#
-#$cmd = Test::Command->new(cmd => "$efa E HBf Du HBf MH");
-#
-#$cmd->exit_isnt_num(0);
-#$cmd->stdout_is_eq($EMPTY);
-#$cmd->stderr_like($re_usage);
-
-#for my $opt (qw/-e --exclude/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# $cmd->stderr_is_eq($err_exclude . $err_common);
-#}
-#
-#for my $opt (qw/-m --max-change/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt nan $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# # no stderr test - depends on Getopt::Long
-#}
-#
-#for my $opt (qw/-P --prefer/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# $cmd->stderr_is_eq($err_prefer . $err_common);
-#}
-#
-#for my $opt (qw/-i --include/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# $cmd->stderr_is_eq($err_include . $err_common);
-#}
-#
-#for my $opt (qw/-w --walk-speed/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# $cmd->stderr_is_eq($err_walk_speed . $err_common);
-#}
-#
-#for my $opt (qw/-t --time/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt 35:12 $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# $cmd->stderr_is_eq($err_time . $err_common);
-#}
-#
-#for my $opt (qw/-d --date/) {
-# $cmd = Test::Command->new(cmd => "$efa $opt 11.23.2010 $testarg");
-#
-# $cmd->exit_isnt_num(0);
-# $cmd->stdout_is_eq($EMPTY);
-# $cmd->stderr_is_eq($err_date . $err_common);
-#}
+$cmd->exit_isnt_num(0);
+$cmd->stdout_is_eq($EMPTY);
+$cmd->stderr_is_eq(
+ mk_err('place', 'origin', 'Need at least two elements')
+);
+
+$cmd = Test::Command->new(cmd => "$efa E HBf MH");
+
+$cmd->exit_isnt_num(0);
+$cmd->stdout_is_eq($EMPTY);
+$cmd->stderr_is_eq(
+ mk_err('place', 'origin', 'Need at least two elements')
+);
+
+$cmd = Test::Command->new(cmd => "$efa E HBf Du HBf MH");
+
+$cmd->exit_isnt_num(0);
+$cmd->stdout_is_eq($EMPTY);
+$cmd->stderr_is_eq(
+ mk_err('place', 'origin', 'Need at least two elements')
+);
+
+for my $opt (qw/-e --exclude/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ $cmd->stderr_is_eq(
+ mk_err('exclude', 'invalid', 'Must consist of zug s-bahn u-bahn stadtbahn tram stadtbus regionalbus schnellbus seilbahn schiff ast sonstige')
+ );
+}
+
+for my $opt (qw/-m --max-change/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt nan $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ # no stderr test - depends on Getopt::Long
+}
+
+for my $opt (qw/-P --prefer/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ $cmd->stderr_is_eq(
+ mk_err('prefer', 'invalid', 'Must be either speed, nowait or nowalk')
+ );
+}
+
+for my $opt (qw/-i --include/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ $cmd->stderr_is_eq(
+ mk_err('include', 'invalid', 'Must be one of local/ic/ice')
+ );
+}
+
+for my $opt (qw/-w --walk-speed/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt invalid $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ $cmd->stderr_is_eq(
+ mk_err('walk_speed', 'invalid', 'Must be normal, fast or slow')
+ );
+}
+
+for my $opt (qw/-t --time --depart -a --arrive/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt 35:12 $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ $cmd->stderr_is_eq(
+ mk_err('time', '35:12', 'Must match HH:MM')
+ );
+}
+
+for my $opt (qw/-d --date/) {
+ $cmd = Test::Command->new(cmd => "$efa $opt 11.23.2010 $testarg");
+
+ $cmd->exit_isnt_num(0);
+ $cmd->stdout_is_eq($EMPTY);
+ $cmd->stderr_is_eq(
+ mk_err('date', '11.23.2010', 'Must match DD.MM.[YYYY]')
+ );
+}
for my $opt (qw/-v --version/) {
$cmd = Test::Command->new(cmd => "$efa $opt");