Commit | Line | Data |
e9cf3ae6 |
1 | use Test::More tests => 44; |
af3ff00e |
2 | use strict; |
3 | use warnings; |
4 | |
5 | use_ok('Catalyst'); |
6 | |
7 | my @complist = |
8 | map { "MyApp::$_"; } |
8dc75d26 |
9 | qw/C::Controller M::Model V::View Controller::C Model::M View::V Controller::Model::Dummy::Model Model::Dummy::Model/; |
af3ff00e |
10 | |
8dc75d26 |
11 | my $thingie={}; |
12 | bless $thingie,'MyApp::Model::Test::Object'; |
13 | push @complist,$thingie; |
af3ff00e |
14 | { |
15 | |
16 | package MyApp; |
17 | |
18 | use base qw/Catalyst/; |
19 | |
3b88a455 |
20 | __PACKAGE__->components( { map { ( ref($_)||$_ , $_ ) } @complist } ); |
6977fbff |
21 | |
22 | # allow $c->log->warn to work |
23 | __PACKAGE__->setup_log; |
af3ff00e |
24 | } |
25 | |
26 | is( MyApp->view('View'), 'MyApp::V::View', 'V::View ok' ); |
27 | |
28 | is( MyApp->controller('Controller'), |
29 | 'MyApp::C::Controller', 'C::Controller ok' ); |
30 | |
31 | is( MyApp->model('Model'), 'MyApp::M::Model', 'M::Model ok' ); |
32 | |
8dc75d26 |
33 | is( MyApp->model('Dummy::Model'), 'MyApp::Model::Dummy::Model', 'Model::Dummy::Model ok' ); |
34 | |
35 | isa_ok( MyApp->model('Test::Object'), 'MyApp::Model::Test::Object', 'Test::Object ok' ); |
36 | |
37 | is( MyApp->controller('Model::Dummy::Model'), 'MyApp::Controller::Model::Dummy::Model', 'Controller::Model::Dummy::Model ok' ); |
38 | |
af3ff00e |
39 | is( MyApp->view('V'), 'MyApp::View::V', 'View::V ok' ); |
40 | |
41 | is( MyApp->controller('C'), 'MyApp::Controller::C', 'Controller::C ok' ); |
42 | |
43 | is( MyApp->model('M'), 'MyApp::Model::M', 'Model::M ok' ); |
3b88a455 |
44 | |
6977fbff |
45 | # failed search |
46 | { |
47 | is( MyApp->model('DNE'), undef, 'undef for invalid search' ); |
48 | } |
49 | |
3b88a455 |
50 | is_deeply( [ sort MyApp->views ], |
51 | [ qw/V View/ ], |
52 | 'views ok' ); |
53 | |
54 | is_deeply( [ sort MyApp->controllers ], |
55 | [ qw/C Controller Model::Dummy::Model/ ], |
56 | 'controllers ok'); |
57 | |
58 | is_deeply( [ sort MyApp->models ], |
59 | [ qw/Dummy::Model M Model Test::Object/ ], |
60 | 'models ok'); |
a3b71f0f |
61 | |
6977fbff |
62 | { |
63 | my $warnings = 0; |
64 | no warnings 'redefine'; |
65 | local *Catalyst::Log::warn = sub { $warnings++ }; |
66 | |
67 | like (MyApp->view , qr/^MyApp\::(V|View)\::/ , 'view() with no defaults returns *something*'); |
68 | ok( $warnings, 'view() w/o a default is random, warnings thrown' ); |
69 | } |
a3b71f0f |
70 | |
71 | is ( bless ({stash=>{current_view=>'V'}}, 'MyApp')->view , 'MyApp::View::V', 'current_view ok'); |
72 | |
73 | my $view = bless {} , 'MyApp::View::V'; |
74 | is ( bless ({stash=>{current_view_instance=> $view }}, 'MyApp')->view , $view, 'current_view_instance ok'); |
75 | |
76 | is ( bless ({stash=>{current_view_instance=> $view, current_view=>'MyApp::V::View' }}, 'MyApp')->view , $view, |
77 | 'current_view_instance precedes current_view ok'); |
78 | |
6977fbff |
79 | { |
80 | my $warnings = 0; |
81 | no warnings 'redefine'; |
82 | local *Catalyst::Log::warn = sub { $warnings++ }; |
83 | |
84 | like (MyApp->model , qr/^MyApp\::(M|Model)\::/ , 'model() with no defaults returns *something*'); |
85 | ok( $warnings, 'model() w/o a default is random, warnings thrown' ); |
86 | } |
a3b71f0f |
87 | |
88 | is ( bless ({stash=>{current_model=>'M'}}, 'MyApp')->model , 'MyApp::Model::M', 'current_model ok'); |
89 | |
90 | my $model = bless {} , 'MyApp::Model::M'; |
91 | is ( bless ({stash=>{current_model_instance=> $model }}, 'MyApp')->model , $model, 'current_model_instance ok'); |
92 | |
93 | is ( bless ({stash=>{current_model_instance=> $model, current_model=>'MyApp::M::Model' }}, 'MyApp')->model , $model, |
94 | 'current_model_instance precedes current_model ok'); |
95 | |
96 | MyApp->config->{default_view} = 'V'; |
97 | is ( bless ({stash=>{}}, 'MyApp')->view , 'MyApp::View::V', 'default_view ok'); |
72f87c4b |
98 | is ( MyApp->view , 'MyApp::View::V', 'default_view in class method ok'); |
a3b71f0f |
99 | |
100 | MyApp->config->{default_model} = 'M'; |
101 | is ( bless ({stash=>{}}, 'MyApp')->model , 'MyApp::Model::M', 'default_model ok'); |
72f87c4b |
102 | is ( MyApp->model , 'MyApp::Model::M', 'default_model in class method ok'); |
103 | |
6977fbff |
104 | # regexp behavior tests |
105 | { |
106 | # is_deeply is used because regexp behavior means list context |
107 | is_deeply( [ MyApp->view( qr{^V[ie]+w$} ) ], [ 'MyApp::V::View' ], 'regexp view ok' ); |
108 | is_deeply( [ MyApp->controller( qr{Dummy\::Model$} ) ], [ 'MyApp::Controller::Model::Dummy::Model' ], 'regexp controller ok' ); |
109 | is_deeply( [ MyApp->model( qr{Dum{2}y} ) ], [ 'MyApp::Model::Dummy::Model' ], 'regexp model ok' ); |
fb5f4242 |
110 | |
aa61c190 |
111 | # object w/ qr{} |
fb5f4242 |
112 | is_deeply( [ MyApp->model( qr{Test} ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' ); |
113 | |
114 | { |
115 | my $warnings = 0; |
116 | no warnings 'redefine'; |
117 | local *Catalyst::Log::warn = sub { $warnings++ }; |
118 | |
aa61c190 |
119 | # object w/ regexp fallback |
fb5f4242 |
120 | is_deeply( [ MyApp->model( 'Test' ) ], [ MyApp->components->{'MyApp::Model::Test::Object'} ], 'Object returned' ); |
121 | ok( $warnings, 'regexp fallback warnings' ); |
122 | } |
123 | |
75aff34d |
124 | is_deeply( [ MyApp->view('MyApp::V::View$') ], [ 'MyApp::V::View' ], 'Explicit return ok'); |
125 | is_deeply( [ MyApp->controller('MyApp::C::Controller$') ], [ 'MyApp::C::Controller' ], 'Explicit return ok'); |
126 | is_deeply( [ MyApp->model('MyApp::M::Model$') ], [ 'MyApp::M::Model' ], 'Explicit return ok'); |
6977fbff |
127 | } |
128 | |
129 | { |
130 | my @expected = qw( MyApp::C::Controller MyApp::Controller::C ); |
131 | is_deeply( [ sort MyApp->controller( qr{^C} ) ], \@expected, 'multiple controller returns from regexp search' ); |
132 | } |
133 | |
134 | { |
135 | my @expected = qw( MyApp::V::View MyApp::View::V ); |
136 | is_deeply( [ sort MyApp->view( qr{^V} ) ], \@expected, 'multiple view returns from regexp search' ); |
137 | } |
138 | |
139 | { |
140 | my @expected = qw( MyApp::M::Model MyApp::Model::M ); |
141 | is_deeply( [ sort MyApp->model( qr{^M} ) ], \@expected, 'multiple model returns from regexp search' ); |
142 | } |
143 | |
144 | # failed search |
145 | { |
146 | is( scalar MyApp->controller( qr{DNE} ), 0, '0 results for failed search' ); |
147 | } |
148 | |
72f87c4b |
149 | #checking @args passed to ACCEPT_CONTEXT |
72f87c4b |
150 | { |
e9cf3ae6 |
151 | my $args; |
152 | |
c7ded7aa |
153 | no warnings; |
154 | *MyApp::Model::M::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; |
155 | *MyApp::View::V::ACCEPT_CONTEXT = sub { my ($self, $c, @args) = @_; $args= \@args}; |
6977fbff |
156 | |
e9cf3ae6 |
157 | MyApp->model('M', qw/foo bar/); |
158 | is_deeply($args, [qw/foo bar/], '$c->model args passed to ACCEPT_CONTEXT ok'); |
159 | |
160 | my $x = MyApp->view('V', qw/foo2 bar2/); |
161 | is_deeply($args, [qw/foo2 bar2/], '$c->view args passed to ACCEPT_CONTEXT ok'); |
162 | |
163 | # regexp fallback |
164 | MyApp->view('::View::V', qw/foo3 bar3/); |
165 | is_deeply($args, [qw/foo3 bar3/], 'args passed to ACCEPT_CONTEXT ok'); |
166 | } |