Entering MooseX, Thirteen Lucky for Some

Well going on with my revised tests from my last post I am now going to look at the instance usage of my little MooseX.

Well normally I have been using this MooseX with Classes that use Moose::Util to install new roles so that is what I going to test in this next .t, so lets start with

 
#!perl -T
use Test::More tests => 3;
use Test::Moose::More;
use_ok('MooseX::AuthorizedMethodRoles');

use Moose::Uti;
...

Opps lets go back for a second I should not assume that this Mod is going to be present so I think I will have to leverage Test::More a little and mix things up a bit like this;

 
#!perl -T
use Test::More;
eval {
   require Moose::Util;
};

unless ($@){
plan skip_all =>qw(Skipped all. 'Moose::Util' not installed. Not required but this Module is not much use without it!)

}
else {
plan tests => 2;
}

...

So now I do a quick check with the require wrapped in an eval and if I get an error (something in $@) I skip all my tests and for someone without Moose::Util installed they will get this and my Mod will still install.

 
D:\Blogs\Moosex-AuthorizedMethodRoles\t\30-Instance.t .. skipped: Skipped
Files=1, Tests=0,  0 wallclock secs ( 0.03 usr +  0.00 sys =  0.03 CPU)
Result: NOTESTS

So on with the rest of the test. I have a quick ues_ok of my indented Module (can never have enough of those) and then my 3 little roles we have seen before

 
use_ok('MooseX::AuthorizedMethodRoles');

{
package test_role1;
use Moose::Role;
has 'nothing_r1 '=> (is => 'ro',default=>'role 1');

}
{
package test_role2;
use Moose::Role;
has 'nothing_r2' => (is => 'ro',default=>'role 2');

}
{
package test_role3;
use Moose::Role;
has 'nothing_r3' => (is => 'ro',default=>'role 3');

}

Next in my little Perl package I add in a BEGIN sub that will add in my role something like this

 
{  
   package instance_test_one_of;
   use MooseX::AuthorizedMethodRoles;

sub BUILD {
my $self = shift;
my ($attr) = @_;

Moose::Util::apply_all_roles($self,("test_role1",=>$attr));
}
authorized_roles ping => {one_of=>['test_role1','test_role2']}, sub {
return "ping instance_test_one_of";
};

has 'a_role'=> (is => 'ro',default=>'test_role1');

}

my $test1 = instance_test_one_of->new();
ok($test1->ping(),"instance_test_one_of pass");

and then I just carry on for another 6 test in the same vain as my test from this post and my final results are

 
D:\Blogs\Moosex-AuthorizedMethodRoles\t\30-Instance.t .. 
1..3
ok 1 - use MooseX::AuthorizedMethodRoles;
ok 2
...
ok 8 - instance_test_one_of_Required_fail pass

All tests successful.
Files=1, Tests=8, 1 wallclock secs ( 0.03 usr + 0.00 sys = 0.03 CPU)
Result: PASS

So that covers that just need to do a little more snooping about to see if there any anything else I should test for and I think this is ready for the big time.

fairbanks-moose.jpg

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations