Does it Pass the Moose Test?

The title of this entry will only mean something to a very select few in the world and if you think it has anything to do with perl look below


Anyway I just finished my version .02 of Acme::Moose as I totally botched version .01 as it is a llama not a moose.

However, that is another story. At least I decided to add in a little testing to this version and what the heck I might as well use the fancy new Test::Moose::More and see what it has to offer.

So after about 10 mins of installation from CPAN I was ready to give it a try.

Well at first glance it took a few mins and a couple of runs of my .t to figure out that 'Test::Moose::More' is really just for testing Moose classes so you have to fall back to something like 'Test::More' if you want to run regular type test, but no matter.

So my first test looked like this

# Base Moose Test
use Acme::Moose; 
use Test::More tests => 4;
use Test::Moose::More;

diag('Loading Moose and version');

my $moose = Acme::Moose->new; 


has_attribute_ok $moose,'foodage', "Can eat"  ;
has_attribute_ok $moose,'happiness',  "Can be happy"  ;
has_attribute_ok $moose,'tired',  "Can be sleepy"  ;

has_method_ok $moose, (qw(feed play nap sacrifice));
and I got this

ok 1 - Acme::Moose has a meta
ok 2 - Can eat
ok 3 - Can be happy
ok 4 - Can be sleepy
All tests successful.
Files=1, Tests=4,  1 wallclock secs ( 0.14 usr +  0.00 sys =  0.14 CPU)
Result: PASS
Fine and dandy. I expanded on this a little by adding the following

has_method_ok $moose, (qw(feed play nap sacrifice));
check_sugar_removed_ok $moose;
and as I expected the test failed as now I am running 16 tests so a slight adjustment to my test plan and all was fine.

So far so good. Now I know I have a Moose class, I have my three attributes, my 4 methods and that I cleaned up all of my Moose poop.

So far the real good one of the bunch is 'check_sugar_removed_ok'. If I happen to have a good number of Moose classes about I can test to ensure I at least did 'no Moose' on them.

I was a little weary about the how much typing I had to do for this testing as at 8 lines of code it is almost the same size as my original module. I then read a little further down the page and found 'validate_class' which kind of made the test I did before a little redundant as all I had to do was this

validate_class $moose => (
    attributes => [ qw(foodage happiness tired) ],
    methods    => [ qw(feed play nap sacrifice) ],
    isa        => [ 'Acme::Moose' ],
    # ensures $thing does not do these roles
    does_not   => [ 'Acme::Llama' ],
So the question becomes would I use this in the future. I would say yes as it is very good for testing the abstract nature of my software to ensure it is compliant to a coding style.

To give an example, if I had a large team creating a very large code base, there is a good chance that someone made the mistake of creating a traditional 'sub' style for something that should be an 'Attribute'. This package can test for that.

I could see it utility as the opening act of a test suite. If I was lucky enough to have all the classes, roles and attributes mapped out first I could create the test that would test for compliance. I could also use the same test suite to see how things are progressing as things will pass as they are being implemented.

I do not think it is meant for functional testing but then again why reinvent the wheel we have Test::More for that. I think in the long run it may save me some testing as I would not have to write a bunch of 'setter' & 'getter' tests for my attributes as I am 99.97% sure the Moose setters and getters will work 98.89% of the time.

One thing that may be missing is a way to test if a 'class' has extra stuff in it. For example if I added another attribute 'hates_llamas' to my there is no test that tells me that this one is not on a list??

So IMHO the 'validate_class' should do this. If I do add in this extra attribute my class is no longer 'valid' as it now has something extra and the test should fail.

Anyway lots to look at and I have a Moose to feed.


Car 0 Moose 0

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