Commits

Shlomi Fish committed a21ab2a

Add a meaningful test.

  • Participants
  • Parent commits 2fe0c0f

Comments (0)

Files changed (4)

File HTML-Widgets-NavMenu-ToJSON/MANIFEST

 t/manifest.t
 t/pod-coverage.t
 t/pod.t
+t/output.t
 Makefile.PL
 META.yml
 META.json

File HTML-Widgets-NavMenu-ToJSON/lib/HTML/Widgets/NavMenu/ToJSON/Data_Persistence.pm

     return $self->_data->{id_persistence};
 }
 
+sub _default_data
+{
+    my $self = shift;
+
+    return { id_persistence => { paths_ids => { }, next_id => 1, }, };
+}
 
 sub get_id_for_url
 {

File HTML-Widgets-NavMenu-ToJSON/lib/HTML/Widgets/NavMenu/ToJSON/Data_Persistence/YAML.pm

 {
     my $self = shift;
 
-    my ($data) = YAML::XS::LoadFile($self->_filename());
+    my $data;
+
+    if (!eval
+    {
+        ($data) = YAML::XS::LoadFile($self->_filename());
+
+        1;
+    })
+    {
+        $data = $self->_default_data();
+    }
 
     $self->_data(
         $data

File HTML-Widgets-NavMenu-ToJSON/t/output.t

+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 9;
+
+use HTML::Widgets::NavMenu::ToJSON;
+use HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML;
+
+use File::Temp qw(tempdir);
+use File::Spec;
+
+use JSON ( qw(decode_json) );
+
+{
+    my $tempdir = tempdir( CLEANUP => 1);
+
+    my $yaml_fn = File::Spec->catfile($tempdir, "persistence.yaml");
+    my $tree_contents =
+    {
+        'host' => "default",
+        'text' => "Top 1",
+        'title' => "T1 Title",
+        'subs' =>
+        [
+            {
+                'text' => "Home",
+                'url' => "",
+            },
+            {
+                'text' => "About Me",
+                'title' => "About Myself",
+                'url' => "me/",
+                'expand' => { 're' => "^me/", },
+                'subs' =>
+                [
+                    {
+                        'text' => "Group Hug",
+                        'url' => "me/group-hug/",
+                    },
+                    {
+                        'text' => "Cool I/O",
+                        'url' => "me/cool-io/",
+                    },
+                    {
+                        'text' => "Resume",
+                        'url' => "resume.html",
+                    },
+                ],
+            },
+            {
+                'text' => "Halifax",
+                'url' => "halifax/",
+                'skip' => 1,
+            },
+            {
+                'text' => "Software",
+                'title' => "Open Source Software I Wrote",
+                'url' => "open-source/",
+                'expand' => { 're' => "^open-source/", },
+                'subs' =>
+                [
+                    {
+                        'text' => "Fooware",
+                        'url' => "open-source/fooware/",
+                        'skip' => 1,
+                    },
+                    {
+                        'text' => "Condor-Man",
+                        'title' => "Kwalitee",
+                        'url' => "open-source/condor-man/",
+                    },
+                ],
+            },
+        ],
+    };
+
+    my $persistence =
+    HTML::Widgets::NavMenu::ToJSON::Data_Persistence::YAML->new(
+        {
+            filename => $yaml_fn,
+        }
+    );
+
+    # TEST
+    ok ($persistence, "Persistence obj was initialized.");
+
+    my $obj = HTML::Widgets::NavMenu::ToJSON->new(
+        {
+            data_persistence_store => $persistence,
+            # The one given as input to HTML::Widgets::NavMenu
+            tree_contents => $tree_contents,
+        }
+    );
+
+    # TEST
+    ok ($persistence, "JSON converter was initialized.");
+
+    my $json = $obj->output_as_json( { } );
+    my $as_perl = decode_json ($json);
+
+    # TEST
+    is ($as_perl->[0]->{id}, 1, "Root id is 1.");
+
+    # TEST
+    is ($as_perl->[0]->{text}, 'Home', "Root text is Home.");
+
+    # TEST
+    is ($as_perl->[0]->{url}, '', "Root url is empty.");
+
+    # TEST
+    is ($as_perl->[1]->{text}, 'About Me', '1 text');
+
+    # TEST
+    is ($as_perl->[1]->{title}, 'About Myself', '1 title');
+
+    # TEST
+    is ($as_perl->[1]->{subs}->[0]->{text}, 'Group Hug', 'subs 0 text');
+
+    # TEST
+    is ($as_perl->[1]->{subs}->[2]->{url}, 'resume.html', 'subs 1 url');
+}