Advanced-Config

 view release on metacpan or  search on metacpan

t/75-check_all_languages.t  view on Meta::CPAN

   dbug_ok (1, "---------------------- Done! ------------------------------");

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

   DBUG_LEAVE (0);
}

# --------------------------------------------------------------------
# Create a new Advanced::Config objact ...
# --------------------------------------------------------------------
sub build_new_object
{
   DBUG_ENTER_FUNC (@_);
   my $mode_utf8 = shift;     # 0 or 1.
   my $lbl       = shift;     # Initial, Encrypted or Decrypted
   my $pause     = shift;     # 0 or 1.
   my $file      = shift;

   my $cfg = Advanced::Config->new ( $file,
                                     { croak => 1,    use_utf8 => $mode_utf8,
                                       dbug_test_use_case_parse_override  => 1,
                                       dbug_test_use_case_hide_override   => 1,
                                       disable_variable_modifiers => 1 },
                                     { required => 0, date_enable_yy => 1 },
                                     { }
                                   );

   my $type = $mode_utf8 ? "UTF-8" : "normal";
   dbug_isa_ok ( $cfg, "Advanced::Config" );
   dbug_isa_ok ( pause_load ($cfg, $pause), "Advanced::Config" );

   DBUG_RETURN ( $cfg );
}

# --------------------------------------------------------------------
sub pause_load
{
   DBUG_ENTER_FUNC (@_);
   my $cfg   = shift;
   my $pause = shift;

   DBUG_PAUSE ()  if ( $pause );

   DBUG_RETURN ( $cfg->load_config () );
}

