Commits

kfre...@52c325ad-5fd0-0310-8a0f-c43feede02cc  committed 4356ac6

Adding Warnings for bad parameter use

  • Participants
  • Parent commits 28c00e6
  • Branches kfredric-playground-1

Comments (0)

Files changed (2)

File modules/Text-Sprintf-Named/lib/Text/Sprintf/Named.pm

 use strict;
 
 use Carp;
+use warnings::register;
 
 =head1 NAME
 
 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}};
 }
 

File modules/Text-Sprintf-Named/t/03-incomplete.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 6;
+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 )';