Advanced-Config

 view release on metacpan or  search on metacpan

t/12-validate_sections.t  view on Meta::CPAN

#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use File::Basename;
use File::Spec;
use Sys::Hostname;
use Fred::Fish::DBUG 2.09 qw / on /;
use Fred::Fish::DBUG::Test 2.09;

# How to find the helper module ...
BEGIN { push (@INC, File::Spec->catdir (".", "t", "test-helper")); }
use helper1234;

my $fish;

# This program does a detailed validation of what's found
# in the t/config/12-*.cfg files.
# The 01 file sources in the others so they can be examined
# as a single Object!

BEGIN {
   $fish = turn_fish_on_off_for_advanced_config ();

   unlink ( $fish );

   DBUG_ENTER_FUNC ();

   use_ok ("Advanced::Config");

   DBUG_VOID_RETURN ();
}


END {
   DBUG_ENTER_FUNC ();
   # Don't do any tests in the END block ...
   DBUG_VOID_RETURN ();
}

# --------------------------------------
# Start of the main program!
# --------------------------------------
{
   # Turn fish on ...
   DBUG_PUSH ( $fish );

   DBUG_ENTER_FUNC (@ARGV);

   dbug_ok (1, "In the MAIN program ...");  # Test # 2 ...

   my $cfg1 = init_object ();     # Normal mode ...
   DBUG_PRINT ("====", "%s", "="x50);
   my $cfg2 = init_object (1);    # Inherit mode ...
   DBUG_PRINT ("====", "%s", "="x50);


   # Builds the hash to validate the config file against ...
   my ($valid_normal_cfg, $valid_inherit_cfg) = init_validation_hashes ();


   foreach my $idx ( 1, 2 ) {
      dbug_ok (1, "-"x40);

      my ($cfg, $valid, $mode);
      if ( $idx == 1 ) {
         ($cfg, $valid, $mode ) = ( $cfg1, $valid_normal_cfg, "normal" );
      } elsif ( $idx == 2 ) {
         ($cfg, $valid, $mode ) = ( $cfg2, $valid_inherit_cfg, "inherit" );
      } else {
         dbug_ok (0, "Valid Iteration ... ($idx)");
         next;
      }
      dbug_ok (1, "Processing mode: ${mode}");

      my @sections = $cfg->find_sections ();

      my $total = keys %{$valid};
      my $cnt = @sections;
      dbug_is ($cnt, $total, "The config file defines the correct number of sections!  ($cnt vs $total)");

      my $s;
      foreach $s ( @sections ) {
         dbug_ok ( exists $valid->{$s}, "Found section '$s' in the validaton hash!" );
      }
      foreach $s ( sort keys %{$valid} ) {
         my $sect = $cfg->get_section ($s);
         unless ( $sect ) {
            dbug_ok (0, "Found section '$s' in the config file!");
            next;
         }

         # Now let's validate the section contents ...
         my @tag_list = $sect->find_tags ();
         my $cnt1 = @tag_list;
         my $cnt2 = keys %{$valid->{$s}};
         dbug_is ($cnt1, $cnt2, "Section '$s' has the correct number of tag/value pairs!  ($cnt1)");

         # Validating the list of tags in the config file match what's in my validation hash.
         $cnt = 0;
         foreach my $t ( @tag_list ) {
            unless ( exists $valid->{$s}->{$t} ) {
               dbug_ok (0, "Tag \"$_\" also exists in the validation hash!");
               ++$cnt;
            }
         }
         dbug_is ($cnt, 0, "All tags were accounted for in the validation hash for section '$s'!");

         # Validating that my hash matches what's in the config file ...
         foreach my $t ( sort keys %{$valid->{$s}} ) {
            my $val1 = $valid->{$s}->{$t};
            my $val2 = $sect->get_value ( $t );
            my $chk  = (defined $val2) && ($val1 eq $val2);
            $val2 = (defined $val2) ? $val2 : "";
            dbug_ok ( $chk, "Validating tag \"$t\" in section \"$s\" matches config file.  ($val2)" );
            unless ( $chk ) {
               DBUG_PRINT ("ERROR", "Value should have been: %s", $val1);
            }
         }
      }  # End foreach $s loop ...
   }     # End foreach $idx loop ...

   # Since I didn't count the test cases, must end my program
   # with a call to this method.  Can't do this in END anymore!
   done_testing ();

   DBUG_LEAVE (0);
}

# ==============================================------======================
# Initialize the requested configuration ...

sub init_object
{
   DBUG_ENTER_FUNC (@_);
   my $inherit = shift || 0;

   my %gOpts;
   $gOpts{inherit} = 1  if ( $inherit );

   my $file = File::Spec->catfile ("t", "config", "12-use_sections.cfg");

   my $cfg;
   eval {
      $cfg = Advanced::Config->new ($file, undef, \%gOpts);
      dbug_ok (defined $cfg, "Advanced::Config object has been created!  (inherit => $inherit)");
      my $ldr = $cfg->load_config ();
      dbug_ok (defined $ldr, "Advanced::Config object has been loaded into memory!");
   };
   if ( $@ ) {
      unless (defined $cfg) {
         dbug_ok (defined $cfg, "Advanced::Config object has been created!  (inherit => $inherit)");
      }



( run in 0.657 second using v1.01-cache-2.11-cpan-39bf76dae61 )