Tag Archives: Test::modern

Using Moo (continued)

Continuing from my last post  Using Moo,  I will add a little more functionality to my File::Info class.  At first I was going to subclass it, but then I decided to use  Moo::Role instead. Moo::Role which is based on Role::Tiny. Roles are a really convenient way of adding extra functionality to your class.

package File::Info;
use Scalar::Util qw/blessed looks_like_number/;
use Moo;
use v5.16;
#-------------------------------------------------------------------------------
#  Base class to provide some useful information about a given file.
#-------------------------------------------------------------------------------
use Path::Tiny;
use Carp;
use File::stat;
use namespace::clean;    # clean imported functions from your namespace.
#-------------------------------------------------------------------------------
#  Attributes
#-------------------------------------------------------------------------------
has file => (
    is  => 'ro',
    isa => sub {
        #--- Because I like Path::Tiny
        Carp::croak(
            qq{'in_file' needs to be an existing file and a Path::Tiny Object!})
          unless ( ref $_[0] eq 'Path::Tiny' and $_[0]->exists );
    },
    coerce => sub {
        return $_[0]
          if ( ref $_[0] eq 'Path::Tiny' );
        return path( $_[0] );
    },
    required => 1
);

has file_stat => (
    is  => 'rwp',
    isa => sub {
        Carp::croak(qq{$_[0] is not a File::stat::stat!})
          unless ( $_[0] and ( ref $_[0] ) );
    },
);

has size_bytes => (
    is  => 'rwp',
    isa => sub {
        Carp::croak(qq{'bytes' value $_[0] is not numeric!})
          unless ( looks_like_number( $_[0] ) );
    },
    lazy    => 1,
    builder => sub {
        $_[0]->file_stat->size;
    },
);

has mod_time_seconds => (
    is  => 'ro',
    isa => sub {
        Carp::croak(qq{$_[0] is not a number!})
          unless ( looks_like_number( $_[0] ) );
    },
    lazy    => 1,
    builder => sub {
        $_[0]->file_stat->mtime;
    },
);

#--- Moo roles to print bytes/seconds in a more readable form
with qw{MakeBytesPretty MakeSecondsPretty};

#-------------------------------------------------------------------------------
#  Builders and Triggers
#-------------------------------------------------------------------------------
sub BUILD {
    my ($self) = @_;
    $self->_set_file_stat( File::stat::stat( $self->file ) )
      or Carp::croak( qq{File stat failed to get the file stat for, }
          . $self->file
          . qq{!: $!} );
}
1;

And the Roles…

package MakeBytesPretty;
use Moo::Role; #--- Makes this a role, and not a class. 
use v5.16;
use namespace::clean;

requires 'size_bytes'; #-- Make sure that this attribute exists somewhare. 
#-------------------------------------------------------------------------------
#  Moo role to prints bytes a little more pretty.
#-------------------------------------------------------------------------------
sub make_file_size_pretty {
    my ($self) = @_;
    my $bytes = $self->size_bytes;

    return ( not $bytes )
      ? 0
      : ( $bytes <= 1024 ) ? sprintf( "%0.02f", $bytes )
      . " Bytes"

      : ( $bytes <= 1048576 ) ? sprintf( "%0.02f", ( $bytes / 1024 ) ) . " KB"

      : ( $bytes <= 1073741824 )
      ? sprintf( "%0.02f", ( $bytes / 1048576 ) ) . " MB"

      : ( $bytes <= 1099511627776 )
      ? sprintf( "%0.02f", ( $bytes / 1073741824 ) ) . " GB"

      : ( $bytes <= 1125899906842624 )
      ? sprintf( "%0.02f", ( $bytes / 1099511627776 ) ) . " TB"

      : ( $bytes <= 1152921504606846976 )

      ? sprintf( "%0.02f", ( $bytes / 1125899906842624 ) ) . " PB"    # Petabyte

      : sprintf( "%0.02f", ( $bytes / 1152921504606846976 ) ) . " EB" # Exabyte
}
#-------------------------------------------------------------------------------
#  End
#-------------------------------------------------------------------------------
1;
package MakeSecondsPretty;
use Moo::Role;
use v5.16;
use namespace::clean;

