ConfigReader-Simple-1.28/0000755000076500000240000000000011213636675014213 5ustar brianstaffConfigReader-Simple-1.28/Changes0000644000076500000240000000562311213636674015513 0ustar brianstaffChanges for ConfigReader::Simple 1.28 - Tue Jun 9 23:57:40 2009 * Various distro fixes, correct license, and new Makemaker hotness. No need to upgrade. 1.27 - Wed Aug 20 12:33:43 2008 * Fixed bug in _validate_keys that looked at a directives value instead of checking for the directive name. This affected directives with perly false values when you gave a list of required keys to new(). 1.26 - Fri Nov 9 05:33:07 2007 * cleanups for the move from CVS to SVN, and for the latest kwalitee * no need to upgrade 1.25 - Fri Mar 16 18:09:27 2007 * Fixing up distro and verifying that all bugs in RT are fixed * A call to new() without args now doesn't try to open the null file 1.24 - Tue Jan 9 22:02:27 2007 * updated copyright and license info * no code changes, so no need to upgrade 1.23 - Mon Apr 10 09:30:54 2006 * Changed configuration directive parsing to accept [^\s=]+ instead of just \w+ * If you want to name your configuration directives with characters outside of \w, you should upgrade to this version * If your teammates are using config directives with characters outside of \w, they're going to make you upgrade. :) 1.21 - Wed Aug 24 18:37:34 2005 * Fixed string parsing bug which messed up on continuation lines * cloning an object now makes a deep copy, fixing another bug 1.20 * Removed all module dependencies except Test::More 1.18 * Tue Feb 10 20:46:05 2004 * Adam Trickett added support for continuation lines * I added docs for config file formats 1.17 - Sun Nov 30 11:12:50 2003 + some fix ups for the POD docs + fixed a problem in t/pod.t so the test finds the files + if you already have this installed, you don't need this update 1.15 - 22-Oct-2002 * new_multiple can keep going if it cannot read a file * added $Die, $Warn, $ERROR, %Error variables for settings * things that should croak do 1.14 - 29-Sep-2002 23:51 * cleaned up Makefile.PL * added prereq test 1.12 - 13-Jul-2002 21:13 * added add_config_file() to pull in more configuration directives after you have already created the object. 1.11 - 28-Apr-2002 11:11 * added new_multiple() to parse multiple files into the same object. 0.91 * you don't need to call parse() since new() does it for you * you can use the NAME=VALUE format in config files thanks to Alan W. Jurgensen * the module is now maintained by brian d foy 0.9 * Bumped up the version number because it's nearly mature * Now you can declare all, some or no keys and it will only die if a declared key is not found in the config file. (Kim Ryan ) 0.5 * Changed module name to ConfigReader::Simple to match up with other modules in CPAN better. 0.4 * Quote*delimiters now have to be matched, ie not 'foo" etc. 0.3 * Doesn't get confused by trailing whitespace * Copes with quote-delimited strings as long as they're all on the same line. ConfigReader-Simple-1.28/eg/0000755000076500000240000000000011213636675014606 5ustar brianstaffConfigReader-Simple-1.28/eg/config.desc0000644000076500000240000000014311213636674016710 0ustar brianstaffCamel What is the name of the camel? Llama Where is the home of the llama? Sum What is two and two?ConfigReader-Simple-1.28/eg/config_creator.pl0000644000076500000240000000234711213636674020134 0ustar brianstaff#!/usr/bin/perl =head1 NAME config_creator.pl - read a configuration description and prompt for value =head1 SYNOPSIS config_creator.pl description_file =head1 DESCRIPTION The config_creator.pl program reads a configuration description file and then prompts the user for values, creating a configuration file in the process. =head1 SOURCE AVAILABILITY This source is part of a SourceForge project which always has the latest sources in CVS, as well as all of the previous releases. https://sourceforge.net/projects/brian-d-foy/ If, for some reason, I disappear from the world, one of the other members of the project can shepherd this module appropriately. =head1 AUTHORS brian d foy, Ebdfoy@cpan.orgE =head1 COPYRIGHT Copyright (c) 2002-2004 brian d foy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut my $config = ''; while( <> ) { next if m/\s*#/; chomp; my( $directive, $description ) = split m/\s+/, $_, 2; my $answer = prompt( $description ); $config .= "$directive $answer\n"; } print $config; sub prompt { my $message = shift; print "$message> "; my $answer = ; chomp $answer; return $answer; }ConfigReader-Simple-1.28/eg/example.pl0000755000076500000240000000067211213636674016605 0ustar brianstaff#!/usr/bin/perl -w # # Testing ConfigReader::Simple # # Last updated by gossamer on Tue Sep 1 22:43:42 EST 1998 # use ConfigReader::Simple; my $config = ConfigReader::Simple->new("./example.config", [qw(Test1 Test2)]); $config->parse(); $config->_validate_keys(); # die if there are keys in example.config # that aren't declared. print "The value for directive Test1 is: " . $config->get("Test1") . "\n"; ConfigReader-Simple-1.28/examples/0000755000076500000240000000000011213636675016031 5ustar brianstaffConfigReader-Simple-1.28/examples/README0000644000076500000240000000010511213636674016704 0ustar brianstaffSee the tests in the t/ directory for examples until I add some more.ConfigReader-Simple-1.28/lib/0000755000076500000240000000000011213636675014761 5ustar brianstaffConfigReader-Simple-1.28/lib/Simple.pm0000644000076500000240000004171711213636674016561 0ustar brianstaffpackage ConfigReader::Simple; use strict; use warnings; no warnings; use subs qw(_init_errors); use vars qw($VERSION $AUTOLOAD %ERROR $ERROR $Warn $Die); use Carp qw(croak carp); use UNIVERSAL qw(isa); $Die = ''; $ERROR = ''; $VERSION = '1.28'; $Warn = 0; our $DEBUG = 0; my $Error = ''; sub SUCCESS() { 1 }; sub FAILURE() { 0 }; =head1 NAME ConfigReader::Simple - Simple configuration file parser =head1 SYNOPSIS use ConfigReader::Simple; # parse one file $config = ConfigReader::Simple->new("configrc", [qw(Foo Bar Baz Quux)]); # parse multiple files, in order $config = ConfigReader::Simple->new_multiple( Files => [ "global", "configrc" ], Keys => [qw(Foo Bar Baz Quux)] ); my @directives = $config->directives; $config->get( "Foo" ); if( $config->exists( "Bar" ) ) { print "Bar was in the config file\n"; } # copy an object to play with it separately my $clone = $config->clone; # only affects clone $clone->set( "Foo", "Buster" ); # save the config to a single file $clone->save( "configrc" ) # save the config to a single file, but only with # certain directives $clone->save( "configrc" => [qw(Foo Bar)] ) # save to multiple configuration files $clone->save( "configrc" => [qw(Foo Bar)], "global" => [qw(Baz Quux)], ); =head1 DESCRIPTION C reads and parses simple configuration files. It is designed to be smaller and simpler than the C module and is more suited to simple configuration files. =head2 The configuration file format The configuration file uses a line-oriented format, meaning that the directives do not have containers. The values can be split across lines with a continuation character, but for the most part everything ends up on the same line. The first group of non-whitespace characters is the "directive", or the name of the configuration item. The linear whitespace after that separates the directive from the "value", which is the rest of the line, including any other whitespace. In this example, the directive is "Camel" and the value is "Dromedary". Camel Dromedary Optionally, you can use a equal sign to separate the directive from the value. Camel=Dromedary The equal sign can also have whitespace on either or both sides. Camel = Dromedary Camel= Dromedary In the next example, the directive is "Llama" and the value is "Live from Peru" Llama Live from Peru This is the same, to C, as the following which has more whitespace between the directive and the value. Llama Live from Peru You can also enclose the value in single or double quotes. Llama "Live from Peru" Llama 'Live from Peru' Llama='Live from Peru' In some cases you may want to split the logical line across two lines, perhaps to see it better in a terminal window. For that, use a \ followed only by whitespace. To split the last entry across two lines, we use the \ at the end of the line. These three entries are the same: Llama Live from Peru Llama Live from \ Peru Llama Live \ from \ Peru If a line is only whitespace, or the first whitespace character is a #, the Perl comment character, C ignores the line unless it is the continuation of the previous line. =head2 Methods =over 4 =item new ( FILENAME, DIRECTIVES ) Creates a C object. C tells the instance where to look for the configuration file. If FILENAME cannot be found, an error message for the file is added to the %ERROR hash with the FILENAME as a key, and a combined error message appears in $ERROR. C is an optional argument and is a reference to an array. Each member of the array should contain one valid directive. A directive is the name of a key that must occur in the configuration file. If it is not found, the method croaks. The directive list may contain all the keys in the configuration file, a sub set of keys or no keys at all. The C method is really a wrapper around C. =cut sub new { my $class = shift; my $filename = shift; my $keyref = shift; $keyref = [] unless defined $keyref; my $self = $class->new_multiple( Files => [ defined $filename ? $filename : () ], Keys => $keyref ); return $self; } =item new_multiple( Files => ARRAY_REF, Keys => ARRAY_REF ) Create a configuration object from several files listed in the anonymous array value for the C key. The module reads the files in the same order that they appear in the array. Later values override earlier ones. This allows you to specify global configurations which you may override with more specific ones: ConfigReader::Simple->new_multiple( Files => [ qw( /etc/config /usr/local/etc/config /home/usr/config ) ], ); This function croaks if the values are not array references. If this method cannot read a file, an error message for that file is added to the C<%ERROR> hash with the filename as a key, and a combined error message appears in C<$ERROR>. Processing the list of filenames continues if a file cannot be found, which may produced undesired results. You can disable this feature by setting the C<$ConfigReader::Simple::Die> variable to a true value. =cut sub new_multiple { _init_errors(); my $class = shift; my %args = @_; my $self = {}; $args{'Keys'} = [] unless defined $args{'Keys'}; croak( __PACKAGE__ . ': Files argument must be an array reference') unless isa( $args{'Files'}, ref [] ); croak( __PACKAGE__ . ': Keys argument must be an array reference') unless isa( $args{'Keys'}, ref [] ); $self->{"filenames"} = $args{'Files'}; $self->{"validkeys"} = $args{'Keys'}; bless $self, $class; foreach my $file ( @{ $self->{"filenames"} } ) { my $result = $self->parse( $file ); croak $Error if( not $result and $Die ); $ERROR{$file} = $Error unless $result; } $ERROR = join "\n", map { $ERROR{$_} } keys %ERROR; return $self; } =item new_string( Strings => ARRAY_REF, Keys => ARRAY_REF ) Create a configuration object from several strings listed in the anonymous array value for the C key. The module reads the strings in the same order that they appear in the array. Later values override earlier ones. This allows you to specify global configurations which you may override with more specific ones: ConfigReader::Simple->new_strings( Strings => [ \$global, \$local ], ); This function croaks if the values are not array references. =cut sub new_string { _init_errors; my $class = shift; my %args = @_; my $self = {}; $args{'Keys'} = [] unless defined $args{'Keys'}; croak( __PACKAGE__ . ': Strings argument must be an array reference') unless isa( $args{'Strings'}, ref [] ); croak( __PACKAGE__ . ': Keys argument must be an array reference') unless isa( $args{'Keys'}, ref [] ); bless $self, $class; $self->{"strings"} = $args{'Strings'}; $self->{"validkeys"} = $args{'Keys'}; foreach my $string_ref ( @{ $self->{"strings"} } ) { croak( __PACKAGE__ . ': Element of Strings is not a scalar reference' ) unless isa( $string_ref, ref \ '' ); $self->parse_string( $string_ref ); } return $self; } =item add_config_file( FILENAME ) Parse another configuration file and add its directives to the current configuration object. Any directives already defined will be replaced with the new values found in FILENAME. =cut sub add_config_file { _init_errors; my( $self, $filename ) = @_; return unless $self->parse( $filename ); push @{ $self->{"filenames"} }, $filename; return 1; } =item files Return the list of configuration files associated with this object. The order of the return values is the order of parsing, so the first value is the first file parsed (and subsequent files may mask it). =cut sub files { @{ $_[0]->{"filenames"} } } =item new_from_prototype( Create a clone object. This is the same thing as calling clone(). =cut sub new_from_prototype { _init_errors; my $self = shift; my $clone = $self->clone; return $clone; } sub AUTOLOAD { my $self = shift; my $method = $AUTOLOAD; $method =~ s/.*:://; $self->get( $method ); } sub DESTROY { return 1; } =item parse( FILENAME ) This does the actual work. This is automatically called from C, although you can reparse the configuration file by calling C again. =cut sub parse { my( $self, $file ) = @_; $Error = ''; unless( open CONFIG, $file ) { $Error = "Could not open configuration file [$file]: $!"; carp $Error if $Warn; return; } $self->{"file_fields"}{$file} = []; while( ) { if ( s/\\ \s* $//x ) { $_ .= ; redo unless eof CONFIG; } chomp; next if /^\s*(#|$)/; my ($key, $value) = &parse_line($_); #carp "Key: '$key' Value: '$value'\n" if $DEBUG; $self->{"config_data"}{$key} = $value; push @{ $self->{"file_fields"}{$file} }, $key; } close(CONFIG); $self->_validate_keys; return 1; } =item parse_string( SCALAR_REF ) Parses the string inside the reference SCALAR_REF just as if it found it in a file. =cut sub parse_string { my $self = shift; my $string = shift; my @lines = split /\r?\n/, $$string; chomp( @lines ); # carp "A: Found " . @lines . " lines" if $DEBUG; while( my $line = shift @lines ) { # carp "1: Line is $line" if $DEBUG; CONT: { if ( $line =~ s/\\ \s* $//x ) { # carp "a: reading continuation line $lines[0]" if $DEBUG; $line .= shift @lines; # carp "b: Line is $line" if $DEBUG; redo CONT unless @lines == 0; } } # carp "2: Line is $line" if $DEBUG; chomp $line; next if $line =~ /^\s*(#|$)/; # carp "3: Line is $line" if $DEBUG; my ($key, $value) = &parse_line( $line ); # carp "Key: '$key' Value: '$value'" if $DEBUG; $self->{"config_data"}{$key} = $value; } $self->_validate_keys; return 1; } =item get( DIRECTIVE ) Returns the parsed value for that directive. For directives which did not have a value in the configuration file, C returns the empty string. =cut sub get { $_[0]->{"config_data"}{$_[1]} } =item set( DIRECTIVE, VALUE ) Sets the value for DIRECTIVE to VALUE. The DIRECTIVE need not already exist. This overwrites previous values. The VALUE must be a simple scalar. It cannot be a reference. If the VALUE is a reference, the function prints a warning and returns false. =cut sub set { my $self = shift; my( $key, $value ) = @_; if( ref $value ) { $ERROR = "Second argument to set must be a simple scalar"; if( $Warn ) { carp $ERROR; return; } elsif( $Die ) { croak $ERROR; } return; } $self->{"config_data"}{$key} = $value; } =item unset( DIRECTIVE ) Remove the value from DIRECTIVE, which will still exist. It's value is undef. If the DIRECTIVE does not exist, it will not be created. Returns FALSE if the DIRECTIVE does not already exist, and TRUE otherwise. =cut sub unset { my $self = shift; my $key = shift; return unless $self->exists( $key ); $self->{"config_data"}{$key} = undef; return 1; } =item remove( DIRECTIVE ) Remove the DIRECTIVE. Returns TRUE is DIRECTIVE existed and FALSE otherwise. =cut sub remove { my $self = shift; my $key = shift; return unless $self->exists( $key ); delete $self->{"config_data"}{$key}; return 1; } =item directives() Returns a list of all of the directive names found in the configuration file. The keys are sorted ASCII-betically. =cut sub directives { my $self = shift; my @keys = sort keys %{ $self->{"config_data"} }; return @keys; } =item exists( DIRECTIVE ) Return TRUE if the specified directive exists, and FALSE otherwise. =cut sub exists { my $self = shift; my $name = shift; return CORE::exists $self->{"config_data"}{ $name }; } =item clone Return a copy of the object. The new object is distinct from the original so you can make changes to the new object without affecting the old one. =cut # this is only the first stab at this -- from 35,000 # feet in coach class # # I expect that the hash will be very simple. Some keys # might have a reference value, but that reference value # will be "flat", so it won't have references in it. sub clone { my $self = shift; my $clone = bless {}, ref $self; $clone->{"filenames"} = [ @{ $self->{"filenames"} } ]; $clone->{"validkeys"} = [ @{ $self->{"validkeys"} } ]; foreach my $file ( keys %{ $self->{"file_fields"} } ) { $clone->{"file_fields"}{ $file } = [ @{ $self->{"file_fields"}{ $file } } ]; } foreach my $key ( $self->directives ) { $clone->set( $key, $self->get( $key ) ); } return $clone; } =item save( FILENAME [ => ARRAY_REF [, FILENAME => ARRAY_REF ] ] ); The save method works in three ways, depending on the argument list. With a single argument, the save function attempts to save all of the field-value pairs of the object to the file named by the argument. $clone->save( "configrc" ); With two arguments, the method expects the second argument to be an array reference which lists the directives to save in the file. $clone->save( "configrc" => [qw(Foo Bar)] ); With more than two arguments, the method expects filename-list pairs. The method will save in each file the values in their respective array references. $clone->save( "configrc" => [qw(Foo Bar)], "global" => [qw(Baz Quux)], ); In the last two cases, the method checks that the value for each pair is an array reference before it affects any files. It croaks if any value is not an array reference. Once the method starts writing files, it tries to write all of the specified files. Even if it has a problem with one of them, it continues onto the next one. The method does not necessarily write the files in the order they appear in the argument list, and it does not check if you specified the same file twice. =cut sub save { my $self = shift; my @args = @_; if( @args == 0 ) # no args! { carp "No arguments to method!"; return; } if( @args == 1 ) # this is a single file { push @args, [ $self->directives ]; } unless( @args % 2 == 0 ) { croak "Odd number of arguments" }; my %hash = @args; foreach my $value ( values %hash ) { croak "Argument is not an array reference" unless isa( $value, 'ARRAY' ); } foreach my $file ( keys %hash ) { carp $ERROR unless $self->_save( $file, $hash{$file} ); } 1; } sub _save { my( $self, $file, $directives ) = @_; unless( isa( $directives, ref [] ) ) { $ERROR = 'Argument is not an array reference'; return; } my $fh; unless( open $fh, ">", $file ) { $ERROR = $!; return; } foreach my $directive ( @$directives ) { print $fh ( join( "\t", $directive, $self->get( $directive ) ), "\n" ); } return SUCCESS; } =begin private =item parse_line( STRING ) Internal method. Don't call this directly. Takes a line of text and turns it into the directive and value. =end private =cut sub parse_line { return ( $1, $3 ) if $_[0] =~ / ^\s* ( [^\s=]+ ) \s* [=]? \s* (['"]?) (.*?) \2 \s* $/x; croak "Config: Can't parse line: $_[0]\n"; } sub _init_errors { %ERROR = (); $Error = undef; $ERROR = undef; } =begin private =item _validate_keys If any keys were declared when the object was constructed, check that those keys actually occur in the configuration file. This function croaks if a declared key does not exist. =end private =cut sub _validate_keys { my $self = shift; return SUCCESS unless exists $self->{"validkeys"}; croak "validkeys was not an array reference!" unless isa( $self->{"validkeys"}, ref [] ); my @keys = eval { @{ $self->{"validkeys"} } }; my @missing = grep { ! exists $self->{"config_data"}{$_} }@keys; croak "Config: required keys [@missing] do not occur in config" if @missing; return SUCCESS; } =back =head2 Package variables =over 4 =item $Die - DEPRECATED If set to a true value, all errors are fatal. =item $ERROR The last error message. =item %ERROR The error messages from unreadable files. The key is the filename and the value is the error message. =item $Warn - DEPRECATED If set to a true value, methods may output warnings. =back =head1 LIMITATIONS/BUGS Directives are case-sensitive. If a directive is repeated, the first instance will silently be ignored. =head1 CREDITS Bek Oberin C<< >> wote the original module Kim Ryan C<< >> adapted the module to make declaring keys optional. Thanks Kim. Alan W. Jurgensen C<< >> added a change to allow the NAME=VALUE format in the configuration file. Andy Lester, C<< >>, for maintaining the module while brian was on active duty. Adam Trickett, C<< >>, added multi-line support. You might want to see his C module. Greg White has been a very patient user and tester. =head1 SOURCE AVAILABILITY The source is in Github: http://github.com/briandfoy/ConfigReader-Simple/tree/master =head1 AUTHORS brian d foy, C<< >> =head1 COPYRIGHT AND LICENSE Copyright (c) 2002-2009 brian d foy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut 1; ConfigReader-Simple-1.28/LICENSE0000644000076500000240000000010611213636674015214 0ustar brianstaffYou can use ConfigReader::Simple under the same terms as Perl itself. ConfigReader-Simple-1.28/Makefile.PL0000644000076500000240000000166711213636674016176 0ustar brianstaffuse 5.006; use strict; use ExtUtils::MakeMaker; eval "use Test::Manifest 1.21"; WriteMakefile( 'NAME' => 'ConfigReader::Simple', 'VERSION_FROM' => 'lib/Simple.pm', 'ABSTRACT' => 'Simple configuration file parser', 'LICENSE' => 'perl', 'AUTHOR' => 'brian d foy ', 'PREREQ_PM' => { 'Test::Warn' => '0', 'Test::Output' => '0', }, 'PM' => { 'lib/Simple.pm' => '$(INST_LIBDIR)/Simple.pm', }, ( $ExtUtils::MakeMaker::VERSION ge '6.48' ? ( MIN_PERL_VERSION => 5.006, META_MERGE => { resources => { repository => 'git://github.com/briandfoy/test-output.git', }, keywords => ['testing','STDOUT','STDERR'], }, ) : () ), 'MAN3PODS' => { 'lib/Simple.pm' => '$(INST_MAN3DIR)/ConfigReader::Simple.$(MAN3EXT)', }, clean => { FILES => "ConfigReader-Simple-*" }, ); ConfigReader-Simple-1.28/MANIFEST0000644000076500000240000000101511213636675015341 0ustar brianstaffChanges eg/config.desc eg/config_creator.pl eg/example.pl examples/README lib/Simple.pm LICENSE Makefile.PL MANIFEST README t/_save.t t/add_config_file.t t/clone.config t/clone.t t/eof.config t/example.config t/exists.t t/get.t t/global.config t/load.t t/new_multiple.t t/new_string.t t/parse.t t/parse_line.t t/parse_string.t t/pod.t t/pod_coverage.t t/prereq.t t/remove.t t/save.t t/set.t t/test_manifest t/unparseable.config t/validate_keys.t META.yml Module meta-data (added by MakeMaker) ConfigReader-Simple-1.28/META.yml0000664000076500000240000000132011213636675015462 0ustar brianstaff--- #YAML:1.0 name: ConfigReader-Simple version: 1.28 abstract: Simple configuration file parser author: - brian d foy license: perl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: perl: 5.006 Test::Output: 0 Test::Warn: 0 resources: repository: git://github.com/briandfoy/test-output.git no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.50 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 keywords: - testing - STDOUT - STDERR ConfigReader-Simple-1.28/README0000644000076500000240000000076511213636674015102 0ustar brianstaffYou can install this using in the usual Perl fashion perl Makefile.PL make make test make install The documentation is in the module file. Once you install the file, you can read it with perldoc. perldoc ConfigReader::Simple If you want to read it before you install it, you can use perldoc directly on the module file. perldoc lib/ConfigReader/Simple.pm This module is also in Github http://github.com/briandfoy/ConfigReader-Simple/tree/master Enjoy, brian d foy, bdfoy@cpan.org ConfigReader-Simple-1.28/t/0000755000076500000240000000000011213636675014456 5ustar brianstaffConfigReader-Simple-1.28/t/_save.t0000644000076500000240000000274511213636674015747 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; use Data::Dumper; use File::Spec; my $null_file = File::Spec->devnull; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = '_save'; use_ok( $class ); can_ok( $class, $method ); my $config = $class->new_string( Strings => [ \ 'Cat Buster' ] ); isa_ok( $config, $class ); is( $config->get( 'Cat' ), 'Buster' ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test _save without a list of directives. It should fail { my $rc = $config->_save( $null_file ); ok( ! $rc, "return value is false without list of directives" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test _save with list of directives, but not as an array ref { my $rc = $config->_save( $null_file, 'Cat' ); ok( ! $rc, "return value is false without list of directives" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test _save with a file name I can't open. Should fail { my $filename = "/a/b/c/d/e/f/1/2/3/4/5/buster"; my $rc = $config->_save( $filename, [ qw( Cat ) ] ); ok( ! $rc, "return value is false without list of directives" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test _save with a file name I can open. Should work { my $rc = $config->_save( $null_file, [ qw(Cat) ] ); ok( $rc, "return value is true without list of directives" ); }ConfigReader-Simple-1.28/t/add_config_file.t0000644000076500000240000000176211213636674017724 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; use File::Spec::Functions qw(catfile); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'add_config_file'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test with a file that does not exist { my $filename = 'a/b/c'; ok( ! -e $filename, "Missing file is actually missing" ); my $mock = bless {}, $class; is( $mock->$method( $filename ), undef, "$method with missing file fails" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test with a file that isn't parseable { no warnings 'redefine'; no strict 'refs'; local *{ "${class}::parse" } = sub { 0 }; my $filename = catfile( qw(t example.config) ); ok( -e $filename, "$filename exists" ); my $mock = bless {}, $class; is( $mock->$method( $filename ), undef, "$method with empty file fails" ); }ConfigReader-Simple-1.28/t/clone.config0000644000076500000240000000017011213636674016742 0ustar brianstaff# A Comment Test1 Test 1 value Test2 Test 2 value Test3=foo Test4=bar Zero 0 Undef Test10 foo \ bar \ baz #Faketest ConfigReader-Simple-1.28/t/clone.t0000644000076500000240000000410411213636674015741 0ustar brianstaffuse Test::More tests => 17; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; use_ok( $class ); can_ok( $class, qw(clone new_from_prototype) ); my @Directives = qw( Test1 Test2 Test3 Test4 ); my $config = $class->new( "t/example.config", \@Directives ); isa_ok( $config, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # can we clone the object? # this should be a deep copy my $clone = $config->clone; isa_ok( $clone, $class ); my $proto = $config->new_from_prototype; isa_ok( $proto, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # can we change the clone without affecting the original? { my $Test1_value = 'Kundry'; $clone->set( 'Test1', $Test1_value ); is( $clone->Test1, $Test1_value, 'Cloned object has right value after change' ); isnt( $config->Test1, $Test1_value, 'Original object has right value after clone change' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # can we change the original without affecting the clone? { my $Test2_value = 'Second Squire'; $config->set( 'Test2', $Test2_value ); is( $config->Test2, $Test2_value, 'Original object has right value after change' ); isnt( $clone->Test2, $Test2_value, 'Clone object has right value after original change' ); my @files = $config->files; my @clone_files = $clone->files; is( scalar @files, 1, "Original object has 1 associated file" ); is( scalar @clone_files, 1, "Clone object has 1 associated file" ); is( $files[-1], "t/example.config", "Original object returns right file" ); is( $clone_files[-1], "t/example.config", "Clone object returns right file" ); $config->add_config_file( 't/clone.config' ); @files = $config->files; @clone_files = $clone->files; is( scalar @files, 2, "Original object has 1 associated file" ); is( scalar @clone_files, 1, "Clone object has 1 associated file" ); is( $files[-1], "t/clone.config", "Original object returns right file" ); is( $clone_files[-1], "t/example.config", "Clone object returns right file" ); } ConfigReader-Simple-1.28/t/eof.config0000644000076500000240000000014311213636674016413 0ustar brianstaff# This file should end with a continuation line that is not # actually continued Cat Buster Kitty \ConfigReader-Simple-1.28/t/example.config0000644000076500000240000000017011213636674017275 0ustar brianstaff# A Comment Test1 Test 1 value Test2 Test 2 value Test3=foo Test4=bar Zero 0 Undef Test10 foo \ bar \ baz #Faketest ConfigReader-Simple-1.28/t/exists.t0000644000076500000240000000217711213636674016170 0ustar brianstaffuse Test::More tests => 20; use File::Spec::Functions qw(catfile); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'exists'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my @Directives = qw( Test1 Test2 Test3 Test4 ); my $config = $class->new( catfile( qw(t example.config) ), \@Directives ); isa_ok( $config, $class ); can_ok( $config, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # these directives should be okay { foreach my $directive ( @Directives ) { ok( $config->$method( $directive ), "Directive [$directive] exists" ); } } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # these directives should not be okay { my @Directives = qw( Test5 exists blah get ); foreach my $directive ( @Directives ) { ok( ! $config->$method( $directive ), "$method fails for $directive"); ok( ! $config->get( $directive ), "get( $directive ) fails" ); ok( ! $config->$directive, "->$method fails" ); } } ConfigReader-Simple-1.28/t/get.t0000644000076500000240000001051011213636674015416 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; use File::Spec::Functions qw(catfile); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'get'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my @Directives = qw( Test1 Test2 Test3 Test4 ); my $config = $class->new( "t/example.config", \@Directives ); isa_ok( $config, $class ); # get things that do exist is( $config->get( 'Test3' ), 'foo', 'Test3 has right value' ); is( $config->Test3, 'foo' ); is( $config->get( 'Test2' ), 'Test 2 value', 'Test2 has right value' ); is( $config->Test2, 'Test 2 value' ); # get things that do exist, but look like false values to perl is( $config->get( 'Zero' ), '0', 'Zero has right value as string' ); is( $config->get( 'Zero' ), 0,, 'Zero has right value as number' ); is( $config->get( 'Undef' ), '', 'Undef has right value (empty)' ); # get long lines is( $config->get( 'Test10' ), 'foo bar baz', 'Continuation line has right value'); is( $config->Test10, 'foo bar baz') ; # get things that do not exist # using get my $value = not defined $config->get( 'Test' ); ok( $value, 'Test has no value with get()' ); $value = not defined $config->Test; ok( $value, 'Test has no value with AUTOLOAD' ); $value = not defined $config->get( 'Test5' ); ok( $value, 'Test5 has no value with get()' ); $value = not defined $config->Test5; ok( $value, 'Test5 has no value with AUTOLOAD' ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Now try it with multiple files $config = $class->new_multiple( Files => [ qw( t/global.config t/example.config ) ] ); isa_ok( $config, $class ); # get things that do exist is( $config->get( 'Test3' ), 'foo', 'Test3 has right value with AUTOLOAD' ); is( $config->get( 'Scope' ), 'Global', 'Scope has right value with AUTOLOAD' ); is( $config->get( 'Test2' ), 'Test 2 value', 'Test2 has right value with AUTOLOAD' ); is( $config->get( 'Test10'), 'foo bar baz', 'Test10 has right value with AUTOLOAD' ); # try it one at a time { my $config = $class->new( "t/example.config" ); is( $config->get( 'Test3' ), 'foo', 'Test3 has right value with get(), before global' ); is( $config->get( 'Test2' ), 'Test 2 value', 'Test2 has right value with get(), before global' ); is( $config->get( 'Test10' ), 'foo bar baz', 'Test10 has right value with get), before global' ); my $global = catfile( qw( t global.config ) ); ok( -e $global, "$global exists" ); ok( $config->add_config_file( $global), "Added $global" ); is( $config->get( 'Scope' ), 'Global', "Scope has right value after adding $global" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Now try it with multiple files with one missing $config = $class->new_multiple( Files => [ qw( t/missing.config t/global.config ) ] ); isa_ok( $config, $class ); # get things that do exist is( $config->get( 'Scope' ), 'Global', 'Scope has right value with AUTOLOAD, missing file' ); # config should be undef $config = eval { no strict 'refs'; ${ "${class}::Die" } = 1; $class->new_multiple( Files => [ qw( t/missing.config t/example.config ) ] ); }; like( $@, qr|\QCould not open configuration file [t/missing.config]| ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Now try it with a string { my $string = <<'STRING'; TestA Lear TestB MacBeth TestC Richard STRING $config = $class->new_string( Strings => [ \$string ] ); isa_ok( $config, $class ); is( $config->get( 'TestA' ), 'Lear', 'TestA has right value (from string)' ); is( $config->get( 'TestB' ), 'MacBeth', 'TestB has right value (from string)' ); is( $config->get( 'TestC' ), 'Richard', 'TestC has right value (from string)' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Now try it with a string with a continuation line { my $string = <<'STRING'; TestA King \ Lear TestB MacBeth TestC Richard STRING $config = $class->new_string( Strings => [ \$string ] ); isa_ok( $config, $class ); is( $config->get( 'TestA' ), 'King Lear', 'TestA has right value (from string)' ); is( $config->get( 'TestB' ), 'MacBeth', 'TestB has right value (from string)' ); is( $config->get( 'TestC' ), 'Richard', 'TestC has right value (from string)' ); } ConfigReader-Simple-1.28/t/global.config0000644000076500000240000000003211213636674017077 0ustar brianstaffTest3 Global Scope Global ConfigReader-Simple-1.28/t/load.t0000644000076500000240000000017511213636674015564 0ustar brianstaffuse Test::More tests => 1; print "Bail out! ConfigReader::Simple did not compile" unless use_ok( 'ConfigReader::Simple' ); ConfigReader-Simple-1.28/t/new_multiple.t0000644000076500000240000000264111213636674017351 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'new_multiple'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test with no arguments { my $rc = eval{ $class->$method() }; my $at = $@; ok( length $at, "eval fails with no arguments" ); like( $at, qr/must be an array reference/, "reports that it must be an array ref" ); like( $at, qr/Files/, "reports that Files is the problem" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test with Files argument which is not an array ref { my $rc = eval{ $class->$method( Files => 'a b c' ) }; my $at = $@; ok( length $at, "eval fails with no arguments" ); like( $at, qr/must be an array reference/, "reports that it must be an array ref" ); like( $at, qr/Files/, "reports that Files is the problem" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test with Files arguments which is an array ref, Keys that isn't { my $rc = eval{ $class->$method( Files => [], Keys => 'a b c' ) }; my $at = $@; ok( length $at, "eval fails with no arguments" ); like( $at, qr/must be an array reference/, "reports that it must be an array ref" ); like( $at, qr/Keys/, "reports that Keys is the problem" ); }ConfigReader-Simple-1.28/t/new_string.t0000644000076500000240000000411711213636674017024 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'new_string'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it works with both Strings and Keys # Has all valid keys { my $key = 'Cat'; my $cat = 'Buster'; my $config = $class->$method( Strings => [ \ "$key = $cat" ], Keys => [ $key ], ); isa_ok( $config, $class ); is( $config->get( $key ), $cat ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it works with only Strings { my $key = 'Cat'; my $cat = 'Mimi'; my $config = $class->$method( Strings => [ \ "$key = $cat" ], ); isa_ok( $config, $class ); is( $config->get( $key ), $cat ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it croaks with no arguments { eval { $class->$method() }; my $at = $@; ok( defined $at, "$method fails with no arguments" ); like( $at, qr/must be an array reference/ ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it croaks with Strings containing a non-scalar ref { eval { $class->$method( Strings => [ 'Foo = Bar' ] ) }; my $at = $@; ok( defined $at, "$method fails when string contains a non-reference" ); like( $at, qr/not a scalar reference/ ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it croaks when Strings value is not an array ref { eval { $class->$method( Strings => '', Keys => [] ) }; my $at = $@; ok( defined $at, "$method fails when Keys value is the empty string" ); like( $at, qr/must be an array reference/ ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it croaks when Keys value is not an array ref { eval { $class->$method( Strings => [ ], Keys => '' ) }; my $at = $@; ok( defined $at, "$method fails when Keys value is the empty string" ); like( $at, qr/must be an array reference/ ); } ConfigReader-Simple-1.28/t/parse.t0000644000076500000240000000540411213636674015757 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; use Test::Output; use File::Spec::Functions qw(catfile); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'parse'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a missing file, Warn off, Die off # should return nothing, but with no warning { my $missing = 'a/b/c'; ok( ! -e $missing, "Missing file is actually missing" ); local $ConfigReader::Simple::Warn = 0; local $ConfigReader::Simple::Die = 0; my $mock = bless {}, $class; isa_ok( $mock, $class ); can_ok( $mock, $method ); ok( ! $mock->parse( $missing ), 'parse fails with missing file, $Warn and $Die off' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a missing file, Warn off, Die on # should return nothing, but with no warning { my $missing = 'a/b/c'; ok( ! -e $missing, "Missing file is actually missing" ); local $ConfigReader::Simple::Warn = 0; local $ConfigReader::Simple::Die = 1; my $mock = bless {}, $class; isa_ok( $mock, $class ); can_ok( $mock, $method ); ok( ! $mock->parse( $missing ), 'parse fails with missing file, $Warn and $Die off' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a missing file, Warn on, Die off # should return nothing, but with a warning { my $missing = 'a/b/c'; ok( ! -e $missing, "Missing file is actually missing" ); local $ConfigReader::Simple::Warn = 1; local $ConfigReader::Simple::Die = 0; local $SIG{__WARN__} = sub { print STDERR @_ }; my $mock = bless {}, $class; isa_ok( $mock, $class ); can_ok( $mock, $method ); stderr_like { $mock->parse( $missing ) } qr/Could not open/, "parse fails with missing file"; } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a missing file, Warn on, Die on # should return nothing, but with a warning { my $missing = 'a/b/c'; ok( ! -e $missing, "Missing file is actually missing" ); local $ConfigReader::Simple::Warn = 1; local $ConfigReader::Simple::Die = 1; local $SIG{__WARN__} = sub { print STDERR @_ }; my $mock = bless {}, $class; isa_ok( $mock, $class ); can_ok( $mock, $method ); stderr_like { $mock->parse( $missing ) } qr/Could not open/, "parse fails with missing file"; } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a file that ends on a continuation line that is uncontinued # should still work { my $file = catfile( qw(t eof.config) ); ok( -e $file, "File exists" ); my $mock = bless {}, $class; isa_ok( $mock, $class ); can_ok( $mock, $method ); ok( $mock->parse( $file ), 'parsing file is just fine' ); } ConfigReader-Simple-1.28/t/parse_line.t0000644000076500000240000000156511213636674016772 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'parse_line'; use_ok( $class ); ok( defined &ConfigReader::Simple::parse_line, "$method is defined" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a line that should work { my( $directive, $value ) = &ConfigReader::Simple::parse_line( "Cat = Buster" ); is( $directive, 'Cat' ); is( $value, 'Buster' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Try it with a line that should fails { my $rc = eval { &ConfigReader::Simple::parse_line( "" ) }; my $at = $@; ok( length $at, "eval of parse_line failed with empty string" ); like( $at, qr/Can't parse/, "Reports that it can't parse the string" ); }ConfigReader-Simple-1.28/t/parse_string.t0000644000076500000240000000144011213636674017341 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = 'parse_string'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # { my $string = <<"HERE"; cat Buster dog \\ Tuffy bird Poppy # comment = hello kitty \\ Mimi HERE my $config = $class->new(); $config->parse_string( \$string ); is( $config->get( "cat" ), "Buster" ); is( $config->get( "kitty" ), "Mimi" ); is( $config->get( "dog" ), "Tuffy" ); is( $config->get( "bird" ), "Poppy" ); is( $config->get( "comment" ), undef ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ConfigReader-Simple-1.28/t/pod.t0000644000076500000240000000020111213636674015415 0ustar brianstaffuse Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); ConfigReader-Simple-1.28/t/pod_coverage.t0000644000076500000240000000040611213636674017277 0ustar brianstaffuse Test::More; eval "use Test::Pod::Coverage"; if( $@ ) { plan skip_all => "Test::Pod::Coverage required for testing POD"; } else { plan tests => 1; pod_coverage_ok( "ConfigReader::Simple", { trustme => [ qr/^[A-Z_]+$/, qr/parse_line/ ] } ); } ConfigReader-Simple-1.28/t/prereq.t0000644000076500000240000000017211213636674016140 0ustar brianstaffuse Test::More; eval "use Test::Prereq"; plan skip_all => "Test::Prereq required to test dependencies" if $@; prereq_ok();ConfigReader-Simple-1.28/t/remove.t0000644000076500000240000000112011213636674016131 0ustar brianstaffuse Test::More tests => 5; use ConfigReader::Simple; my @Directives = qw( Test1 Test2 Test3 Test4 ); my $config = ConfigReader::Simple->new( "t/example.config", \@Directives ); isa_ok( $config, 'ConfigReader::Simple' ); # remove things that do exist is( $config->get( 'Test3' ), 'foo', 'Test3 has the right value' ); ok( $config->remove( 'Test3' ), 'Test3 is removed' ); my $value = not $config->exists( 'Test3' ); ok( $value, 'Test3 no longer exists' ); # remove things that do not exist $value = not $config->remove( 'Tenor' ); ok( $value, 'Tenor does not exist, but was removed' ); ConfigReader-Simple-1.28/t/save.t0000644000076500000240000000453711213636674015611 0ustar brianstaffuse Test::More tests => 22; use Test::Warn; use File::Spec::Functions qw(catfile); my $class = 'ConfigReader::Simple'; use_ok( $class ); can_ok( $class, qw( save ) ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my @Directives = qw( Test1 Test2 Test3 Test4 ); my @Files = map { catfile( "t" , "$_.config" ) } qw(save_test1 save_test2); unlink @Files; my $config = $class->new( catfile( qw( t example.config) ), \@Directives ); isa_ok( $config, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # test with no values { my $result; warning_like { $result = $config->save() } qr/No arguments to method/, 'No arguments emits warning'; ok( ! defined $result, 'No arguments return undef' ); is( $@, '', 'There is nothing in $@ for a warning' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # test with one value { ok( ! -e $Files[0], 'File does not exist' ); my $result = $config->save( $Files[0] ); ok( $result, 'Single argument returns true value' ); ok( -e $Files[0], 'File exists' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # test with two values foreach my $value ( undef, 'foo', {}, 0 ) { my $result = eval { $config->save( $Files[0] => $value ) }; ok( ! defined $result, 'Bad arguments return undef' ); like( $@, qr/Argument is not an array reference/, 'Error string says the right thing' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # test with three values { my $result = eval { $config->save( $Files[0] => [], $Files[1] ) }; ok( ! defined $result, 'Three arguments return undef' ); like( $@, qr/Odd number of arguments/ ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # test with four values { my $result = eval { $config->save( $Files[0] => [ qw( Test1 Test2 ) ], $Files[1] => [ qw( Zero Test3 ) ] ) }; ok( $result, 'Four arguments return true' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # test when _save fails { no warnings 'redefine'; no warnings 'once'; no strict 'refs'; local *{ "${class}::_save" } = sub { $ConfigReader::Simple::ERROR = 'Oops'; 0 }; ok( ! $class->_save, "_save mock fails as it should" ); warning_like { $config->save( $Files[0] => [ qw( Test1 Test2 ) ] ) } qr/Oops/; } unlink @Files; ConfigReader-Simple-1.28/t/set.t0000644000076500000240000000534211213636674015441 0ustar brianstaffuse Test::More tests => 19; use Test::Output; my $class = 'ConfigReader::Simple'; use_ok( $class ); my @Directives = qw( Test1 Test2 Test3 Test4 ); my $config = $class->new( "t/example.config", \@Directives ); isa_ok( $config, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # set things that do exist foreach my $pair ( [ qw(Test1 Foo)] , [ qw(Pagagena Papageno) ], [ qw(Tamino Pamina) ] ) { my $key = $pair->[0]; my $value = $pair->[1]; $config->set( $key, $value ); is( $config->get( $key ), $value, "$key has the right value with get" ); is( $config->$key, $value, "$key has the right value with autoload" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Setting things to references should fail: With $Die set { no warnings 'once'; local $ConfigReader::Simple::Die = 1; my $config = $class->new; isa_ok( $config, $class ); my $rc = eval { $class->set( 'Cat', \ 'Buster' ) }; my $at = $@; ok( length $at, '$@ is set while trying to set with a scalar reference' ); like( $at, qr/must be a simple scalar/ ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Setting things to references should fail: With $Die not set, $Warn set { no warnings 'once'; local $ConfigReader::Simple::Die = undef; local $ConfigReader::Simple::Warn = 1; local $SIG{__WARN__} = sub { print STDERR @_ }; my $config = $class->new; isa_ok( $config, $class ); stderr_like { $class->set( 'Cat', \ 'Buster' ) } qr/must be a simple scalar/, 'set complains when $Warn is set and given a reference'; } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Setting things to references should fail: With $Die not set, $Warn not set { no warnings 'once'; local $ConfigReader::Simple::Die = undef; local $ConfigReader::Simple::Warn = undef; local $SIG{__WARN__} = sub { print STDERR @_ }; my $config = $class->new; isa_ok( $config, $class ); stderr_like { $class->set( 'Cat', \ 'Buster' ) } qr/^$/, 'set silent when $Warn, $Die are unset and given a reference'; } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # unset things that do exist { my $directive = 'Test2'; ok( $config->unset( $directive ), "Unset thing that exists [$directive]" ); my $not_defined = not defined $config->$directive; ok( $not_defined, "Unset thing [$directive] still has value" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # unset things that do not exist { my $directive = 'Tenor'; my $value = not $config->unset( $directive ); ok( $value, 'Unset thing that does not exist [$directive]' ); $value = not $config->exists( $directive ); ok( $value, 'Unset thing that did not exist [$directive] exists' ); } ConfigReader-Simple-1.28/t/test_manifest0000644000076500000240000000027411213636674017250 0ustar brianstaffload.t pod.t pod_coverage.t prereq.t validate_keys.t parse.t parse_line.t parse_string.t new_multiple.t add_config_file.t exists.t get.t set.t remove.t clone.t _save.t save.t new_string.t ConfigReader-Simple-1.28/t/unparseable.config0000644000076500000240000000000011213636674020133 0ustar brianstaffConfigReader-Simple-1.28/t/validate_keys.t0000644000076500000240000000242211213636674017466 0ustar brianstaff#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # my $class = 'ConfigReader::Simple'; my $method = '_validate_keys'; use_ok( $class ); can_ok( $class, $method ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it works if validkeys is not set { my $config = bless {}, $class; # a mock, nothing set ok( $config->$method(), "$method returns true without validkeys set" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it works if validkeys is set, but not an array reference { my $config = bless { validkeys => 4 }, $class; # a mock, nothing set eval { $config->$method() }; my $at = $@; ok( length $at, 'eval fails when validkeys is not an aray ref' ); like( $at, qr/not an array reference/, "Passing non array ref fails" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test that it works if validkeys is set, but missing in config { my $config = bless { validkeys => [ qw(Buster) ] }, $class; # a mock, nothing set eval { $config->$method() }; my $at = $@; ok( length $at, 'eval fails when Buster directive is missing' ); like( $at, qr/do not occur/, "Missing key fails" ); }