Subject: | Suggestions for enhanced diagnostics |
Attached Diff includes suggestions for enhanced Diagnostics for usage,
as well as test cases for the changes.
1. If format() is called with any arguments, but none of them are
'args', it throws a warning.
2. If the parameter lookup method tries to lookup the value for a
replacement ( calc_param ) and that parameter has never been created by
the user, it warns about failing to set the parameter. ( I made it
return an empty '' to reduce the number of confusing warnings that it
produces, but I'm having second thoughts about this )
Warnings can be disabled with 'no warnings q/Text::Sprintf::Named/'
Subject: | mycopy-master.diff |
diff --git a/modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm b/modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm
index 871fde3..7d13af0 100644
--- a/modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm
+++ b/modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm
@@ -4,6 +4,7 @@ use warnings;
use strict;
use Carp;
+use warnings::register;
=head1 NAME
@@ -97,6 +98,10 @@ sub format
my $args = shift || {};
+ if ( (scalar keys %{$args}) > 0 && not exists $args->{args} ){
+ warnings::warnif( $self, 'Format parameters were specified, but none of them were \'args\', this is probably a mistake.' );
+ }
+
my $named_params = $args->{args} || {};
my $format = $self->_fmt;
@@ -140,7 +145,10 @@ The name of the conversion.
sub calc_param
{
my ($self, $args) = @_;
-
+ if ( not exists $args->{named_params}->{$args->{name}} ){
+ warnings::warnif($self, "Token '$args->{name}' specified in the format '$self->{_fmt}' was not found." );
+ return '';
+ }
return $args->{named_params}->{$args->{name}};
}
diff --git a/modules/Text-Sprintf-Named/t/03-incomplete.t b/modules/Text-Sprintf-Named/t/03-incomplete.t
new file mode 100644
index 0000000..d33867d
--- /dev/null
+++ b/modules/Text-Sprintf-Named/t/03-incomplete.t
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 8;
+use Test::Warn;
+use Text::Sprintf::Named;
+
+my $obj;
+
+#Test 1
+$obj = Text::Sprintf::Named->new( { fmt => 'No Tokens Here!', } );
+
+warnings_are { $obj->format() }[], 'No Tokens and No Parameters';
+
+#Test 2
+$obj = Text::Sprintf::Named->new( { fmt => "Example >%(name)s<", } );
+
+warning_like { $obj->format() } qr/Token 'name'/,
+ 'Missing Token Throws Warning ( String )';
+
+#Test 3
+$obj = Text::Sprintf::Named->new( { fmt => "Example >%(foo)8.3f<", } );
+
+warnings_like { $obj->format() }[ qr/Token 'foo'/, qr/numeric.*sprintf/ ],
+ 'Missing Token Throws Warning ( Float )';
+
+no warnings 'Text::Sprintf::Named';
+
+#Test 4
+$obj = Text::Sprintf::Named->new( { fmt => 'No Tokens Here!', } );
+
+warnings_are { $obj->format() }[], 'No Tokens and No Parameters';
+
+#Test 5
+$obj = Text::Sprintf::Named->new( { fmt => "Example >%(name)s<", } );
+
+warnings_are { $obj->format() }[],
+ '[Silent] Missing Token Throws Warning ( String )';
+
+#Test 6
+$obj = Text::Sprintf::Named->new( { fmt => "Example >%(foo)8.3f<", } );
+
+warnings_like { $obj->format() }[qr/numeric.*sprintf/],
+ '[Subdued] Missing Token Throws Warning ( Float )';
+
+#Test 7
+
+$obj = Text::Sprintf::Named->new( { fmt => '.' } );
+
+use warnings 'Text::Sprintf::Named';
+
+warning_like {
+ $obj->format(
+ {
+ erroneous_parameter => 'this one',
+ more_error => 'this',
+ this_will_never => 'work',
+ }
+ );
+}
+qr/Format parameters were specified, but none/,
+ 'Weird Format Parameters Throws Warning';
+
+# Test 8
+no warnings 'Text::Sprintf::Named';
+
+warnings_are {
+ $obj->format(
+ {
+ erroneous_parameter => 'this one',
+ more_error => 'this',
+ this_will_never => 'work',
+ }
+ );
+}
+[], '[Silenced] Weird Format Parameters Throws Warning';
+