Commits

Anonymous committed 7ab7820

Converted the tests to use is_deeply instead of compare_arrays().

  • Participants
  • Parent commits 0e05934

Comments (0)

Files changed (4)

module/t/02site-map.t

 {
     my $results = shift;
     my $expected_string = shift;
+    my $test_blurb = shift;
     
     my @result = @$results;
 
     my @expected = (split(/\n/, $expected_string));
 
-    return (compare_string_arrays(\@expected, \@result) == 0);
+    is_deeply(\@expected, \@result, $test_blurb);
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_site_map($results, $expected_text), 
-        "site_map #1");
+    validate_site_map($results, $expected_text, "site_map #1");
 }
 
 {
 </ul>
 EOF
     # TEST
-    ok (validate_site_map($results, $expected_text), 
-        "site_map #2");
+    validate_site_map($results, $expected_text, "site_map #2");
 }
 
 {
 </ul>
 EOF
     # TEST
-    ok (validate_site_map($results, $expected_text),
-        "site_map - complex");
+    validate_site_map($results, $expected_text, "site_map - complex");
 }
 
 # Now testing that the separator is safely skipped and does not generate
 </ul>
 EOF
     # TEST
-    ok (validate_site_map($results, $expected_text), 
-        "site_map - separator");
+    validate_site_map($results, $expected_text, "site_map - separator");
 }
 
 # This is a test for the rec_url_type directive.
 </ul>
 EOF
     # TEST
-    ok (validate_site_map($results, $expected_text), 
-        "site_map - rec_url_type");
+    validate_site_map($results, $expected_text, "site_map - rec_url_type");
 }
 
 
 </ul>
 EOF
     # TEST
-    ok (validate_site_map($results, $expected_text), 
-        "site_map - url_is_abs");
+    validate_site_map($results, $expected_text, "site_map - url_is_abs");
 }

module/t/04nav-menu.t

 
 my $test_data = get_test_data();
 
-sub validate_nav_menu
+sub test_nav_menu
 {
     my $rendered = shift;
     my $expected_string = shift;
+    my $test_blurb = shift;
     
     my @result = (@{$rendered->{html}});
 
     my @expected = (split(/\n/, $expected_string));
 
-    return (compare_string_arrays(\@expected, \@result) == 0);
+    is_deeply (\@expected, \@result, $test_blurb);
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu for minimal - 1"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu for minimal - 1"); 
 }
 
 
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu for minimal - 2"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu for minimal - 2"); 
 }
 
 # This test tests that an expand_re directive should not cause
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu for expand_re"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu for expand_re"); 
 }
 
 # This test tests that an empty expand_re directive works after a successful
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string),
-        "Nav Menu for empty expand_re after successful pattern match");
+    test_nav_menu($rendered, $expected_string, "Nav Menu for empty expand_re after successful pattern match");
 }
 
 # This test tests the show_always directive which causes the entire
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string),
-        "Nav Menu with show_always");
+    test_nav_menu($rendered, $expected_string, "Nav Menu with show_always");
 }
 
 # This test tests a menu auto-expands if the current URL is an item
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string),
-        "Nav Menu with a selected sub-item");
+    test_nav_menu($rendered, $expected_string, "Nav Menu with a selected sub-item");
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu with Separators"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu with Separators"); 
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu with Hidden Item"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu with Hidden Item"); 
 }
 
 
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu with a role of \"header\""); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu with a role of \"header\""); 
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu with a selected item with a role of \"header\" "); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu with a selected item with a role of \"header\" "); 
 }
 
 # Test the selective expand. (test #1)
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Selective Expand Nav-Menu #1"); 
+    test_nav_menu($rendered, $expected_string, "Selective Expand Nav-Menu #1"); 
 }
 
 # Test the selective expand. (test #2)
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Selective Expand Nav-Menu #2"); 
+    test_nav_menu($rendered, $expected_string, "Selective Expand Nav-Menu #2"); 
 }
 
 # This is a test for the url_type directive.
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu for url_type - 1"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu for url_type - 1"); 
 }
 
 # This is a test for the rec_url_type directive.
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu for rec_url_type - 1"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu for rec_url_type - 1"); 
 }
 
 # Test the url_is_abs directive
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu for url_is_asb - 1"); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu for url_is_asb - 1"); 
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Mixed Expand Nav-Menu #1"); 
+    test_nav_menu($rendered, $expected_string, "Mixed Expand Nav-Menu #1"); 
 }
 
 
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Mixed Expand Nav-Menu #2"); 
+    test_nav_menu($rendered, $expected_string, "Mixed Expand Nav-Menu #2"); 
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Mixed Expand Nav-Menu #3"); 
+    test_nav_menu($rendered, $expected_string, "Mixed Expand Nav-Menu #3"); 
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Special Chars Nav Menu"); 
+    test_nav_menu($rendered, $expected_string, "Special Chars Nav Menu"); 
 }
 
 # Test a special chars-based URL.
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Nav Menu with a special chars URL."); 
+    test_nav_menu($rendered, $expected_string, "Nav Menu with a special chars URL."); 
 }

