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