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