# --------------------------------------------------------------------
# Compares 2 Advanced::Config objects and verify they are the same!
# Stops on any error encountered.
# --------------------------------------------------------------------
sub compare_objects
{
   DBUG_ENTER_FUNC (@_);
   my $cfg_src = shift;
   my $cfg_dst = shift;

   DBUG_PAUSE ()  unless ( $run_as_developer );

   my @src = $cfg_src->find_sections ();
   my @dst = $cfg_src->find_sections ();
   unless (dbug_is ( $#src, $#dst, "Both objects have the same number of sections in them!" )) {
      return DBUG_RETURN (0);
   }

    my $stop = 0;
   foreach (@src) {
      my $sCfg1 = $cfg_src->get_section ( $_ );
      my $sCfg2 = $cfg_dst->get_section ( $_ );

      my $sts = ( $sCfg1 && $sCfg2 ) ? 1 : 0;
      unless (dbug_ok ($sts, "Section '$_' exists in both objects!")) {
         $stop = 1;
         last;
      }

      my @tags1 = $sCfg1->find_tags ();
      my @tags2 = $sCfg2->find_tags ();
      unless (dbug_is ( $#tags1, $#tags2, "Both copies of section '$_' have the same number of tags in them!")) {
         $stop = 1;
         last;
      }

      foreach my $t (@tags1) {
         my $val1 = $sCfg1->get_value ($t);
         my $val2 = $sCfg2->get_value ($t);
         $sts = ( defined $val2 && $val1 eq $val2 ) ? 1 : 0;
         unless ( dbug_ok ($sts, "Tag '$t' in both objects have the same value ($val1) ($val2)" ) ) {
            $stop = 1;
         }
      }
      last  if ( $stop );
   }

   DBUG_RETURN ( $stop ? 0 : 1 );
}

# --------------------------------------------------------------------
# Now onto validating we can read the config file we created ...
# --------------------------------------------------------------------
sub test_array
{
   my $cfg    = shift;
   my $lbl    = shift;
   my $lang   = shift;
   my $tag    = shift;
   my $months = shift;   # Ref to MoY or MoYs ...

   my ($bad, $good) = (0, 0);
   if ( $tag =~ m/^(.?)MoY.*_(\d+)$/ ) {
      my ($special, $idx) = ($1, $2);
      my ($val1, $val2);

      $val1 = $cfg->get_value ($tag);
      if ( $special eq "l" ) {
         $val2 = lc ($months->[$idx]);
      } elsif ( $special eq "u" ) {
         $val2 = uc ($months->[$idx]);
      } else {
         $val2 = $months->[$idx];
      }

      if ( $val1 ne $val2 ) {
         my ($u1, $u2) = (utf8::is_utf8($val1)||0, utf8::is_utf8($val2)||0);
         dbug_ok (0, "Loaded ${lbl} [${idx}] ok! ($val1) vs ($val2) - utf8($u1 vs $u2)");
         ++$bad;
      }
      ++$good;
   } else {
      ++$bad
   }

   return ( $bad );
}

# --------------------------------------------------------------------
# Validates that reading/writing to the config file doesn't introduce issues.
sub validate_MoY_MoYs
{
   DBUG_ENTER_FUNC (@_);
   my $cfg = shift;

   my $fatal = 0;

   DBUG_PAUSE ()  unless ( $run_as_developer );

   foreach ( $cfg->find_sections () ) {
      my $sCfg = $cfg->get_section ( $_, 1 );
      my $lang = $sCfg->get_value ("Language", {required => 0});
      unless ( defined $lang ) {
         dbug_ok (1, "Skipping section '${_}' due to no Language tag!");
         next;
      }

      my %data = ( Language => $lang, Module => "Date::Language::${lang}" );
      my ($MoY_ref, $MoYs_ref) = Advanced::Config::Date::_swap_lang_common ( \%data, 0, 1 );

      foreach my $tag ( $sCfg->find_tags (qr /^MoY_/, 0) ) {
         $fatal += test_array ($sCfg, "MoY", $lang, $tag, $MoY_ref );
      }

      foreach my $tag ( $sCfg->find_tags (qr /^MoYs_/, 0) ) {
         $fatal += test_array ($sCfg, "MoYs", $lang, $tag, $MoYs_ref );
      }

      foreach my $tag ( $sCfg->find_tags (qr /^uMoY_/, 0) ) {
         $fatal += test_array ($sCfg, "MoY-u", $lang, $tag, $MoY_ref );
      }

      foreach my $tag ( $sCfg->find_tags (qr /^uMoYs_/, 0) ) {
         $fatal += test_array ($sCfg, "MoYs-u", $lang, $tag, $MoYs_ref );
      }

      foreach my $tag ( $sCfg->find_tags (qr /^lMoY_/, 0) ) {
         $fatal += test_array ($sCfg, "MoY-l", $lang, $tag, $MoY_ref );
      }

      foreach my $tag ( $sCfg->find_tags (qr /^lMoYs_/, 0) ) {
         $fatal += test_array ($sCfg, "MoYs-l", $lang, $tag, $MoYs_ref );
      }
   }

   if ($fatal) {
      done_testing ();
      DBUG_LEAVE (11);
   }

   DBUG_VOID_RETURN ();
}

# --------------------------------------------------------------------
sub validate_dates
{
   DBUG_ENTER_FUNC (@_);
   my $cfg           = shift;
   my $utf8_expected = shift;

   DBUG_PAUSE ()  unless ( $run_as_developer );

   foreach my $s ( $cfg->find_sections () ) {
      my $sCfg = $cfg->get_section ( $s, 1 );
      my $lang = $sCfg->get_value ("Language", {required => 0});
      unless ( defined $lang ) {
         dbug_ok (1, "Skipping section '$s' due to no Language tag!");
         next;
      }

      my $tmp = $lang;
      # $tmp = Advanced::Config::Date::swap_language ($lang);
      if ( $tmp ne $lang ) {
         dbug_ok (0, "Language was changed to ${lang}");
         next;
      }
      dbug_ok (1, "Validating dates for language ${lang} ...");

      # Validate the weekdays ...
      my $wd = $sCfg->get_list_values ("WeekDays", qr/\s*,\s*/,  undef, {required => 1});
      my $cnt = @{$wd};
      $cnt = 7  if ( $cnt == 8 && $wd->[0] eq $wd->[-1] );
      dbug_is ( $cnt, 7, "Found 7 weekdays defined by tag 'WeekDays' ($cnt)" );

      foreach my $tag ( @{$wd} ) {
         my $val = $sCfg->get_value ($tag) || "";
         DBUG_PRINT ("UTF8", "utf8 flag (%d)", utf8::is_utf8($val));

         my $ok = ($val =~ m/^Found /) ? 1 : 0;
         dbug_ok ($ok, "Found Weekday Tag ($tag): ${val}");
      }

      # Validate the date itself ...
      foreach my $tag ( $sCfg->find_tags (qr /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/, 0) ) {
         my $val1 = $sCfg->get_value ($tag);
         DBUG_PRINT ("UTF8", "utf8 flag (%d)", utf8::is_utf8($val1));
         my $val2 = $sCfg->get_date ($tag, $lang, date_language_warn => 0);
         if ( $val2 && $val2 eq $tag ) {
            dbug_ok (1, "Found tag: ${tag} in section '${s}' for ${lang} --> ${val2} -- ${val1}");
         } else {
            dbug_ok (0, "Tag ${tag} in section '${s}' for ${lang} points to a valid date: ${val1}");
         }
      }
      DBUG_PRINT ("----", "-------------------------------------------------");
   }

   DBUG_VOID_RETURN ();
}

# ====================================================================
# From here on down deals with creating the config file.
# ====================================================================
sub build_config_file
{
   DBUG_ENTER_FUNC (@_);
   my $all_languages = shift;
   my $wide_flag     = shift;

   my $file = $wide_flag ? $config_file_wide : $config_file_normal;

   unless ( open (CONFIG, ">", $file) ) {
      dbug_ok (0, "Creating the UTF-8 config file: $file");
      return DBUG_RETURN (0);
   }

   my $msg;
   if ( $wide_flag ) {
      dbug_ok (1, "Creating the UTF-8 config file: $file");
      binmode (CONFIG, "encoding(UTF-8)");   # Converts to wide-char / Unicode output.
      $msg = "This file was created using UTF-8 Encoding";
   } else {
      dbug_ok (1, "Creating the normal config file: $file");
      $msg = "This file was created without any special Encoding";
   }

   print CONFIG "\n";
   print CONFIG "# This is an auto-genearted config file.\n";
   print CONFIG "# Do not modify it by hand!\n\n";
   print CONFIG "# ${msg}\n\n";

   my $englishDoW = $all_languages->{English}->{DoW};

   foreach my $lang ( sort keys %{$all_languages} ) {
      my $l = $all_languages->{$lang};

      next  if ( $l->{wide} && ! $wide_flag );

      print CONFIG "[ $lang ]\n";
      print CONFIG "    Language = $lang\n";     # To preserve the case...
      print CONFIG "    Wide = $l->{wide}\n";
      print CONFIG "    WeekDays = ", join (",  ", @{$l->{DoW}}), "\n\n";

      foreach ( 0..6 ) {
         print CONFIG "    $l->{DoW}->[$_] = Found $englishDoW->[$_] in ${lang}  ($l->{DoW}->[$_])\n";
      }
      print CONFIG "\n";

      # ---------------------------
      # The 4 digit years ....
      # ---------------------------
      foreach ( 1..12 ) {
         print CONFIG build_date (0, 2018,  $_,  $_ + 1, $l, $lang);
      }
      print CONFIG "\n";

      foreach ( 1..12 ) {
         print CONFIG build_date (1, 2019,  $_,  $_ + 14, $l, $lang);
      }
      print CONFIG "\n";

      # ----------------------------------------------------------
      # The 2 digit years .... Use 199x years to avoid ambiguity
      # ----------------------------------------------------------
      foreach ( 1..12 ) {
         print CONFIG build_date (2, 1998,  $_,  $_ + 2, $l, $lang);
      }
      print CONFIG "\n";

      foreach ( 1..12 ) {
         print CONFIG build_date (3, 1999,  $_,  $_ + 15, $l, $lang);
      }
      print CONFIG "\n";
   }

   print CONFIG "----------------------------------------------------------\n\n";

   # These extra sections are for advanced checks ...
   # Using variables and Encryption ...
   my $cntr = 0;
   foreach my $lang ( "Chinese", "Greek", "Russian" ) {
      my $l = $all_languages->{$lang};

      next  unless ( defined $l );
      next  if ( $l->{wide} && ! $wide_flag );

      ++$cntr;
      print CONFIG "[ ZZ Extra ${cntr} ]\n";
      print CONFIG "    Language = \${$lang.Language}\n";
      print CONFIG "    Wide = \${$lang.Wide}\n";
      print CONFIG "    WeekDays = \${$lang.WeekDays}\n\n";

      foreach ( 0..6 ) {
         my $wd = $l->{DoW}->[$_];
         print CONFIG "    $wd = \${$lang.$wd}      # ENCRYPT\n";
      }
      print CONFIG "\n";

      foreach ( 1..12 ) {
         my $dt = sprintf ("%04d-%02d-%02d", 2018, $_, $_ + 1);
         print CONFIG "    ${dt} = \${$lang.$dt}      # ENCRYPT\n";
      }
      print CONFIG "\n";
      foreach ( 1..12 ) {
         my $dt = sprintf ("%04d-%02d-%02d", 2019, $_, $_ + 14);
         print CONFIG "    ${dt} = \${$lang.$dt}      # ENCRYPT\n";
      }
      print CONFIG "\n";
      foreach ( 1..12 ) {
         my $dt = sprintf ("%04d-%02d-%02d", 1998, $_, $_ + 2);
         print CONFIG "    ${dt} = \${$lang.$dt}      # ENCRYPT\n";
      }
      print CONFIG "\n";
      foreach ( 1..12 ) {
         my $dt = sprintf ("%04d-%02d-%02d", 1999, $_, $_ + 15);
         print CONFIG "    ${dt} = \${$lang.$dt}      # ENCRYPT\n";
      }
      print CONFIG "\n";
   }

   print CONFIG "----------------------------------------------------------\n\n";

   # So can validate we have no issues writing foreign languages to a
   # file and reading them back out again!
   foreach my $lang ( sort keys %{$all_languages} ) {
      my $l = $all_languages->{$lang};

      next  if ( $l->{wide} && ! $wide_flag );

      print CONFIG "[ $lang ]\n";
      foreach ( 0..11 ) {
         print CONFIG "MoY_${_} = $l->{MoY}->[$_]    # ENCRYPT\n";
      }
      print CONFIG "\n";
      foreach ( 0..11 ) {
         print CONFIG "MoYs_${_} = $l->{MoYs}->[$_]    # ENCRYPT\n";
      }
      print CONFIG "\n";



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