Make anonymous classes work correctly
[gitmo/Mouse.git] / t / 030_roles / 019_build.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More;
5 BEGIN {
6     eval "use Test::Output;";
7     plan skip_all => "Test::Output is required for this test" if $@;
8
9     unless(eval { require Class::Method::Modifiers::Fast } or eval{ require Class::Method::Modifiers }){
10         plan skip_all => "Class::Method::Modifiers(::Fast)? is required for this test" if $@;
11     }
12
13     plan tests => 8;
14 }
15
16 # this test script ensures that my idiom of:
17 # role: sub BUILD, after BUILD
18 # continues to work to run code after object initialization, whether the class
19 # has a BUILD method or not
20
21 my @CALLS;
22
23 do {
24     package TestRole;
25     use Mouse::Role;
26
27     sub BUILD           { push @CALLS, 'TestRole::BUILD' }
28     before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' };
29     after  BUILD => sub { push @CALLS, 'TestRole::BUILD:after' };
30 };
31
32 do {
33     package ClassWithBUILD;
34     use Mouse;
35
36     ::stderr_is {
37         with 'TestRole';
38     } '';
39
40     sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
41 };
42
43 do {
44     package ExplicitClassWithBUILD;
45     use Mouse;
46
47     ::stderr_is {
48         with 'TestRole' => { excludes => 'BUILD' };
49     } '';
50
51     sub BUILD { push @CALLS, 'ExplicitClassWithBUILD::BUILD' }
52 };
53
54 do {
55     package ClassWithoutBUILD;
56     use Mouse;
57     with 'TestRole';
58 };
59
60 {
61     is_deeply([splice @CALLS], [], "no calls to BUILD yet");
62
63     ClassWithBUILD->new;
64
65     is_deeply([splice @CALLS], [
66         'TestRole::BUILD:before',
67         'ClassWithBUILD::BUILD',
68         'TestRole::BUILD:after',
69     ]);
70
71     ClassWithoutBUILD->new;
72
73     is_deeply([splice @CALLS], [
74         'TestRole::BUILD:before',
75         'TestRole::BUILD',
76         'TestRole::BUILD:after',
77     ]);
78
79     if (ClassWithBUILD->meta->is_mutable) {
80         ClassWithBUILD->meta->make_immutable;
81         ClassWithoutBUILD->meta->make_immutable;
82         redo;
83     }
84 }
85