Add Class::C3::Componetised::ApplyHooks features
[p5sagit/Class-C3-Componentised.git] / t / 03-on-apply.t
1 use strict;
2 use warnings;
3
4 use FindBin;
5 use Test::More;
6 use Test::Exception;
7
8 use lib "$FindBin::Bin/lib";
9
10 my $awesome_robot = 0;
11 my $first = 0;
12 my $last = 0;
13
14 BEGIN {
15   package MyModule::Plugin::TestActions;
16
17   use Class::C3::Componentised::ApplyHooks;
18
19   BEFORE_APPLY { $awesome_robot++; $first = $awesome_robot };
20   BEFORE_APPLY { $awesome_robot++; $first = $awesome_robot };
21   AFTER_APPLY  { $awesome_robot++;  $last  = $awesome_robot };
22
23   1;
24 }
25
26 BEGIN {
27   package MyModule::Plugin::TestActionDie;
28
29   use Class::C3::Componentised::ApplyHooks
30     -before_apply => sub { die 'this component is not applicable (yuk yuk yuk)' };
31
32   1;
33 }
34
35 BEGIN {
36   package MyModule::Plugin::TestActionLoadFrew;
37
38   use Class::C3::Componentised::ApplyHooks;
39
40   BEFORE_APPLY { $_[0]->load_components('TestActionFrew') };
41
42   1;
43 }
44
45 BEGIN {
46   package MyModule::Plugin::TestActionFrew;
47   sub frew { 1 }
48   1;
49 }
50
51 use_ok('MyModule');
52 is( $first, 0, 'first starts at zero' );
53 is( $last, 0, 'last starts at zero' );
54
55 MyModule->load_components('TestActions');
56 is( $first, 2, 'first gets value of 1 (it runs first)' );
57 is( $last, 3, 'last gets value of 2 (it runs last)' );
58
59 dies_ok { MyModule->load_components('TestActionDie') } 'die from BEFORE_APPLY works';
60
61 dies_ok { MyModule->frew } 'fREW is not loaded';
62 MyModule->load_components('TestActionLoadFrew');
63 is( MyModule->frew, 1, 'fREW is loaded' );
64
65 done_testing;