module/t/07tree-iter.t

+#!/usr/bin/perl -w
+
+package MyIter;
+
+use strict;
+
+use base 'HTML::Widgets::NavMenu::Tree::Iterator';
+
+sub initialize
+{
+    my $self = shift;
+
+    $self->SUPER::initialize(@_);
+
+    my %args = (@_);
+
+    $self->{'data'} = $args{'data'};
+
+    $self->{'results'} = [];
+
+    return 0;
+}
+
+sub append
+{
+    my $self = shift;
+    push @{$self->{'results'}}, @_;
+    return 0;
+}
+
+sub get_initial_node
+{
+    my $self = shift;
+    return $self->{'data'};
+}
+
+sub get_node_subs
+{
+    my $self = shift;
+    my %args = (@_);
+    my $node = $args{'node'};
+    return
+        exists($node->{'childs'}) ?
+            [ @{$node->{'childs'}} ] :
+            [];
+}
+
+sub get_new_accum_state
+{
+    my $self = shift;
+    my %args = (@_);
+    my $parent_item = $args{'item'};
+    my $node = $args{'node'};
+
+    if (!defined($parent_item))
+    {
+        return $node->{'accum'};
+    }
+
+    my $prev_state = 
+        $parent_item->accum_state();
+
+    return ($node->{'accum'} || $prev_state);
+}
+
+sub node_start
+{
+    my $self = shift;
+    my $top_item = $self->top;
+    my $node = $self->top->node();
+
+    $self->append(join("-", "Start", $node->{'id'}, $top_item->accum_state));
+}
+
+sub node_end
+{
+    my $self = shift;
+    my $node = $self->top->node();
+
+    $self->append(join("-", "End", $node->{'id'}));
+}
+
+sub node_should_recurse
+{
+    my $self = shift;
+    my $node = $self->top->node();
+    return $node->{'recurse'};
+}
+
+1;
+
+package MyIterComplexSubs;
+
+use vars qw(@ISA);
+
+@ISA=qw(MyIter);
+
+sub get_node_from_sub
+{
+    my $self = shift;
+
+    my %args = (@_);
+
+    my $item = $args{'item'};
+    my $sub = $args{'sub'};
+    my $node = $item->node();
+
+    return $node->{'subs_db'}->{$sub};
+}
+
+1;
+
+package main;
+
+use Test::More tests => 4;
+
+use strict;
+
+use HTML::Widgets::NavMenu::Test::Util;
+
+sub test_traverse
+{
+    my ($data, $expected, $test_name, $class) = (@_);
+    $class ||= "MyIter";
+    my $traverser =
+        $class->new(
+            'data' => $data
+        );
+
+    $traverser->traverse();
+
+    is_deeply($traverser->{'results'}, $expected, $test_name);
+}
+
+{
+    my $data =
+        {
+            'id' => "A",
+            'recurse' => 1,
+            'accum' => "one",
+            'childs' =>
+            [
+                {
+                    'id' => "B",
+                    'accum' => "two",
+                },
+                {
+                    'id' => "C",
+                    'recurse' => 1,
+                    'childs' =>
+                    [
+                        {
+                            'id' => "FG",
+                        },
+                    ],
+                },
+            ],
+        };
+    my @expected = ("Start-A-one", "Start-B-two", "End-B",
+        "Start-C-one", "Start-FG-one", "End-FG", "End-C", "End-A");
+
+    # TEST 
+    test_traverse($data, \@expected, "Simple example for testing the Tree traverser.");
+}
+
+# This test checks that the should_recurse predicate is honoured.
+{
+    my $data =
+        {
+            'id' => "A",
+            'recurse' => 1,
+            'accum' => "one",
+            'childs' =>
+            [
+                {
+                    'id' => "B",
+                    'accum' => "two",
+                },
+                {
+                    'id' => "C",
+                    'recurse' => 0,
+                    'childs' =>
+                    [
+                        {
+                            'id' => "FG",
+                        },
+                    ],
+                },
+            ],
+        };
+    my @expected = ("Start-A-one", "Start-B-two", "End-B",
+        "Start-C-one", "End-C", "End-A");
+
+    # TEST 
+    test_traverse($data, \@expected, "Example with recurse = 0");
+}
+
+{
+    my $data =
+        {
+            'id' => "A",
+            'recurse' => 1,
+            'accum' => "one",
+            'childs' =>
+            [
+                {
+                    'id' => "B",
+                    'accum' => "two",
+                },
+                {
+                    'id' => "C",
+                    'recurse' => 0,
+                    'childs' =>
+                    [
+                        {
+                            'id' => "FG",
+                        },
+                        {
+                            'id' => "E",
+                            'recurse' => 0,
+                            'childs' =>
+                            [
+                                {
+                                    'id' => "Y",
+                                },
+                                {
+                                    'id' => "Z",
+                                },
+                            ],
+                        },
+                    ],
+                },
+                {
+                    'id' => "AGH",
+                    'recurse' => 1,
+                    'accum' => "three",
+                    'childs' =>
+                    [
+                        {
+                            'id' => "MON",
+                            'recurse' => 0,
+                            'accum' => "four",
+                            'childs' =>
+                            [
+                                {
+                                    'id' => "HELLO",
+                                    'recurse' => 1,
+                                },
+                            ],
+                        },
+                        {
+                            'id' => "KOJ",
+                            'recurse' => 1,
+                        },
+                    ],
+                }
+            ],
+        };
+    my @expected = ("Start-A-one", "Start-B-two", "End-B",
+        "Start-C-one", "End-C", "Start-AGH-three", 
+        "Start-MON-four", "End-MON", "Start-KOJ-three", "End-KOJ",
+        "End-AGH", "End-A");
+
+    # TEST 
+    test_traverse($data, \@expected, "Example with lots of weird combinations");
+}
+
+{
+    my $data =
+        {
+            'id' => "A",
+            'recurse' => 1,
+            'accum' => "one",
+            'childs' => [qw(hello good)],
+            'subs_db' =>
+            {
+                'hello' =>
+                {
+                    'id' => "BOK",
+                    'accum' => "two",
+                },
+                'good' =>
+                {
+                    'id' => "C",
+                },
+            },
+        };
+
+    my @expected = ("Start-A-one", "Start-BOK-two", "End-BOK",
+        "Start-C-one", "End-C", "End-A");
+
+    # TEST
+    test_traverse($data, \@expected, "Example with complex sub resolution",
+        "MyIterComplexSubs");
+}

module/t/10ul-classes.t

 
 my $test_data = get_test_data();
 
-sub validate_nav_menu
+sub test_nav_menu
 {
     my $rendered = shift;
     my $expected_string = shift;
+    my $test_blurb = shift;
     
     my @result = (@{$rendered->{html}});
 
     my @expected = (split(/\n/, $expected_string));
 
-    return (compare_string_arrays(\@expected, \@result) == 0);
+    is_deeply (\@expected, \@result, $test_blurb);
 }
 
 {
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string), 
-        "Testing ul classes for no CSS class to be assigned."); 
+    test_nav_menu($rendered, $expected_string, "Testing ul classes for no CSS class to be assigned."); 
 }
 
 # This test tests the show_always directive which causes the entire
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string),
-        "Nav Menu with depth classes");
+    test_nav_menu ($rendered, $expected_string, "Nav Menu with depth classes");
 }
 
 # This test tests the escaping of the class names.
 EOF
 
     # TEST
-    ok (validate_nav_menu($rendered, $expected_string),
-        "Nav Menu with depth classes");
+    test_nav_menu ($rendered, $expected_string, "Nav Menu with depth classes");
 }