Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / roles / build.t
CommitLineData
39443466 1#!/usr/bin/env perl
2use strict;
3use warnings;
3d659f7f 4use Test::More;
4d438a84 5
6use Test::Requires {
7 'Test::Output' => '0.01', # skip all if not installed
8};
39443466 9
10# this test script ensures that my idiom of:
11# role: sub BUILD, after BUILD
12# continues to work to run code after object initialization, whether the class
13# has a BUILD method or not
14
39443466 15my @CALLS;
16
17do {
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
26do {
27 package ClassWithBUILD;
28 use Moose;
e394f5e7 29
f6bee6fe 30 ::stderr_is {
e394f5e7 31 with 'TestRole';
f6bee6fe 32 } '';
39443466 33
34 sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
35};
36
37do {
d443cad0 38 package ExplicitClassWithBUILD;
39 use Moose;
e394f5e7 40
41 ::stderr_is {
82f8ace6 42 with 'TestRole' => { -excludes => 'BUILD' };
e394f5e7 43 } '';
d443cad0 44
45 sub BUILD { push @CALLS, 'ExplicitClassWithBUILD::BUILD' }
46};
47
48do {
39443466 49 package ClassWithoutBUILD;
50 use Moose;
51 with 'TestRole';
52};
53
17819f17 54{
27f4baaf 55 is_deeply([splice @CALLS], [], "no calls to BUILD yet");
56
57 ClassWithBUILD->new;
58
59 is_deeply([splice @CALLS], [
60 'TestRole::BUILD:before',
61 'ClassWithBUILD::BUILD',
62 'TestRole::BUILD:after',
63 ]);
64
65 ClassWithoutBUILD->new;
66
67 is_deeply([splice @CALLS], [
68 'TestRole::BUILD:before',
69 'TestRole::BUILD',
70 'TestRole::BUILD:after',
71 ]);
72
17819f17 73 if (ClassWithBUILD->meta->is_mutable) {
74 ClassWithBUILD->meta->make_immutable;
75 ClassWithoutBUILD->meta->make_immutable;
76 redo;
77 }
78}
39443466 79
a28e50e4 80done_testing;