Avoid duplication in running the same tests with and without immutable
[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 => 6;
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 my @CALLS;
12
13 do {
14     package TestRole;
15     use Moose::Role;
16
17     sub BUILD           { push @CALLS, 'TestRole::BUILD' }
18     before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' };
19     after  BUILD => sub { push @CALLS, 'TestRole::BUILD:after' };
20 };
21
22 do {
23     package ClassWithBUILD;
24     use Moose;
25     with 'TestRole';
26
27     sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
28 };
29
30 do {
31     package ClassWithoutBUILD;
32     use Moose;
33     with 'TestRole';
34 };
35
36 {
37     is_deeply([splice @CALLS], [], "no calls to BUILD yet");
38
39     ClassWithBUILD->new;
40
41     is_deeply([splice @CALLS], [
42         'TestRole::BUILD:before',
43         'ClassWithBUILD::BUILD',
44         'TestRole::BUILD:after',
45     ]);
46
47     ClassWithoutBUILD->new;
48
49     is_deeply([splice @CALLS], [
50         'TestRole::BUILD:before',
51         'TestRole::BUILD',
52         'TestRole::BUILD:after',
53     ]);
54
55     if (ClassWithBUILD->meta->is_mutable) {
56         ClassWithBUILD->meta->make_immutable;
57         ClassWithoutBUILD->meta->make_immutable;
58         redo;
59     }
60 }
61