Advanced-Config

 view release on metacpan or  search on metacpan

t/15-validate_multi_source_cfg.t  view on Meta::CPAN

   DBUG_PRINT ("----", "%s", "-"x50);

   my @sections = $cfg->find_sections ();
   my $cnt = @sections;
   dbug_is ($cnt, $total, "The config file defines the correct number of sections!  ($cnt)");

   my $s;
   foreach $s ( @sections ) {
      dbug_ok ( exists $validate->{$s}, "Found section '$s' in the validaton hash!" );
   }

   foreach $s ( sort keys %{$validate} ) {
      my $sect = $cfg->get_section ($s);

      dbug_ok (1, "-"x30);
      unless ( $sect ) {
         dbug_ok (0, "Found section '$s' in the config file!");
         next;
      }

      my @tag_list = $sect->find_tags ();
      my $cnt1 = @tag_list;
      my $cnt2 = keys %{$validate->{$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 $validate->{$s}->{$t} ) {
            dbug_ok (0, "Tag \"$_\" 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 %{$validate->{$s}} ) {
         my $val1 = $validate->{$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);
         }
      }
   }

   # 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);
}

# ==============================================------======================
# All tags & sections defined in the config files must be initialized below!
# The config file is: t/config/15-multi_source_01_main.cfg
# It's fairly complex based on how all it's sub-config files interact!

# NOTE: No tag may have undef as a value!
#       That it can't happen in this module if a tag is defined!
#       Undef means the tag doesn't exist instead!

sub init_validation_hash
{
   DBUG_ENTER_FUNC (@_);
   my $opts = shift;

   # Tags in the main section ...
   my %main = (  "main_01" => "Hello World!",
                 "hello"   => "again!",
                 "main_02" => "What's up Doc?",
                 "main_03" => "I'm hunting wrabits!",
                 "main_04" => "Good bye cruel world!"
              );

   # Tags in the Common section ...
   my %common = (  "common_01" => "Humpty Dumpty!",
                   "common_02" => "Sat on a wall!",
                   "common_03" => "He had a great fall!",
                   "common_04" => "They couldn't put humpty together again!"
                );

   # Tags in the Overwrite section ...
   my %overwrite = ( "overwrite" => "From file 04!" );

   # Common tags between different sections ...
   my %first  = ( "hello" => "1st!" );
   my %second = ( "hello" => "2nd?" );
   my %third  = ( "hello" => "3rd." );
   my %fourth = ( "hello" => "4th!?!" );

   # Sedction names points to the tag/value pairs found in them.
   my %sections = ( "main"      => \%main,
                    "common"    => \%common,
                    "overwrite" => \%overwrite,
                    "first"     => \%first,
                    "second"    => \%second,
                    "third"     => \%third,
                    "fourth"    => \%fourth,
                  );

   my $total = keys %sections;

   DBUG_RETURN ($total, \%sections);
}



( run in 0.772 second using v1.01-cache-2.11-cpan-d80b1682f3f )