From d146e44ff3a8b3304e6603a2238c40034c4b91a1 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 20 Nov 2011 12:42:44 +0100 Subject: VRR.pm: Check for 'ambiguous input' error condition --- lib/Travel/Status/DE/VRR.pm | 44 ++++++++++++++++++++ t/21-vrr-ambig.t | 28 +++++++++++++ t/in/essen_alfredusbad_ambiguous.xml | 81 ++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 t/21-vrr-ambig.t create mode 100644 t/in/essen_alfredusbad_ambiguous.xml diff --git a/lib/Travel/Status/DE/VRR.pm b/lib/Travel/Status/DE/VRR.pm index 3fe437f..3efb1e8 100644 --- a/lib/Travel/Status/DE/VRR.pm +++ b/lib/Travel/Status/DE/VRR.pm @@ -109,6 +109,8 @@ sub new { $self->{tree} = XML::LibXML->load_xml( string => $self->{xml}, ); + $self->check_for_ambiguous(); + return $self; } @@ -147,6 +149,48 @@ sub sprintf_time { ); } +sub check_for_ambiguous { + my ($self) = @_; + + my $xml = $self->{tree}; + + my $xp_place = XML::LibXML::XPathExpression->new('//itdOdv/itdOdvPlace'); + my $xp_name = XML::LibXML::XPathExpression->new('//itdOdv/itdOdvName'); + + my $xp_place_elem = XML::LibXML::XPathExpression->new('./odvPlaceElem'); + my $xp_name_elem = XML::LibXML::XPathExpression->new('./odvNameElem'); + + my $e_place = ( $xml->findnodes($xp_place) )[0]; + my $e_name = ( $xml->findnodes($xp_name) )[0]; + + if ( not( $e_place and $e_name ) ) { + + # this should not happen[tm] + cluck('skipping ambiguity check- itdOdvPlace/itdOdvName missing'); + return; + } + + if ( $e_place->getAttribute('state') eq 'list' ) { + $self->{errstr} = sprintf( + 'Ambiguous place input: %s', + join( q{ | }, + map { $_->textContent } + @{ $e_place->findnodes($xp_place_elem) } ) + ); + return; + } + if ( $e_name->getAttribute('state') eq 'list' ) { + $self->{errstr} = sprintf( + 'Ambiguous name input: %s', + join( q{ | }, + map { $_->textContent } @{ $e_name->findnodes($xp_name_elem) } ) + ); + return; + } + + return; +} + sub lines { my ($self) = @_; my @lines; diff --git a/t/21-vrr-ambig.t b/t/21-vrr-ambig.t new file mode 100644 index 0000000..7dd96a4 --- /dev/null +++ b/t/21-vrr-ambig.t @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use 5.010; +use utf8; + +use File::Slurp qw(slurp); +use Test::More tests => 7; + +BEGIN { + use_ok('Travel::Status::DE::VRR'); +} +require_ok('Travel::Status::DE::VRR'); + +my $xml = slurp('t/in/essen_alfredusbad_ambiguous.xml'); + +my $status = Travel::Status::DE::VRR->new_from_xml(xml => $xml); + +isa_ok($status, 'Travel::Status::DE::VRR'); +can_ok($status, qw(errstr results)); + +$status->check_for_ambiguous(); + +is($status->errstr, 'Ambiguous name input: Alfredusbad | Am Alfredusbad', + 'errstr ok'); + +is_deeply([$status->lines], [], 'no lines'); +is_deeply([$status->results], [], 'no results'); diff --git a/t/in/essen_alfredusbad_ambiguous.xml b/t/in/essen_alfredusbad_ambiguous.xml new file mode 100644 index 0000000..9b3e589 --- /dev/null +++ b/t/in/essen_alfredusbad_ambiguous.xml @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + Essen + E + + + name list + Alfredusbad + Am Alfredusbad + Afredusbad + + + + + + + + + + + + + + + + + + + + + + + + + + + Zug + S-Bahn + U-Bahn + Stadtbahn + Straßen-/Trambahn + Stadtbus + Regionalbus + Schnellbus + Seil-/Zahnradbahn + Schiff + AST/Rufbus + Sonstige + + + + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3