Fix test failures when user does not have C::Method::Modifiers(::Fast) installed
[gitmo/Mouse.git] / t / 030_roles / 019_build.t
CommitLineData
67199842 1#!/usr/bin/env perl
2use strict;
3use warnings;
49a56bba 4use Test::More;
5BEGIN {
6 plan skip_all =>
7 "This test requires Class::Method::Modifiers or Class::Method::Modifiers::Fast"
8 unless eval {
9 require Class::Method::Modifiers::Fast;
10 } or eval {
11 require Class::Method::Modifiers;
12 };
13}
14
15plan tests => 6;
16
67199842 17
18# this test script ensures that my idiom of:
19# role: sub BUILD, after BUILD
20# continues to work to run code after object initialization, whether the class
21# has a BUILD method or not
22
23my @CALLS;
24
25do {
26 package TestRole;
27 use Mouse::Role;
28
29 sub BUILD { push @CALLS, 'TestRole::BUILD' }
30 before BUILD => sub { push @CALLS, 'TestRole::BUILD:before' };
31 after BUILD => sub { push @CALLS, 'TestRole::BUILD:after' };
32};
33
34do {
35 package ClassWithBUILD;
36 use Mouse;
37 with 'TestRole';
38
39 sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
40};
41
42do {
43 package ClassWithoutBUILD;
44 use Mouse;
45 with 'TestRole';
46};
47
48is_deeply([splice @CALLS], [], "no calls to BUILD yet");
49
50ClassWithBUILD->new;
51
52is_deeply([splice @CALLS], [
53 'TestRole::BUILD:before',
54 'ClassWithBUILD::BUILD',
55 'TestRole::BUILD:after',
56]);
57
58ClassWithoutBUILD->new;
59
60is_deeply([splice @CALLS], [
61 'TestRole::BUILD:before',
62 'TestRole::BUILD',
63 'TestRole::BUILD:after',
64]);
65
66ClassWithBUILD->meta->make_immutable;
67ClassWithoutBUILD->meta->make_immutable;
68
69is_deeply([splice @CALLS], [], "no calls to BUILD yet");
70
71ClassWithBUILD->new;
72
73is_deeply([splice @CALLS], [
74 'TestRole::BUILD:before',
75 'ClassWithBUILD::BUILD',
76 'TestRole::BUILD:after',
77]);
78
79ClassWithoutBUILD->new;
80
81is_deeply([splice @CALLS], [
82 'TestRole::BUILD:before',
83 'TestRole::BUILD',
84 'TestRole::BUILD:after',
85]);
86