File: 13-tt_obj_caching.t

package info (click to toggle)
libcgi-application-plugin-anytemplate-perl 0.18-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 776 kB
  • ctags: 120
  • sloc: perl: 1,073; sh: 8; makefile: 2
file content (319 lines) | stat: -rwxr-xr-x 9,759 bytes parent folder | download | duplicates (4)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319

use strict;
use Test::More 'no_plan';

my $Per_Template_Driver_Tests = 2;

my %Objects;

{
    package MyProject;
    use CGI::Application;

    use vars '@ISA';
    @ISA = ('CGI::Application');

    sub setup {
        my $self = shift;
        $self->header_type('none');
        $self->start_mode('simple');
        $self->run_modes([qw/simple/]);

        $self->template('project')->config(      # defaults to storage in MyProject
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );

        $self->template('cgiapp')->config(
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
            TemplateToolkit => {
                storage_class => 'CGI::Application',
            },
        );
        $self->template('none')->config(
            type           => 'TemplateToolkit',
            TemplateToolkit => {
                object_caching => 0,
            },
            include_paths  => 't/tmpl',
        );
    }
}
{
    package OtherProject;
    use CGI::Application;

    use vars '@ISA';
    @ISA = ('CGI::Application');

    sub setup {
        my $self = shift;
        $self->header_type('none');
        $self->start_mode('simple');
        $self->run_modes([qw/simple/]);

        $self->template('project')->config(      # defaults to storage in MyProject
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );
        $self->template('cgiapp')->config(
            type          => 'TemplateToolkit',
            TemplateToolkit => {
                storage_class => 'CGI::Application',
            },
            include_paths => 't/tmpl',
        );
        $self->template('none')->config(
            type           => 'TemplateToolkit',
            TemplateToolkit => {
                object_caching => 0,
            },
            include_paths  => 't/tmpl',
        );
    }
}

{
    package WebApp1;

    use vars '@ISA';
    @ISA = ('MyProject');

    use Test::More;
    use CGI::Application::Plugin::AnyTemplate;

    sub setup {
        my $self = shift;
        $self->SUPER::setup(@_);
        $self->template('app')->config(           # defaults to storage in WebApp1
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );

        $self->template('alpha')->config(         # defaults to storage in WebApp1
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );
        $self->template('beta')->config(          # defaults to storage in WebApp1
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );

    }

    sub simple {
        my $self = shift;

        my ($template, $obj1, $obj2);

        $template = $self->template('app')->load;
        $Objects{WebApp1}{'app_obj'} = $template->object;

        $template = $self->template('project')->load;
        $Objects{WebApp1}{'project_obj'} = $template->object;

        $template = $self->template('cgiapp')->load;
        $Objects{WebApp1}{'cgiapp_obj'} = $template->object;

        $template = $self->template('none')->load;
        $Objects{WebApp1}{'none1_obj'} = $template->object;

        $template = $self->template('none')->load;
        $Objects{WebApp1}{'none2_obj'} = $template->object;

        $template = $self->template('alpha')->load;
        $Objects{WebApp1}{'alpha'} = $template->object;

        $template = $self->template('beta')->load;
        $Objects{WebApp1}{'beta'} = $template->object;

        '';
    }
}

{
    package WebApp2;

    use vars '@ISA';
    @ISA = ('MyProject');

    use Test::More;
    use CGI::Application::Plugin::AnyTemplate;

    sub setup {                                  # defaults to storage in WebApp2
        my $self = shift;
        $self->SUPER::setup(@_);
        $self->template('app')->config(
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );
    }
    sub simple {
        my $self = shift;

        my ($template, $obj1, $obj2);

        $template = $self->template('app')->load;
        $Objects{WebApp2}{'app_obj'} = $template->object;

        $template = $self->template('project')->load;
        $Objects{WebApp2}{'project_obj'} = $template->object;

        $template = $self->template('cgiapp')->load;
        $Objects{WebApp2}{'cgiapp_obj'} = $template->object;

        $template = $self->template('none')->load;
        $Objects{WebApp2}{'none1_obj'} = $template->object;

        $template = $self->template('none')->load;
        $Objects{WebApp2}{'none2_obj'} = $template->object;

        '';
    }
}

