7 eval "use Test::Output;";
8 plan skip_all => "Test::Output is required for this test" if $@;
11 # this test script ensures that my idiom of:
12 # role: sub BUILD, after BUILD
13 # continues to work to run code after object initialization, whether the class
14 # has a BUILD method or not
15 # note: as of moose 0.95, this idiom is no longer necessary ('after BUILD' on
16 # its own is sufficient) -doy
24 sub BUILD { push @CALLS, 'TestRole::BUILD' }
25 before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' };
26 after BUILD => sub { push @CALLS, 'TestRole::BUILD:after' };
30 package ClassWithBUILD;
37 sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
41 package ExplicitClassWithBUILD;
45 with 'TestRole' => { excludes => 'BUILD' };
48 sub BUILD { push @CALLS, 'ExplicitClassWithBUILD::BUILD' }
52 package ClassWithoutBUILD;
58 package TestRoleWithoutBUILD;
61 before BUILD => sub { push @CALLS, 'TestRoleWithoutBUILD::BUILD:before' };
62 after BUILD => sub { push @CALLS, 'TestRoleWithoutBUILD::BUILD:after' };
66 package AnotherClassWithBUILD;
70 with 'TestRoleWithoutBUILD';
73 sub BUILD { push @CALLS, 'AnotherClassWithBUILD::BUILD' }
77 package AnotherClassWithoutBUILD;
81 with 'TestRoleWithoutBUILD';
86 is_deeply([splice @CALLS], [], "no calls to BUILD yet");
90 is_deeply([splice @CALLS], [
91 'TestRole::BUILD:before',
92 'ClassWithBUILD::BUILD',
93 'TestRole::BUILD:after',
96 ClassWithoutBUILD->new;
98 is_deeply([splice @CALLS], [
99 'TestRole::BUILD:before',
101 'TestRole::BUILD:after',
104 AnotherClassWithBUILD->new;
106 is_deeply([splice @CALLS], [
107 'TestRoleWithoutBUILD::BUILD:before',
108 'AnotherClassWithBUILD::BUILD',
109 'TestRoleWithoutBUILD::BUILD:after',
112 AnotherClassWithoutBUILD->new;
114 is_deeply([splice @CALLS], [
115 'TestRoleWithoutBUILD::BUILD:before',
116 'TestRoleWithoutBUILD::BUILD:after',
118 } qw(ClassWithBUILD ClassWithoutBUILD
119 AnotherClassWithBUILD AnotherClassWithoutBUILD);