File: import.t

package info (click to toggle)
libdancer2-perl 0.400001%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,580 kB
  • sloc: perl: 8,461; makefile: 9
file content (270 lines) | stat: -rw-r--r-- 5,416 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use strict;
use warnings;
use Test::More tests => 34;
use Test::Fatal;
use Scalar::Util 'refaddr';
use Plack::Test;
use HTTP::Request::Common;

BEGIN {
    require Dancer2;
    can_ok( Dancer2::, 'runner' );
    is( Dancer2::->runner, undef, 'No runner by default' );
}

{
    package App::CreatingRunner;
    use Dancer2;
}

isa_ok( Dancer2->runner, 'Dancer2::Core::Runner', 'Runner created' );
my $runner_refaddr = refaddr( Dancer2->runner );

{
    package App::NotRecreatingRunner;
    use Dancer2;
}

isa_ok( Dancer2->runner, 'Dancer2::Core::Runner', 'Runner created' );
is( refaddr( Dancer2->runner ), $runner_refaddr, 'Runner not recreated' );

{
    {
        package FakeRunner;
        sub psgi_app {
            ::isa_ok( $_[0], 'FakeRunner' );
            ::is( $_[1], 'psgi_param', 'psgi_app calls Runner->psgi_app' );
            return 'Got it';
        }
    }

    local $Dancer2::runner = bless {}, 'FakeRunner';
    ::is(
        Dancer2->psgi_app('psgi_param'),
        'Got it',
        'psgi_app works as expected',
    );
}

{
    package App::ScriptAllowed;
    require Dancer2;

    ::is(
        ::exception { Dancer2->import(':script') },
        undef,
        ':script is allowed',
    );
}

{
    package App::TestsAllowed;
    require Dancer2;

    ::is(
        ::exception { Dancer2->import(':tests') },
        undef,
        ':tests is allowed',
    );
}

{
    package App::SyntaxAllowed;
    require Dancer2;

    ::is(
        ::exception { Dancer2->import(':syntax') },
        undef,
        ':syntax is allowed',
    );
}

{
    package App::KeyPairOnly;
    require Dancer2;

    ::like(
        ::exception { Dancer2->import('single') },
        qr{^parameters must be key/value pairs},
        'Must import key/value pairs',
    );

    ::like(
        ::exception { Dancer2->import(qw<three items requested>) },
        qr{^parameters must be key/value pairs},
        'Must import key/value pairs',
    );

    ::is(
        ::exception { Dancer2->import( '!unless' ) },
        undef,
        'Must import key/value pairs unless prefixed by !',
    );

    ::is(
        ::exception { Dancer2->import( '!unless', '!prefixed', '!bythis' ) },
        undef,
        'Must import key/value pairs unless prefixed by !',
    );
}

{
    package App::GettingDSL;
    use Dancer2;

    ::can_ok( __PACKAGE__, qw<get post> );
}

{
    package App::GettingSelectiveDSL;
    use Dancer2 '!post';

    # proper way
    ::can_ok( __PACKAGE__, 'get' );

    # checking this would work too
    ::ok( __PACKAGE__->can('get'), 'get imported successfully' );
    ::ok( ! __PACKAGE__->can('post'), 'Can import keywords selectively' );
}

{
    package App::Main;
    use Dancer2;
    get '/main' => sub {1};
}

{
    package App::ComposedToMain;
    use Dancer2 appname => 'App::Main';
    get '/alsomain' => sub {1};
}

{
    my $runner = Dancer2->runner;
    isa_ok( $runner, 'Dancer2::Core::Runner' );
    my $apps = $runner->apps;

    cmp_ok(
        scalar @{$apps},
        '==',
        12,
        'Correct number of Apps created so far',
    );

    my @names = sort map +( $_->name ), @{$apps};

    # this is the list of all Apps loaded in this test
    is_deeply(
        \@names,
        [qw<
            App::CreatingRunner
            App::GettingDSL
            App::GettingSelectiveDSL
            App::KeyPairOnly
            App::Main
            App::NoStrictNoWarningsNoUTF8
            App::NotRecreatingRunner
            App::ScriptAllowed
            App::StrictAndWarningsAndUTF8
            App::SyntaxAllowed
            App::TestsAllowed
            App::WithSettingsChanged
        >],
        'All apps accounted for',
    );

    my $app = App::Main->to_app;
    isa_ok( $app, 'CODE' );
    test_psgi $app, sub {
        my $cb = shift;
        is(
            $cb->( GET '/main' )->content,
            1,
            'Got original app response',
        );

        is(
            $cb->( GET '/alsomain' )->content,
            1,
            'Can compose apps with appname',
        );
    };
}

{
    package App::WithSettingsChanged;
    use Dancer2;
}

{
    App::WithSettingsChanged->import( with => { layout => 'mobile' } );

    my ($app) = grep +( $_->name eq 'App::WithSettingsChanged' ),
        @{ Dancer2->runner->{'apps'} };

    ::isa_ok( $app, 'Dancer2::Core::App' );
    ::is(
        $app->template_engine->{'layout'},
        'mobile',
        'Changed settings using with keyword',
    );
}

{
    package App::NoStrictNoWarningsNoUTF8;
    use Dancer2;
    no strict;
    no warnings;
    no utf8;

    local $@ = undef;

    eval '$var = 30';

    ::is(
        $@,
        '',
        'no strict (control test)',
    );

    local $SIG{'__WARN__'} = sub {
        ::is(
            $_[0],
            undef,
            'no warning (control test)',
        );
    };

    eval 'my $var; my $var;';

    my $str = "щука";
    ::isnt( length $str, 4, 'utf8 pragma not imported' );
}

{
    package App::StrictAndWarningsAndUTF8;
    use Dancer2;

    local $@ = undef;

    local $SIG{'__WARN__'} = sub {
        ::ok(
            $_[0],
            'warnings pragma imported',
        );
    };

    eval '$var = 30;';

    ::like(
        $@,
        qr/^Global symbol/,
        'strict pragma imported',
    );

    eval 'my $var; my $var;';

    my $str = "щука";
    ::is( length $str, 4, 'utf8 pragma imported' );
}