Entering MooseX, Part the Tenth

Well back on the Moose path again and today a little post on my next test for my first little Moosex. Like I said in my last post I like my tests to be simple and functional.

I did have look at the test suite for MooseX-AuthorizedMethods and for me it was a little too Mooseish fro my tastes, I guess he was very keen on testing to see if bits of Moose worked rather than focusing on the functionality of the modual. For example I did not see test of using a Custom Verifiers at least one that I could figure out. Like I said in a previous post, one of those would been nice.

So lets just stick with the basic functionality no need I think to get into all the Moose side of stuff with the meta etc.

So I start with

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

Well I need some roles and then a package with some a few methods with my declartor applied. I like it simple and stupid so here we go

 
{
   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');
   
   
}

{
package test_one_of_pass;
use MooseX::AuthorizedMethodRoles;
with 'test_role1';
authorized_roles ping => {one_of=>['test_role1','test_role2']}, sub {
return "ping test_one_of_pass";
};

}
{
package test_one_of_fail;
use MooseX::AuthorizedMethodRoles;
with 'test_role3';
authorized_roles ping => {one_of=>['test_role1','test_role2']}, sub {
return "ping test_one_of_fail";
};
}

{
package test_required_pass;
use MooseX::AuthorizedMethodRoles;
with 'test_role1';
authorized_roles ping => { required =>['test_role1']}, sub {
return "ping test_required_pass";
};
}
{
package test_required_fail;
use MooseX::AuthorizedMethodRoles;
with 'test_role2';
authorized_roles ping => { required =>['test_role1']}, sub {
return "ping test_required_fail";
};

}
{
package test_required_pass_2;
use MooseX::AuthorizedMethodRoles;
with qw(test_role1 test_role2);
authorized_roles ping => { required =>['test_role1','test_role2']}, sub {
return "ping test_required_pass_2";
};
}
{
package test_required_fail_2;
use MooseX::AuthorizedMethodRoles;
with qw(test_role1 test_role2);
authorized_roles ping => { required =>['test_role1','test_role2','test_role3']}, sub {
return "ping test_required_fail_2";
};
}

{
package test_all;
use MooseX::AuthorizedMethodRoles;
with qw(test_role1 test_role3);
authorized_roles ping => { required =>['test_role1'],
one_of=>['test_role2','test_role3']}, sub {
return "ping test_all";
};

}

and now just some very simple tests for the above

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

my $test2 = test_one_of_fail->new();

eval {
$test2->ping();
};

ok(scalar($@),"test_one_of_fail");

my $test3 = test_required_pass->new();

ok($test3->ping(),"test_required_pass");

my $test4 = test_required_fail->new();

eval {
$test4->ping();
};

ok(scalar($@),"test_required_fail");

my $test5 = test_required_pass_2->new();

ok($test5->ping(),"test_required_pass_2");

my $test6 = test_required_fail_2->new();

eval {
$test6->ping();
};

ok(scalar($@),"test_required_fail_2");

my $test7 = test_all->new();

ok($test7->ping(),"test_all");

and my basic API is covered, and yes in the file I did go back and changed the test count from 1 to 8 so it all works. I ran it was happy so so am I. Now what this simple test set does give you is a good example of how it is suppose to work. I can't tell you the number of times I have had to dig into the .t files of a mod to see how it really should work when shall we say the POD just doesn't cut it.

The only little note on the test is my using scalar($@) to ensure my method call failed. I have found in the past that differing flavors of OS/Test will handle $@ differently and may result in false positives so this just enforces what I want.

Well that is a start I still have a number of other things to test such as the die messages, using the wrong key on the API or missing an array on the API and I think I should test using an improper key in the API.

Oh well there are a few posts left in this me thinks

tumblr_lm9tvvFHnk1qfalqto1_500.png

N.B.

Seems I had a bug in the original version of this post. See this post on how I goofed.

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