From: Shawn M Moore Date: Sat, 19 Jul 2008 22:18:42 +0000 (+0000) Subject: Add a test script to make sure my idiom of "sub BUILD, after BUILD" in a role will... X-Git-Tag: 0_55~45 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=39443466d42bdfefb2a1da1b622c51176d35d5a8;p=gitmo%2FMoose.git Add a test script to make sure my idiom of "sub BUILD, after BUILD" in a role will work tomorrow --- diff --git a/t/030_roles/019_build.t b/t/030_roles/019_build.t new file mode 100644 index 0000000..fe38736 --- /dev/null +++ b/t/030_roles/019_build.t @@ -0,0 +1,78 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More tests => 7; + +# this test script ensures that my idiom of: +# role: sub BUILD, after BUILD +# continues to work to run code after object initialization, whether the class +# has a BUILD method or not + +BEGIN { + use_ok('Moose::Role'); +} + +my @CALLS; + +do { + package TestRole; + use Moose::Role; + + sub BUILD { push @CALLS, 'TestRole::BUILD' } + before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' }; + after BUILD => sub { push @CALLS, 'TestRole::BUILD:after' }; +}; + +do { + package ClassWithBUILD; + use Moose; + with 'TestRole'; + + sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' } +}; + +do { + package ClassWithoutBUILD; + use Moose; + with 'TestRole'; +}; + +is_deeply([splice @CALLS], [], "no calls to BUILD yet"); + +ClassWithBUILD->new; + +is_deeply([splice @CALLS], [ + 'TestRole::BUILD:before', + 'ClassWithBUILD::BUILD', + 'TestRole::BUILD:after', +]); + +ClassWithoutBUILD->new; + +is_deeply([splice @CALLS], [ + 'TestRole::BUILD:before', + 'TestRole::BUILD', + 'TestRole::BUILD:after', +]); + +ClassWithBUILD->meta->make_immutable; +ClassWithoutBUILD->meta->make_immutable; + +is_deeply([splice @CALLS], [], "no calls to BUILD yet"); + +ClassWithBUILD->new; + +is_deeply([splice @CALLS], [ + 'TestRole::BUILD:before', + 'ClassWithBUILD::BUILD', + 'TestRole::BUILD:after', +]); + +ClassWithoutBUILD->new; + +is_deeply([splice @CALLS], [ + 'TestRole::BUILD:before', + 'TestRole::BUILD', + 'TestRole::BUILD:after', +]); +