{
    package OthApp;

    use vars '@ISA';
    @ISA = ('OtherProject');

    use Test::More;
    use CGI::Application::Plugin::AnyTemplate;

    sub setup {
        my $self = shift;
        $self->header_type('none');
        $self->start_mode('simple');
        $self->run_modes([qw/simple/]);

        $self->template('app')->config(          # defaults to storage in OthApp
            type          => 'TemplateToolkit',
            include_paths => 't/tmpl',
        );
        $self->template('project')->config(
            type          => 'TemplateToolkit',
            TemplateToolkit => {
                storage_class => 'OtherProject',
            },
            include_paths => 't/tmpl',
        );
        $self->template('cgiapp')->config(
            type          => 'TemplateToolkit',
            TemplateToolkit => {
                storage_class => 'CGI::Application',
            },
            include_paths => 't/tmpl',
        );
        $self->template('none')->config(
            type           => 'TemplateToolkit',
            TemplateToolkit => {
                object_caching => 0,
            },
            include_paths  => 't/tmpl',
        );
    }

    sub simple {
        my $self = shift;

        my ($template, $obj1, $obj2);

        $template = $self->template('app')->load;
        $Objects{OthApp}{'app_obj'} = $template->object;

        $template = $self->template('project')->load;
        $Objects{OthApp}{'project_obj'} = $template->object;

        $template = $self->template('cgiapp')->load;
        $Objects{OthApp}{'cgiapp_obj'} = $template->object;

        $template = $self->template('none')->load;
        $Objects{OthApp}{'none1_obj'} = $template->object;

        $template = $self->template('none')->load;
        $Objects{OthApp}{'none2_obj'} = $template->object;

        '';
    }
}


# Currently, you are not prevented from storing in a class you aren't a descendent of.
# Is this a bad idea?
# {
#     package BadApp;
#     use base 'OtherProject';
#     use Test::More;
#     use CGI::Application::Plugin::AnyTemplate;
#
#     sub setup {
#         my $self = shift;
#         $self->header_type('none');
#         $self->start_mode('simple');
#         $self->run_modes([qw/simple/]);
#
#         $self->template('project')->config(
#             type          => 'TemplateToolkit',
#             TemplateToolkit => {
#                 storage_class => 'BadBadBad',
#             },
#             include_paths => 't/tmpl',
#         );
#     }
# }

SKIP: {
    if (test_driver_prereqs('TemplateToolkit')) {
        WebApp1->new->run;
        WebApp2->new->run;
        OthApp->new->run;

        # When app is used for storage, all objects should be different
        isnt($Objects{'WebApp1'}{'app_obj'}, $Objects{'WebApp2'}{'app_obj'}, "[app obj] WebApp1 != WebApp2");
        isnt($Objects{'OthApp'}{'app_obj'},  $Objects{'WebApp1'}{'app_obj'}, "[app obj] OthApp  != WebApp1");

        # When project is used for storage, objects for classes derived from MyProject should be identical
        is($Objects{'WebApp1'}{'project_obj'},  $Objects{'WebApp2'}{'project_obj'}, "[project obj] WebApp1 == WebApp2");
        isnt($Objects{'OthApp'}{'project_obj'}, $Objects{'WebApp1'}{'project_obj'}, "[project obj] OthApp  != WebApp1");

        # When cgiapp is used for storage, all objects should be identical
        is($Objects{'WebApp1'}{'cgiapp_obj'},  $Objects{'WebApp2'}{'cgiapp_obj'}, "[cgiapp obj] WebApp1 == WebApp2");
        is($Objects{'OthApp'}{'cgiapp_obj'},   $Objects{'WebApp1'}{'cgiapp_obj'}, "[cgiapp obj] OthApp  == WebApp1");

        # When no storage is used, all objects should be different, even those loaded by the same app
        isnt($Objects{'WebApp1'}{'none1_obj'},  $Objects{'WebApp1'}{'none2_obj'}, "[none obj] WebApp1 (none1) != WebApp1 (none2)");
        isnt($Objects{'WebApp1'}{'none1_obj'},  $Objects{'WebApp2'}{'none1_obj'}, "[none obj] WebApp1 (none1) != WebApp2 (none1)");
        isnt($Objects{'WebApp2'}{'none1_obj'},  $Objects{'WebApp2'}{'none2_obj'}, "[none obj] WebApp2 (none1) != WebApp2 (none2)");
        isnt($Objects{'OthApp'}{'none1_obj'},   $Objects{'WebApp2'}{'none2_obj'}, "[none obj] OthApp (none1)  != WebApp2 (none2)");
        isnt($Objects{'OthApp'}{'none1_obj'},   $Objects{'OthApp'}{'none2_obj'},  "[none obj] OthApp (none1)  != OthApp (none2)");

        # Two template objects using the same storage class, but different
        # names should be different objects
        isnt($Objects{'WebApp1'}{'alpha'},  $Objects{'WebApp1'}{'beta'}, "WebApp1 (alpha) != WebApp1 (beta)");


    }
    else {
        skip "Template::Toolkit not installed", 11;
    }
}

sub test_driver_prereqs {
    my $driver = shift;
    my $driver_module = 'CGI::Application::Plugin::AnyTemplate::Driver::' . $driver;
    eval "require $driver_module;";
    die $@ if $@;

    my @required_modules = $driver_module->required_modules;

    foreach (@required_modules) {
        eval "require $_;";
        if ($@) {
            return;
        }
    }
    return 1;

}