From 14c6474d73fae6aeaa77dd19e10cf5615448aefb Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Fri, 3 Mar 2017 14:12:42 +0200 Subject: Locus: correctly handle uninitialized locations --- lib/App/Beam/Config/Locus.pm | 13 +++++++++++-- t/locus.t | 6 +++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/App/Beam/Config/Locus.pm b/lib/App/Beam/Config/Locus.pm index 050e5bb..7d1087c 100644 --- a/lib/App/Beam/Config/Locus.pm +++ b/lib/App/Beam/Config/Locus.pm @@ -69,7 +69,9 @@ sub add { Returns a string representation of the locus. The argument is optional. If given, its string representation will be concatenated to the formatted -locus with a ": " in between. This is useful for formatting error messages. +locus with a ": " in between. If multiple arguments are supplied, their +string representations will be concatenated, separated by horizontal +space characters. This is useful for formatting error messages. If the locus contains multiple file locations, the method tries to compact them by representing contiguous line ranges as B-I> and outputting @@ -93,6 +95,7 @@ will produce the following: sub format { my $self = shift; unless (exists($self->{string})) { + $self->{string} = ''; foreach my $file (sort { $self->{table}{$a}{order} <=> $self->{table}{$b}{order} } @@ -124,7 +127,13 @@ sub format { $self->{string} .= join(',', @ranges); } } - return "$self->{string}: $_[0]" if @_; + if (@_) { + if ($self->{string} ne '') { + return "$self->{string}: " . join(' ', @_); + } else { + return join(' ', @_); + } + } return $self->{string}; } diff --git a/t/locus.t b/t/locus.t index a08280b..19fc8cc 100644 --- a/t/locus.t +++ b/t/locus.t @@ -3,9 +3,12 @@ use strict; use Test; use App::Beam::Config::Locus; -plan(tests => 7); +plan(tests => 10); my $loc = new App::Beam::Config::Locus; +ok($loc->format, ''); +ok($loc->format('test', 'message'), 'test message'); + $loc->add('foo', 10); ok($loc->format, "foo:10"); @@ -17,6 +20,7 @@ ok($loc->format, "foo:10-13"); $loc->add('foo', 24); $loc->add('foo', 28); ok($loc->format, "foo:10-13,24,28"); +ok($loc->format('test', 'message'), "foo:10-13,24,28: test message"); $loc->add('bar', 1); $loc->add('baz', 8); -- cgit v1.2.1