requires 'mod_time_seconds';
#-------------------------------------------------------------------------------
#  Moo role to print time 'Seconds' in a more pretty way.
#-------------------------------------------------------------------------------
sub make_mod_time_pretty {
    my ( $self ) = @_;
    my $seconds = $self->mod_time_seconds;
    return ( not $seconds )
      ? 0
      : ( $seconds <= 60 ) ? sprintf( "%0.02f", $seconds )
      . " Seconds"

      : ( $seconds <= 3600 )
      ? sprintf( "%0.02f", ( $seconds / 60 ) ) . " Minutes"

      : ( $seconds <= 3600 )
      ? sprintf( "%0.02f", ( $seconds / 3600 ) ) . " Hours"

      : ( $seconds <= 86400 )
      ? sprintf( "%0.02f", ( $seconds / 86400 ) ) . " Days"

      : sprintf( "%0.02f", ( $seconds / 604800 ) ) . " Weeks";
}
#-------------------------------------------------------------------------------
#  End
#-------------------------------------------------------------------------------
1;

As before, it’s a good idea to write a test script to make sure all is ok. This test script isn’t overly thorough, but it’s good enough for this demonstration.

use Test::Modern;    #-- I wonder will it be modern in 10 years time??
use FindBin qw($Bin);
use lib qq{$Bin/../lib};    #-- Where File::Info is located.
use File::Info;
use Scalar::Util qw/blessed looks_like_number/;

my $fi = object_ok(
    sub { File::Info->new( file => qq{IMAG0029.jpg} ) },
    '$fi',
    isa => [qw(Moo::Object )],
    can => [
        qw(  file file_stat size_bytes mod_time_seconds make_file_size_pretty
          make_mod_time_pretty )
    ],
    clean => 1,
    more  => sub {
        my $file_info_obj = shift;
        isa_ok( $file_info_obj->file, 'Path::Tiny',
            q{File::Info 'file' is a Path::Tiny object.} );
        is( $file_info_obj->file->basename,
            qq{IMAG0029.jpg},
            q{File::Info 'file' has the correct file basename.} );
        isa_ok( $file_info_obj->file_stat, q{File::stat},
            qq{File::Info 'file_stat' is a 'File::stat'.} );
        like( $file_info_obj->file_stat->size,
            qr/^[0-9]+$/,
            q{File::Info 'file_stat->size' returns a numeric file size.} );
        like( $file_info_obj->size_bytes,
            qr/^[0-9]+$/, qq{File::Info 'size_bytes' is numeric.} );
        like(
            $file_info_obj->make_file_size_pretty,
            qr/^d+?.d+ (bytes|KB|MB|GB|TB|PB|EB)$/, qq{
            File::Info bytes 'make_file_size_pretty' 
            prints a file size followed by the size Unit.}
        );
        like( $file_info_obj->mod_time_seconds, qr/^d+$/,
            qq{File::Info 'mod_time_seconds' prints the file modification time.}
        );
        like(
            $file_info_obj->make_mod_time_pretty,
            qr/^d+?.d+ (Seconds|Minutes|Hours|Days|Weeks)$/, qq{
            File::Info bytes 'make_mod_timepretty' 
            prints the file mod time followed by the time Unit.}
        );
    },
);

done_testing();

Run the test script …

Moo > prove -v t/test_file.t 
t/test_file.t .. 
    # Subtest: $fi ok
    ok 1 - instantiate $fi
    ok 2 - $fi is blessed
    ok 3 - '$fi' isa 'Moo::Object'
    ok 4 - File::Info->can(...)
    ok 5 - File::Info contains no imported functions
        # Subtest: more tests for $fi
        ok 1 - 'File::Info 'file' is a Path::Tiny object.' isa 'Path::Tiny'
        ok 2 - File::Info 'file' has the correct file basename.
        ok 3 - 'File::Info 'file_stat' is a 'File::stat'.' isa 'File::stat'
        ok 4 - File::Info 'file_stat->size' returns a numeric file size.
        ok 5 - File::Info 'size_bytes' is numeric.
        ok 6 - 
        #             File::Info bytes 'make_file_size_pretty' 
        #             prints a file size followed by the size Unit.
        ok 7 - File::Info 'mod_time_seconds' prints the file modification time.
        ok 8 - 
        #             File::Info bytes 'make_mod_timepretty' 
        #             prints the file mod time followed by the time Unit.
        ok 9 - no exception thrown by additional tests
        1..9
    ok 6 - more tests for $fi
    1..6
