Make Test::Output optional again
[gitmo/Moose.git] / t / 030_roles / 019_build.t
CommitLineData
39443466 1#!/usr/bin/env perl
2use strict;
3use warnings;
3d659f7f 4use Test::More;
5BEGIN {
6 eval "use Test::Output;";
7 plan skip_all => "Test::Output is required for this test" if $@;
8 plan tests => 8;
9}
39443466 10
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
39443466 16my @CALLS;
17
18do {
19 package TestRole;
20 use Moose::Role;
21
22 sub BUILD { push @CALLS, 'TestRole::BUILD' }
23 before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' };
24 after BUILD => sub { push @CALLS, 'TestRole::BUILD:after' };
25};
26
27do {
28 package ClassWithBUILD;
29 use Moose;
e394f5e7 30
f6bee6fe 31 ::stderr_is {
e394f5e7 32 with 'TestRole';
f6bee6fe 33 } '';
39443466 34
35 sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
36};
37
38do {
d443cad0 39 package ExplicitClassWithBUILD;
40 use Moose;
e394f5e7 41
42 ::stderr_is {
43 with 'TestRole' => { excludes => 'BUILD' };
44 } '';
d443cad0 45
46 sub BUILD { push @CALLS, 'ExplicitClassWithBUILD::BUILD' }
47};
48
49do {
39443466 50 package ClassWithoutBUILD;
51 use Moose;
52 with 'TestRole';
53};
54
27f4baaf 55{
56 is_deeply([splice @CALLS], [], "no calls to BUILD yet");
57
58 ClassWithBUILD->new;
59
60 is_deeply([splice @CALLS], [
61 'TestRole::BUILD:before',
62 'ClassWithBUILD::BUILD',
63 'TestRole::BUILD:after',
64 ]);
65
66 ClassWithoutBUILD->new;
67
68 is_deeply([splice @CALLS], [
69 'TestRole::BUILD:before',
70 'TestRole::BUILD',
71 'TestRole::BUILD:after',
72 ]);
73
74 if (ClassWithBUILD->meta->is_mutable) {
75 ClassWithBUILD->meta->make_immutable;
76 ClassWithoutBUILD->meta->make_immutable;
77 redo;
78 }
79}
39443466 80