341b1c901d43d3dd1e91781fb41daa7cd837f54a
[gitmo/Mouse.git] / t / 030_roles / 019_build.t
1 #!/usr/bin/env perl
2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
5 use strict;
6 use warnings;
7 use Test::More;
8
9 use Test::Requires {
10     'Test::Output' => '0.01', # skip all if not installed
11 };
12
13 # this test script ensures that my idiom of:
14 # role: sub BUILD, after BUILD
15 # continues to work to run code after object initialization, whether the class
16 # has a BUILD method or not
17
18 my @CALLS;
19
20 do {
21     package TestRole;
22     use Mouse::Role;
23
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' };
27 };
28
29 do {
30     package ClassWithBUILD;
31     use Mouse;
32
33     ::stderr_is {
34         with 'TestRole';
35     } '';
36
37     sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
38 };
39
40 do {
41     package ExplicitClassWithBUILD;
42     use Mouse;
43
44     ::stderr_is {
45         with 'TestRole' => { -excludes => 'BUILD' };
46     } '';
47
48     sub BUILD { push @CALLS, 'ExplicitClassWithBUILD::BUILD' }
49 };
50
51 do {
52     package ClassWithoutBUILD;
53     use Mouse;
54     with 'TestRole';
55 };
56
57 {
58     is_deeply([splice @CALLS], [], "no calls to BUILD yet");
59
60     ClassWithBUILD->new;
61
62     is_deeply([splice @CALLS], [
63         'TestRole::BUILD:before',
64         'ClassWithBUILD::BUILD',
65         'TestRole::BUILD:after',
66     ]);
67
68     ClassWithoutBUILD->new;
69
70     is_deeply([splice @CALLS], [
71         'TestRole::BUILD:before',
72         'TestRole::BUILD',
73         'TestRole::BUILD:after',
74     ]);
75
76     if (ClassWithBUILD->meta->is_mutable) {
77         ClassWithBUILD->meta->make_immutable;
78         ClassWithoutBUILD->meta->make_immutable;
79         redo;
80     }
81 }
82
83 done_testing;