ok 1 - $fi ok
ok 2 - no (unexpected) warnings (via done_testing)
1..2
ok
All tests successful.
Files=1, Tests=2,  1 wallclock secs ( 0.03 usr  0.00 sys +  0.08 cusr  0.02 csys =  0.13 CPU)
Result: PASS
[17:10 - 1.06]

Ok. Looks fine for now.

However, I would like to change the module around a little. I want to be able to print the size and age of a given file in a somewhat readable fashion.

I removed the ‘mod_time_seconds’ attribute and replaced it with ‘mod_time_moment’ . This attribute will store the file’s modification time as a Time::Moment object. This will add a little more versatility to the module. Time::Moment is a handy and fast time manipulation module that’s relatively new on the block. Other modules that I could have used would be, Time::Piece which I use a lot also, and  the venerable DateTime module, which is excellent for serious date calculations involving time zones, durations etc.

I also added two new methods to get the time since the last file modification. One will return the elapsed time in seconds and the other will return the elapsed time in a more readable form.

Here is the updated File::Info module…

package File::Info;
use Scalar::Util qw/blessed looks_like_number/;
use Moo;
use v5.16;

#-------------------------------------------------------------------------------
#  Base class to provide some useful information about a given file.
#-------------------------------------------------------------------------------
use Path::Tiny;
use Carp;
use File::stat;
use Time::Moment;
use Time::Piece;
use namespace::clean;    # clean imported functions from your namespace.

#-------------------------------------------------------------------------------
#  Attributes
#-------------------------------------------------------------------------------
has file => (
    is  => 'ro',
    isa => sub {

        #--- Because I like Path::Tiny
        Carp::croak(
            qq{'in_file' needs to be an existing file and a Path::Tiny Object!})
          unless ( ref $_[0] eq 'Path::Tiny' and $_[0]->exists );
    },
    coerce => sub {
        return $_[0]
          if ( ref $_[0] eq 'Path::Tiny' );
        return path( $_[1] );
    },
    required => 1
);

has file_stat => (
    is  => 'rwp',
    isa => sub {
        Carp::croak(qq{$_[0] is not a File::stat::stat!})
          unless ( $_[0] and ( ref $_[0] ) );
    },
);

has size_bytes => (
    is  => 'rwp',
    isa => sub {
        Carp::croak(qq{'bytes' value $_[0] is not numeric!})
          unless ( looks_like_number( $_[0] ) );
    },
    lazy    => 1,
    builder => sub {
        $_[0]->file_stat->size;
    },
);

#--- The file's last modification time
#    as a Time::Moment Object
has mod_time_moment => (
    is  => 'ro',
    isa => sub {
        Carp::croak(
            qq{Bad 'mod_time_moment' because $_[0] is not a Time::Moment!})
          unless ( ref $_[0] eq q{Time::Moment} );
    },
    lazy    => 1,
    builder => sub {
        #--- Time::Piece To keep it in the local time zone
   Time::Moment->from_object( scalar Time::Piece::localtime($_[0]->file_stat->mtime ) 
        );
    },
);

#--- Moo roles to print bytes/seconds in a more readable form
with qw{MakeBytesPretty MakeSecondsPretty};

#-------------------------------------------------------------------------------
#  Builders and Triggers
#-------------------------------------------------------------------------------
sub BUILD {
    my ($self) = @_;
    $self->_set_file_stat( File::stat::stat( $self->file ) )
      or Carp::croak( qq{File stat failed to get the file stat for, }
          . $self->file
          . qq{!: $!} );
}

#-------------------------------------------------------------------------------
#  Methods
#-------------------------------------------------------------------------------
#--- The file age since its last modification expressed in seconds.
sub seconds_since_mod {
    my ($self) = @_;
    return ( Time::Moment->now()->epoch - $self->mod_time_moment->epoch );
}

sub time_since_mod_pretty {
    my ($self) = @_;
    return $self->make_seconds_pretty( $self->seconds_since_mod );
}

#-------------------------------------------------------------------------------
1;

I also made some changes to the role MakeSecondsPretty.pm. I removed the ‘requires’  statement and renamed the only method to ‘make_seconds_pretty’ . I also changed this method slightly.

