| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 34; |
| 4 |
use Test::Fatal; |
| 5 |
use Scalar::Util 'refaddr'; |
| 6 |
use Plack::Test; |
| 7 |
use HTTP::Request::Common; |
| 8 |
|
| 9 |
BEGIN { |
| 10 |
require Dancer2; |
| 11 |
can_ok( Dancer2::, 'runner' ); |
| 12 |
is( Dancer2::->runner, undef, 'No runner by default' ); |
| 13 |
} |
| 14 |
|
| 15 |
{ |
| 16 |
package App::CreatingRunner; |
| 17 |
use Dancer2; |
| 18 |
} |
| 19 |
|
| 20 |
isa_ok( Dancer2->runner, 'Dancer2::Core::Runner', 'Runner created' ); |
| 21 |
my $runner_refaddr = refaddr( Dancer2->runner ); |
| 22 |
|
| 23 |
{ |
| 24 |
package App::NotRecreatingRunner; |
| 25 |
use Dancer2; |
| 26 |
} |
| 27 |
|
| 28 |
isa_ok( Dancer2->runner, 'Dancer2::Core::Runner', 'Runner created' ); |
| 29 |
is( refaddr( Dancer2->runner ), $runner_refaddr, 'Runner not recreated' ); |
| 30 |
|
| 31 |
{ |
| 32 |
{ |
| 33 |
package FakeRunner; |
| 34 |
sub psgi_app { |
| 35 |
::isa_ok( $_[0], 'FakeRunner' ); |
| 36 |
::is( $_[1], 'psgi_param', 'psgi_app calls Runner->psgi_app' ); |
| 37 |
return 'Got it'; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
local $Dancer2::runner = bless {}, 'FakeRunner'; |
| 42 |
::is( |
| 43 |
Dancer2->psgi_app('psgi_param'), |
| 44 |
'Got it', |
| 45 |
'psgi_app works as expected', |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
{ |
| 50 |
package App::ScriptAllowed; |
| 51 |
require Dancer2; |
| 52 |
|
| 53 |
::is( |
| 54 |
::exception { Dancer2->import(':script') }, |
| 55 |
undef, |
| 56 |
':script is allowed', |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
{ |
| 61 |
package App::TestsAllowed; |
| 62 |
require Dancer2; |
| 63 |
|
| 64 |
::is( |
| 65 |
::exception { Dancer2->import(':tests') }, |
| 66 |
undef, |
| 67 |
':tests is allowed', |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
{ |
| 72 |
package App::SyntaxAllowed; |
| 73 |
require Dancer2; |
| 74 |
|
| 75 |
::is( |
| 76 |
::exception { Dancer2->import(':syntax') }, |
| 77 |
undef, |
| 78 |
':syntax is allowed', |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
{ |
| 83 |
package App::KeyPairOnly; |
| 84 |
require Dancer2; |
| 85 |
|
| 86 |
::like( |
| 87 |
::exception { Dancer2->import('single') }, |
| 88 |
qr{^parameters must be key/value pairs}, |
| 89 |
'Must import key/value pairs', |
| 90 |
); |
| 91 |
|
| 92 |
::like( |
| 93 |
::exception { Dancer2->import(qw<three items requested>) }, |
| 94 |
qr{^parameters must be key/value pairs}, |
| 95 |
'Must import key/value pairs', |
| 96 |
); |
| 97 |
|
| 98 |
::is( |
| 99 |
::exception { Dancer2->import( '!unless' ) }, |
| 100 |
undef, |
| 101 |
'Must import key/value pairs unless prefixed by !', |
| 102 |
); |
| 103 |
|
| 104 |
::is( |
| 105 |
::exception { Dancer2->import( '!unless', '!prefixed', '!bythis' ) }, |
| 106 |
undef, |
| 107 |
'Must import key/value pairs unless prefixed by !', |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
{ |
| 112 |
package App::GettingDSL; |
| 113 |
use Dancer2; |
| 114 |
|
| 115 |
::can_ok( __PACKAGE__, qw<get post> ); |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
package App::GettingSelectiveDSL; |
| 120 |
use Dancer2 '!post'; |
| 121 |
|
| 122 |
# proper way |
| 123 |
::can_ok( __PACKAGE__, 'get' ); |
| 124 |
|
| 125 |
# checking this would work too |
| 126 |
::ok( __PACKAGE__->can('get'), 'get imported successfully' ); |
| 127 |
::ok( ! __PACKAGE__->can('post'), 'Can import keywords selectively' ); |
| 128 |
} |
| 129 |
|
| 130 |
{ |
| 131 |
package App::Main; |
| 132 |
use Dancer2; |
| 133 |
get '/main' => sub {1}; |
| 134 |
} |
| 135 |
|
| 136 |
{ |
| 137 |
package App::ComposedToMain; |
| 138 |
use Dancer2 appname => 'App::Main'; |
| 139 |
get '/alsomain' => sub {1}; |
| 140 |
} |
| 141 |
|
| 142 |
{ |
| 143 |
my $runner = Dancer2->runner; |
| 144 |
isa_ok( $runner, 'Dancer2::Core::Runner' ); |
| 145 |
my $apps = $runner->apps; |
| 146 |
|
| 147 |
cmp_ok( |
| 148 |
scalar @{$apps}, |
| 149 |
'==', |
| 150 |
12, |
| 151 |
'Correct number of Apps created so far', |
| 152 |
); |
| 153 |
|
| 154 |
my @names = sort map +( $_->name ), @{$apps}; |
| 155 |
|
| 156 |
# this is the list of all Apps loaded in this test |
| 157 |
is_deeply( |
| 158 |
\@names, |
| 159 |
[qw< |
| 160 |
App::CreatingRunner |
| 161 |
App::GettingDSL |
| 162 |
App::GettingSelectiveDSL |
| 163 |
App::KeyPairOnly |
| 164 |
App::Main |
| 165 |
App::NoStrictNoWarningsNoUTF8 |
| 166 |
App::NotRecreatingRunner |
| 167 |
App::ScriptAllowed |
| 168 |
App::StrictAndWarningsAndUTF8 |
| 169 |
App::SyntaxAllowed |
| 170 |
App::TestsAllowed |
| 171 |
App::WithSettingsChanged |
| 172 |
>], |
| 173 |
'All apps accounted for', |
| 174 |
); |
| 175 |
|
| 176 |
my $app = App::Main->to_app; |
| 177 |
isa_ok( $app, 'CODE' ); |
| 178 |
test_psgi $app, sub { |
| 179 |
my $cb = shift; |
| 180 |
is( |
| 181 |
$cb->( GET '/main' )->content, |
| 182 |
1, |
| 183 |
'Got original app response', |
| 184 |
); |
| 185 |
|
| 186 |
is( |
| 187 |
$cb->( GET '/alsomain' )->content, |
| 188 |
1, |
| 189 |
'Can compose apps with appname', |
| 190 |
); |
| 191 |
}; |
| 192 |
} |
| 193 |
|
| 194 |
{ |
| 195 |
package App::WithSettingsChanged; |
| 196 |
use Dancer2; |
| 197 |
} |
| 198 |
|
| 199 |
{ |
| 200 |
App::WithSettingsChanged->import( with => { layout => 'mobile' } ); |
| 201 |
|
| 202 |
my ($app) = grep +( $_->name eq 'App::WithSettingsChanged' ), |
| 203 |
@{ Dancer2->runner->{'apps'} }; |
| 204 |
|
| 205 |
::isa_ok( $app, 'Dancer2::Core::App' ); |
| 206 |
::is( |
| 207 |
$app->template_engine->{'layout'}, |
| 208 |
'mobile', |
| 209 |
'Changed settings using with keyword', |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
{ |
| 214 |
package App::NoStrictNoWarningsNoUTF8; |
| 215 |
use Dancer2; |
| 216 |
no strict; |
| 217 |
no warnings; |
| 218 |
no utf8; |
| 219 |
|
| 220 |
local $@ = undef; |
| 221 |
|
| 222 |
eval '$var = 30'; |
| 223 |
|
| 224 |
::is( |
| 225 |
$@, |
| 226 |
'', |
| 227 |
'no strict (control test)', |
| 228 |
); |
| 229 |
|
| 230 |
local $SIG{'__WARN__'} = sub { |
| 231 |
::is( |
| 232 |
$_[0], |
| 233 |
undef, |
| 234 |
'no warning (control test)', |
| 235 |
); |
| 236 |
}; |
| 237 |
|
| 238 |
eval 'my $var; my $var;'; |
| 239 |
|
| 240 |
my $str = "щука"; |
| 241 |
::isnt( length $str, 4, 'utf8 pragma not imported' ); |
| 242 |
} |
| 243 |
|
| 244 |
{ |
| 245 |
package App::StrictAndWarningsAndUTF8; |
| 246 |
use Dancer2; |
| 247 |
|
| 248 |
local $@ = undef; |
| 249 |
|
| 250 |
local $SIG{'__WARN__'} = sub { |
| 251 |
::ok( |
| 252 |
$_[0], |
| 253 |
'warnings pragma imported', |
| 254 |
); |
| 255 |
}; |
| 256 |
|
| 257 |
eval '$var = 30;'; |
| 258 |
|
| 259 |
::like( |
| 260 |
$@, |
| 261 |
qr/^Global symbol/, |
| 262 |
'strict pragma imported', |
| 263 |
); |
| 264 |
|
| 265 |
eval 'my $var; my $var;'; |
| 266 |
|
| 267 |
my $str = "щука"; |
| 268 |
::is( length $str, 4, 'utf8 pragma imported' ); |
| 269 |
} |
| 270 |
|