Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: Writing tests when you don't know what the output should be

by choroba (Cardinal)
on May 17, 2016 at 22:24 UTC ( [id://1163274]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Writing tests when you don't know what the output should be
in thread Writing tests when you don't know what the output should be

OK, try adding something like
use FindBin; use Test::More tests => 3; use Test::Exception; dies_ok { 'DupEventChecker'->new } 'dir_name not provided'; my $checker = 'DupEventChecker'->new(dir_name => $FindBin::Bin); isa_ok $checker, 'DupEventChecker'; my ($itself) = grep { 0 <= index $0, $_ } $checker->dir->read; ok $itself, 'test script found';

BTW, dir_name is required, but it isn't apparent from the declaration. dir could also have a default (e.g. '.', Cwd::getcwd or File::HomeDir->my_home).

has 'dir' => ( is => 'ro', isa => 'IO::Dir', required => 1 ); around BUILDARGS => sub { $_ = shift for my ($orig, $class); my %args = @_; die 'required dir missing' unless exists $args{dir}; return $class->$orig(dir => 'IO::Dir'->new($args{dir})) }

Or, maybe more clearly,

#!/usr/bin/perl use warnings; use strict; { package DupEventChecker; use IO::Dir; use Moose; use namespace::autoclean; has 'dir' => ( is => 'ro', isa => 'Str', required => 1 ); has '_dir' => ( is => 'ro', isa => 'IO::Dir', lazy => 1, builder => '_build_dir', handles => { read_dir => 'read' }, init_arg => undef, ); sub _build_dir { 'IO::Dir'->new(shift->dir) } __PACKAGE__->meta->make_immutable; } use FindBin; use Test::More tests => 3; use Test::Exception; dies_ok { 'DupEventChecker'->new } 'dir not provided'; my $checker = 'DupEventChecker'->new(dir => $FindBin::Bin); isa_ok $checker, 'DupEventChecker'; my $itself = grep $0 =~ m{(?:^|/)\Q$_\E$}x, $checker->read_dir; is $itself, 1, 'test script found';

Update: Example added to the BTW. Update 2: The example was improved.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^6: Writing tests when you don't know what the output should be
by nysus (Parson) on May 18, 2016 at 03:54 UTC

    Thanks for this. Can you please help me understand why you used an internal and external accessor for dir? I'm new to Moose and I'm not quite sure why you did that. Thanks.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      It depends on what you want. My solution expects the user only wants to supply the directory name and doesn't care about the internal implementation. It locks in a dependency, though, so another approach would be to accept just an object with a defined interface (dependency injection) - it's easier to test and extend.

      You should start with a picture, define the behaviours of the objects, and the implementation should emerge from the design, not vice versa.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re^6: Writing tests when you don't know what the output should be
by nysus (Parson) on May 19, 2016 at 15:43 UTC

    One other question about your example if I might impose. That second to last line has me scratching my head. The grep $0 is nothing I'm familiar with and the regex has me baffled as well. Thanks!

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      my ($itself) = grep { 0 <= index $0, $_ } $checker->dir->read;

      See $0, index, grep. In other words, the read method returns the contents of the directory, grep then tries to find a file name $_ that's contained in the test script's path (it's buggy, as it doesn't check whether the preceding character is a slash).

      m{(?:^|/)\Q$_\E$}x

      The regex fixes the issue: it checks that the filename is present in the script's name from the beginning (^) or a slash to the end ($). \Q quotes special characters in the filename so it matches literally.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1163274]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found