sub make_seconds_pretty {
    my ( $self , $seconds ) = @_;
    return ( not $seconds )
      ? 0
      : ( $seconds <= 60 ) ? sprintf( "%0.02f", $seconds )
      . " Seconds"

      : ( $seconds <= 3600 )
      ? sprintf( "%0.02f", ( $seconds / 60 ) ) . " Minutes"

      : ( $seconds <= 86400 )
      ? sprintf( "%0.02f", ( $seconds / 3600 ) ) . " Hours"

      : ( $seconds <= 604800 )
      ? sprintf( "%0.02f", ( $seconds / 86400 ) ) . " Days"

      : sprintf( "%0.02f", ( $seconds / 604800 ) ) . " Weeks";
}

The testing script also has to be changed to reflect changes in the module.

my $fi = object_ok(
    sub { File::Info->new( file => qq{IMAG0029.jpg} ) },
    '$fi',
    isa => [qw(Moo::Object )],
    can => [
        qw(  file file_stat size_bytes mod_time_moment make_seconds_pretty
          seconds_since_mod time_since_mod_pretty )
    ],
    clean => 1,
    more  => sub {
        my $file_info_obj = shift;
        isa_ok( $file_info_obj->file, 'Path::Tiny',
            q{File::Info 'file' is a Path::Tiny object.} );
        is( $file_info_obj->file->basename,
            qq{IMAG0029.jpg},
            q{File::Info 'file' has the correct file basename.} );
        isa_ok( $file_info_obj->file_stat, q{File::stat},
            qq{File::Info 'file_stat' is a 'File::stat'.} );
        like( $file_info_obj->file_stat->size,
            qr/^[0-9]+$/,
            q{File::Info 'file_stat->size' returns a numeric file size.} );
        like( $file_info_obj->size_bytes,
            qr/^[0-9]+$/, qq{File::Info 'size_bytes' is numeric.} );
        like(
            $file_info_obj->make_file_size_pretty,
            qr/^d+?.d+ (bytes|KB|MB|GB|TB|PB|EB)$/, qq{
            File::Info bytes 'make_file_size_pretty' 
            prints a file size followed by the size Unit.}
        );
        isa_ok( $file_info_obj->mod_time_moment, q{Time::Moment},
            qq{File::Info 'mod_time_moment' returns a Time::Moment object.}
        );
        like( $file_info_obj->seconds_since_mod, qr/^d+$/,
            qq{File::Info 'seconds_since_mod' returns a numeric.}
        );
        like(
           $file_info_obj->time_since_mod_pretty(),
            qr/^d+?.d+ (Seconds|Minutes|Hours|Days|Weeks)$/, qq{
            File::Info 'time_since_mod_pretty' uses 'make_seconds_pretty' 
            to print the 'seconds_since_mod' as time followed by the time Unit.}
        );
    },
);

Run the test again…

Moo > prove -v t/test_file.t 
t/test_file.t .. 
    # Subtest: $fi ok
    ok 1 - instantiate $fi
    ok 2 - $fi is blessed
    ok 3 - '$fi' isa 'Moo::Object'
    ok 4 - File::Info->can(...)
    ok 5 - File::Info contains no imported functions
        # Subtest: more tests for $fi
        ok 1 - 'File::Info 'file' is a Path::Tiny object.' isa 'Path::Tiny'
        ok 2 - File::Info 'file' has the correct file basename.
        ok 3 - 'File::Info 'file_stat' is a 'File::stat'.' isa 'File::stat'
        ok 4 - File::Info 'file_stat->size' returns a numeric file size.
        ok 5 - File::Info 'size_bytes' is numeric.
        ok 6 - 
        #             File::Info bytes 'make_file_size_pretty' 
        #             prints a file size followed by the size Unit.
        ok 7 - 'File::Info 'mod_time_moment' returns a Time::Moment object.' isa 'Time::Moment'
        ok 8 - File::Info 'seconds_since_mod' returns a numeric.
        ok 9 - 
        #             File::Info 'time_since_mod_pretty' uses 'make_seconds_pretty' 
        #             to print the 'seconds_since_mod' as time followed by the time Unit.
        ok 10 - no exception thrown by additional tests
        1..10
    ok 6 - more tests for $fi
    1..6
ok 1 - $fi ok
ok 2 - no (unexpected) warnings (via done_testing)
1..2
ok
All tests successful.
Files=1, Tests=2,  1 wallclock secs ( 0.03 usr  0.01 sys +  0.10 cusr  0.02 csys =  0.16 CPU)
Result: PASS
[22:28 - 0.34]

