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