Acme-Machi

 view release on metacpan or  search on metacpan

t/Acme-Machi.t  view on Meta::CPAN

# -----------------------------------------------------------------------------
METHOD_AFFECTIONATE: {;
  my $loli = Acme::Machi->new();

  # Teach her some good words
  #############################################################
  is($loli->learning( ("I'll read a book for you.",
                       "These are field horsetails!",
                       "Have some barley tea")),
                      1+3,
                      'Test return value of learning funcs'
  );
  #############################################################
  
  open my $fh, '>', \ my $my_string;
  # Apply affectionate() method, check outputs are randomly generated
  #############################################################
  my $applying_4_times = join "",
    map {;
      open  $fh, '>', \ $my_string;
      $loli->affectionate($fh);
      $my_string;
  } (1 .. 40);
  isnt($applying_4_times, $my_string x 40, 
    'check randomness on results of affectionate() method'
  );
  #############################################################

  # Chnaging speaker test
  #############################################################
  {
    $loli->named('Megu');
    open  $fh, '>', \ $my_string;
    $loli->affectionate($fh);
    $my_string =~ qr/\A(.+)?:/u;
    is($1,$loli->name(),'Speaker changing test');
  }
  #############################################################
 
}
# -----------------------------------------------------------------------------

diag(" Test functionality of search_file_from() method");
# -----------------------------------------------------------------------------
METHOD_SEARCH_FILE_FROM: {;
  # Search using DFS & BFS should produce the same result.
  #############################################################
  my $loli = Acme::Machi->new();
  $loli->have_the_habit_of('DFS');
  my $result_DFS = $loli->search_file_from((<*>)[0],'.',1);
  $loli->have_the_habit_of('BFS');
  my $result_BFS = $loli->search_file_from((<*>)[0],'.',1);
  ok($result_DFS == $result_BFS, "Stable test");
  #############################################################


  # Produced Tree-like structures should be the same whichever 
  # method you chose to use.
  #############################################################
  open my $fh, '>', \ my $my_string;
  select $fh;
  my $data;

  $loli->have_the_habit_of('DFS');
  $loli->search_file_from((<*>)[0],'.',0);
  $result_DFS = $my_string;
  eval "$result_DFS";
  $result_DFS = $data;

  open $fh, '>', \ $my_string;
  select $fh;
  $loli->have_the_habit_of('BFS');
  $loli->search_file_from((<*>)[0],'.',0);
  $result_BFS = $my_string;
  eval "$result_BFS";
  $result_BFS = $data;
  select STDOUT;

  sub cmp  {
    my $hash_ref1 = $_[0];
    my $hash_ref2 = $_[1];
    foreach my $k (sort keys %$hash_ref1){
      if (ref $$hash_ref1{$k} eq ref {}){ 
        &cmp($$hash_ref1{$k}, $$hash_ref2{$k});
      } elsif (ref $$hash_ref1{$k} eq ref []){ #array
        if($#{$$hash_ref2{$k}} >0){
          (${$$hash_ref2{$k}}[$_] ne ${$$hash_ref2{$k}}[$_]) 
            && (return) for (0 .. $#{$$hash_ref2{$k}});
        }
      } elsif (ref $$hash_ref1{$k} eq ref \undef){ #scalar
        (${$$hash_ref2{$k}} ne ${$$hash_ref2{$k}}) && (return);
      } else {
        if(defined($$hash_ref2{$k})){
          ($$hash_ref2{$k} ne $$hash_ref2{$k}) && (return);
        }
      }
    }
    1;
  }

#-------- sub reference ver. ------------------------------------
#  my $cmp = sub {
#    my $hash_ref1 = $_[0];
#    my $hash_ref2 = $_[1];
#    foreach my $k (sort keys %$hash_ref1){
#      if (ref $$hash_ref1{$k} eq ref {}){ 
#        __SUB__->($$hash_ref1{$k}, $$hash_ref2{$k});
#      } elsif (ref $$hash_ref1{$k} eq ref []){ #array
#        if($#{$$hash_ref2{$k}} >0){
#          (${$$hash_ref2{$k}}[$_] ne ${$$hash_ref2{$k}}[$_]) 
#            && (return) for (0 .. $#{$$hash_ref2{$k}});
#        }
#      } elsif (ref $$hash_ref1{$k} eq ref \undef){ #scalar
#        (${$$hash_ref2{$k}} ne ${$$hash_ref2{$k}}) && (return);
#      } else {
#        if(defined($$hash_ref2{$k})){
#          ($$hash_ref2{$k} ne $$hash_ref2{$k}) && (return);
#        }
#      }
#    }
#    1;
#  };
#-------- sub reference ver. ------------------------------------

  &cmp($$result_DFS,$$result_BFS);
  ok(&cmp,'Compare two data structures!');
  
  #############################################################
}
# -----------------------------------------------------------------------------
done_testing();



( run in 2.039 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )