fe3873625f81722e9aba0646992d0f206d4c0ff4
[gitmo/Moose.git] / t / 030_roles / 019_build.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Test::More tests => 7;
5
6 # this test script ensures that my idiom of:
7 # role: sub BUILD, after BUILD
8 # continues to work to run code after object initialization, whether the class
9 # has a BUILD method or not
10
11 BEGIN {
12     use_ok('Moose::Role');
13 }
14
15 my @CALLS;
16
17 do {
18     package TestRole;
19     use Moose::Role;
20
21     sub BUILD           { push @CALLS, 'TestRole::BUILD' }
22     before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' };
23     after  BUILD => sub { push @CALLS, 'TestRole::BUILD:after' };
24 };
25
26 do {
27     package ClassWithBUILD;
28     use Moose;
29     with 'TestRole';
30
31     sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
32 };
33
34 do {
35     package ClassWithoutBUILD;
36     use Moose;
37     with 'TestRole';
38 };
39
40 is_deeply([splice @CALLS], [], "no calls to BUILD yet");
41
42 ClassWithBUILD->new;
43
44 is_deeply([splice @CALLS], [
45     'TestRole::BUILD:before',
46     'ClassWithBUILD::BUILD',
47     'TestRole::BUILD:after',
48 ]);
49
50 ClassWithoutBUILD->new;
51
52 is_deeply([splice @CALLS], [
53     'TestRole::BUILD:before',
54     'TestRole::BUILD',
55     'TestRole::BUILD:after',
56 ]);
57
58 ClassWithBUILD->meta->make_immutable;
59 ClassWithoutBUILD->meta->make_immutable;
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