Merge branch 'master' into gsoc_breadboard
[catagits/Catalyst-Runtime.git] / t / aggregate / unit_core_component.t
1 use Test::More;
2 use strict;
3 use warnings;
4 use FindBin;
5 use lib "$FindBin::Bin/../lib";
6 use TestAppComponent;
7
8 my @complist = map { "TestAppComponent::$_"; } qw/C::Controller M::Model V::View/;
9
10 is(ref TestAppComponent->comp('TestAppComponent::V::View'), 'TestAppComponent::V::View', 'Explicit return ok');
11
12 is(ref TestAppComponent->comp('C::Controller'), 'TestAppComponent::C::Controller', 'Two-part return ok');
13
14 is(ref TestAppComponent->comp('Model'), 'TestAppComponent::M::Model', 'Single part return ok');
15
16 is_deeply([ TestAppComponent->comp() ], \@complist, 'Empty return ok');
17
18 # Is this desired behaviour?
19 is_deeply([ TestAppComponent->comp('Foo') ], \@complist, 'Fallthrough return ok');
20
21 # regexp behavior
22 {
23     is_deeply( [ map { ref $_ } TestAppComponent->comp( qr{Model} ) ], [ 'TestAppComponent::M::Model' ], 'regexp ok' );
24
25     {
26         my $warnings = 0;
27         no warnings 'redefine';
28         local *Catalyst::Log::warn = sub { $warnings++ };
29
30         is_deeply( [ TestAppComponent->comp('::M::Model$') ], \@complist, 'no results for regexp fallback');
31         ok( $warnings, 'regexp fallback warnings' );
32     }
33
34 }
35
36 # multiple returns
37 {
38     # already sorted
39     my @expected = qw( TestAppComponent::C::Controller TestAppComponent::M::Model );
40     my @got = map { ref $_ } sort TestAppComponent->comp( qr{::[MC]::} );
41     is_deeply( \@got, \@expected, 'multiple results from regexp ok' );
42 }
43
44 # failed search
45 {
46     is_deeply( scalar TestAppComponent->comp( qr{DNE} ), 0, 'no results for failed search' );
47 }
48
49
50 #checking @args passed to ACCEPT_CONTEXT
51 {
52     my $args;
53
54     {
55         no warnings 'once';
56         *TestAppComponent::M::Model::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args};
57     }
58
59     my $c = bless {}, 'TestAppComponent';
60
61     $c->component('TestAppComponent::M::Model', qw/foo bar/);
62     is_deeply($args, [qw/foo bar/], 'args passed to ACCEPT_CONTEXT ok');
63
64     $c->component('M::Model', qw/foo2 bar2/);
65     is_deeply($args, [qw/foo2 bar2/], 'args passed to ACCEPT_CONTEXT ok');
66 }
67
68 done_testing;