Add a test for loading Moose::Role into main (which now also warns)
[gitmo/Moose.git] / t / 030_roles / 019_build.t
CommitLineData
39443466 1#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More tests => 7;
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
11BEGIN {
12 use_ok('Moose::Role');
13}
14
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;
29 with 'TestRole';
30
31 sub BUILD { push @CALLS, 'ClassWithBUILD::BUILD' }
32};
33
34do {
35 package ClassWithoutBUILD;
36 use Moose;
37 with 'TestRole';
38};
39
40is_deeply([splice @CALLS], [], "no calls to BUILD yet");
41
42ClassWithBUILD->new;
43
44is_deeply([splice @CALLS], [
45 'TestRole::BUILD:before',
46 'ClassWithBUILD::BUILD',
47 'TestRole::BUILD:after',
48]);
49
50ClassWithoutBUILD->new;
51
52is_deeply([splice @CALLS], [
53 'TestRole::BUILD:before',
54 'TestRole::BUILD',
55 'TestRole::BUILD:after',
56]);
57
58ClassWithBUILD->meta->make_immutable;
59ClassWithoutBUILD->meta->make_immutable;
60
61is_deeply([splice @CALLS], [], "no calls to BUILD yet");
62
63ClassWithBUILD->new;
64
65is_deeply([splice @CALLS], [
66 'TestRole::BUILD:before',
67 'ClassWithBUILD::BUILD',
68 'TestRole::BUILD:after',
69]);
70
71ClassWithoutBUILD->new;
72
73is_deeply([splice @CALLS], [
74 'TestRole::BUILD:before',
75 'TestRole::BUILD',
76 'TestRole::BUILD:after',
77]);
78