Moose now warns about class implicitly overriding methods from local
[gitmo/Moose.git] / t / 030_roles / 019_build.t
CommitLineData
39443466 1#!/usr/bin/env perl
2use strict;
3use warnings;
d443cad0 4use Test::More tests => 8;
39443466 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
39443466 11my @CALLS;
12
13do {
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
22do {
23 package ClassWithBUILD;
24 use Moose;
25 with 'TestRole';
26
27 sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
28};
29
30do {
d443cad0 31 package ExplicitClassWithBUILD;
32 use Moose;
33 with 'TestRole' => { excludes => 'BUILD' };
34
35 sub BUILD { push @CALLS, 'ExplicitClassWithBUILD::BUILD' }
36};
37
38do {
39443466 39 package ClassWithoutBUILD;
40 use Moose;
41 with 'TestRole';
42};
43
27f4baaf 44{
45 is_deeply([splice @CALLS], [], "no calls to BUILD yet");
46
47 ClassWithBUILD->new;
48
49 is_deeply([splice @CALLS], [
50 'TestRole::BUILD:before',
51 'ClassWithBUILD::BUILD',
52 'TestRole::BUILD:after',
53 ]);
54
d443cad0 55 ExplicitClassWithBUILD->new;
56
57 is_deeply([splice @CALLS], [
58 'TestRole::BUILD:before',
59 'ExplicitClassWithBUILD::BUILD',
60 'TestRole::BUILD:after',
61 ]);
62
27f4baaf 63 ClassWithoutBUILD->new;
64
65 is_deeply([splice @CALLS], [
66 'TestRole::BUILD:before',
67 'TestRole::BUILD',
68 'TestRole::BUILD:after',
69 ]);
70
71 if (ClassWithBUILD->meta->is_mutable) {
72 ClassWithBUILD->meta->make_immutable;
d443cad0 73 ExplicitClassWithBUILD->meta->make_immutable;
27f4baaf 74 ClassWithoutBUILD->meta->make_immutable;
75 redo;
76 }
77}
39443466 78