[libcode-tidyall-perl] 15/374: check param validity

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:25:40 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcode-tidyall-perl.

commit c5d9a33de3664dc39cbfdbdf50963673d8b2941c
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Tue Jun 12 13:44:40 2012 -0700

    check param validity
---
 lib/Code/TidyAll.pm         |   36 +++++++++++++++++++++++++-----------
 lib/Code/TidyAll/t/Basic.pm |    9 +++++++++
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 8616e8e..7d53599 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -10,18 +10,23 @@ use Time::Duration::Parse qw(parse_duration);
 use strict;
 use warnings;
 
+sub valid_params {
+    return qw(
+      backup_ttl
+      conf_file
+      data_dir
+      no_backups
+      no_cache
+      plugins
+      recursive
+      root_dir
+      verbose
+    );
+}
+my %valid_params_hash;
+
 # Incoming parameters
-use Object::Tiny qw(
-  backup_ttl
-  conf_file
-  data_dir
-  no_backups
-  no_cache
-  plugins
-  recursive
-  root_dir
-  verbose
-);
+use Object::Tiny ( valid_params() );
 
 # Internal
 use Object::Tiny qw(
@@ -35,6 +40,15 @@ sub new {
     my $class  = shift;
     my %params = @_;
 
+    # Check param validity
+    #
+    my $valid_params_hash = $valid_params_hash{$class} ||=
+      { map { ( $_, 1 ) } $class->valid_params() };
+    if ( my @bad_params = grep { !$valid_params_hash->{$_} } keys(%params) ) {
+        die sprintf( "unknown constructor param(s) %s",
+            join( ", ", sort map { "'$_'" } @bad_params ) );
+    }
+
     # Read params from conf file, if provided; handle .../ upward search syntax
     #
     if ( my $conf_file = delete( $params{conf_file} ) ) {
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index ec44aea..183bba8 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -134,6 +134,15 @@ sub test_errors : Tests {
     my $root_dir = $self->create_dir( { "foo/bar.txt" => "abc" } );
     throws_ok { Code::TidyAll->new( root_dir => $root_dir ) } qr/conf_file or plugins required/;
     throws_ok { Code::TidyAll->new( plugins  => {} ) } qr/conf_file or root_dir required/;
+    throws_ok {
+        Code::TidyAll->new(
+            root_dir    => $root_dir,
+            plugins     => {},
+            bad_param   => 1,
+            worse_param => 2
+        );
+    }
+    qr/unknown constructor param\(s\) 'bad_param', 'worse_param'/;
     throws_ok { Code::TidyAll->new( root_dir => $root_dir, plugins => { 'DoesNotExist' => {} } ) }
     qr/could not load plugin class/;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcode-tidyall-perl.git



More information about the Pkg-perl-cvs-commits mailing list