Entering MooseX, The Last Test I Promise*

Well nearing the end of my test now, I have done the basic stuff in this post,the api parts here, the validation bits in this one and the instance bits with my last one. So to carry on I think it would be prudent to test this in a composed setting though I doubt anyone would use it like that.

So will name this one 40-composit and I start as usual with my testing boilerplate and my three little roles

 
#!perl -T
use Test::More;

{
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 I lifted the test class from the original .t and adapted it to my use like this;

 
{ package MyRole;
  use Moose::Role;
  with 'MooseX::Meta::Method::Role::Authorized';
};

Next I use some new test packages this time I create them as standard Moose classes with one sub and one of my test role

 
{  
   package test_one_of_pass;
   use Moose;
   with 'test_role2';
   sub ping {
      return "ping test_one_of_pass";
   }
} 
{  
   package test_one_of_fail;
    use Moose;
   with 'test_role2';

sub ping {
return "ping test_one_of_fail";
};
}

Now the very Mooseish bits, I get the meta class for the 'ping' method from my class like this;

 
my $meth = test_one_of_pass->meta->get_method('ping');
my $meth2 = test_one_of_fail->meta->get_method('ping');

Next I apply the 'MyRole' to that method, This one has the 'MooseX::Meta::Method::Role::Authorized' role and it has one param 'requires' and I need to fill that in with the API hashref in this test it is the 'one_of' key and array-ref

 
MyRole->meta->apply($meth, rebless_params => {requires=>{ one_of => ['test_role2'] }});
MyRole->meta->apply($meth2, rebless_params => {requires=>{ one_of => ['test_role3'] }});

Now I just redo the API test again like this;

 
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");

and give it a run and I get

 
D:\Blogs\Moosex-AuthorizedMethodRoles\t\40-Composed.t .. 
1..2
ok 1 - one_of pass
not ok 2 - test_one_of_fail
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/2 subtests 
Opps!! That's not right failing #2 means I am able to call the 'ping' sub without a die, so looks like this may be the last test post but not the last post in this series because it looks like I have some debugging to do.

moose_in_ice2.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