Ok!

For my next post. I will use my module in a script with the help of  MooX::Options.

 

Using Moo

Moo simplifies OO in Perl.
Here is a Moo class that will provide some information about a file. I will use D.A Golden’s Path::Tiny, because I use this module in almost every script that I write.

package File::Info;
use Moo;
use v5.16;
#-------------------------------------------------------------------------------
#  Base class to provide information about a given file.
#-------------------------------------------------------------------------------
use Path::Tiny;
use File::stat;
use Carp;
use namespace::clean; #--- Dont export unintentionally
#-------------------------------------------------------------------------------
#  Attributes
#-------------------------------------------------------------------------------
has file => (
    is  => 'ro',
    isa => sub {
        Carp::croak(qq{'in_file' needs to be an existing file and a Path::Tiny Object!})
          unless (ref $_[0] eq 'Path::Tiny' and $_[0]->exists);
    },
    coerce => sub {
        return $_[0]
          if ( ref $_[0] eq 'Path::Tiny' );
        return path( $_[0] );
    },
    required => 1
);

has file_stat => (
    is  => 'rwp',
    isa => sub {
        Carp::croak(qq{$_[0] is not a File::stat::stat!})
          unless ( $_[0] and ( ref $_[0] ) );
    },
);

#-------------------------------------------------------------------------------
#  Builders and Triggers
#-------------------------------------------------------------------------------
sub BUILD {
    my ($self) = @_;
    $self->_set_file_stat( File::stat::stat( $self->file ) )
      or Carp::croak( qq{File stat failed to get the file stat for, }
          . $self->file
          . qq{!: $!} );
}
1;

It would be a good idea to test this module before we proceed further.
I found a this Test::Modern  module by Toby Inkster(who always does good stuff). It’s a testing module that includes many other testing modules(Also see Test::Most).

Here’s a test to make sure that all’s ok with my File::Info module.

use Test::Modern;    #-- I wonder will it be modern in 10 years time??
use FindBin qw($Bin);
use lib qq{$Bin/../lib};    #-- Where File::Info is located.
use File::Info;

my $fi = object_ok(
    sub { File::Info->new( file => qq{IMAG0029.jpg} ) },
    '$fi',
    isa   => [qw(Moo::Object )],
    can   => [qw(  file file_stat )],
    clean => 1,
    more  => sub {
        my $file_info_obj = shift;

        isa_ok( $file_info_obj->file, 'Path::Tiny',
            q{File::Info 'file' is a Path::Tiny object.} );

        is( $file_info_obj->file->basename,
            qq{IMAG0029.jpg},
            q{File::Info 'file' has the correct file basename.} );

        isa_ok( $file_info_obj->file_stat, q{File::stat},
            qq{File::Info 'file_stat' is a 'File::stat'.} );

        like( $file_info_obj->file_stat->size,
            qr/^[0-9]+$/,
            q{File::Info 'file_stat->size' returns a numeric file size.} );
    },
);

done_testing();

I ran this test script and got the following results ( on the first attempt of course :)).

Moo > prove -v t/test_file.t 
t/test_file.t .. 
    # Subtest: $fi ok
    ok 1 - instantiate $fi
    ok 2 - $fi is blessed
    ok 3 - '$fi' isa 'Moo::Object'
    ok 4 - File::Info->can(...)
    ok 5 - File::Info contains no imported functions
        # Subtest: more tests for $fi
        ok 1 - 'File::Info 'file' is a Path::Tiny object.' isa 'Path::Tiny'
        ok 2 - File::Info 'file' has the correct file basename.
        ok 3 - 'File::Info 'file_stat' is a 'File::stat'.' isa 'File::stat'
        ok 4 - File::Info 'file_stat->size' returns a numeric file size.
        ok 5 - no exception thrown by additional tests
        1..5
    ok 6 - more tests for $fi
    1..6
ok 1 - $fi ok
ok 2 - no (unexpected) warnings (via done_testing)
1..2
ok
All tests successful.
Files=1, Tests=2,  0 wallclock secs ( 0.03 usr  0.01 sys +  0.09 cusr  0.00 csys =  0.13 CPU)
Result: PASS
[21:42 - 0.37]
[austin@the-general-II 64] Moo >

 

Test::Modern seems pretty cool.  My File::Info module seems to be ok so far.  Now to add some more useful functionality.