File: 05object.t

package info (click to toggle)
libmasonx-interp-withcallbacks-perl 1.20-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,784; makefile: 2
file content (374 lines) | stat: -rw-r--r-- 12,766 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
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!perl -w

use strict;
use FindBin qw($Bin);
use File::Spec::Functions qw(catdir catfile);
use Test::More;
use HTML::Mason::Interp;
my $base_key = 'OOTester';
my $err_msg = "He's dead, Jim";
my $comp = '/dhandler';

##############################################################################
# Figure out if the current configuration can handle OO callbacks.
BEGIN {
    plan skip_all => 'Object-oriented callbacks require Perl 5.6.0 or later'
      if $] < 5.006;

    plan skip_all => 'Attribute::Handlers and Class::ISA required for' .
      ' object-oriented callbacks'
      unless eval { require Attribute::Handlers }
      and eval { require Class::ISA };

    plan tests => 136;
}

##############################################################################
# Set up the callback class.
##############################################################################
package Params::Callback::TestObjects;

use strict;
use base 'Params::Callback';
__PACKAGE__->register_subclass( class_key => $base_key);
use Params::CallbackRequest::Exceptions abbr => [qw(throw_cb_exec)];

sub simple : Callback {
    my $self = shift;
    main::isa_ok($self, 'Params::Callback');
    main::isa_ok($self, __PACKAGE__);
    my $params = $self->params;
    $params->{result} = 'Simple Success';
}

sub complete : Callback(priority => 3) {
    my $self = shift;
    main::isa_ok($self, 'Params::Callback');
    main::isa_ok($self, __PACKAGE__);
    main::is($self->priority, 3, "Check priority is '3'" );
    my $params = $self->params;
    $params->{result} = 'Complete Success';
}

sub inherit : Callback {
    my $self = shift;
    my $params = $self->params;
    $params->{result} = UNIVERSAL::isa($self, 'Params::Callback')
      ? 'Yes' : 'No';
}

sub highest : Callback(priority => 0) {
    my $self = shift;
    main::is( $self->priority, 0, "Check priority is '0'" );
}

sub upperit : PreCallback {
    my $self = shift;
    my $params = $self->params;
    $params->{result} = uc $params->{result} if $params->{do_upper};
}

sub pre_post : Callback {
    my $self = shift;
    my $params = $self->params;
    $params->{chk_post} = 1;
}

sub lowerit : PostCallback {
    my $self = shift;
    my $params = $self->params;
    $params->{result} = lc $params->{result} if $params->{do_lower};
}

sub class : Callback {
    my $self = shift;
    main::isa_ok( $self, __PACKAGE__);
    main::isa_ok( $self, $self->value);
}

sub chk_priority : Callback {
    my $self = shift;
    my $priority = $self->priority;
    my $val = $self->value;
    $val = 5 if $val eq 'def';
    main::is($priority, $val, "Check for priority '$val'" );
    my $params = $self->params;
    $params->{result} .= " " . $priority;
}

sub test_abort : Callback {
    my $self = shift;
    $self->abort(1);
}

sub test_aborted : Callback {
    my $self = shift;
    my $params = $self->params;
    my $val = $self->value;
    eval { $self->abort(1) } if $val;
    $params->{result} = $self->aborted($@) ? 'yes' : 'no';
}

sub exception : Callback {
    my $self = shift;
    if ($self->value) {
        # Throw an exception object.
        throw_cb_exec $err_msg;
    } else {
        # Just die.
        die $err_msg;
    }
}

sub same_object : Callback {
    my $self = shift;
    my $params = $self->params;
    if ($self->value) {
        main::is($self, $params->{obj}, "Check for same object" );
    } else {
        $params->{obj} = $self;
    }
}

sub isa_interp : Callback {
    my $self      = shift;
    main::isa_ok $self->requester, 'MasonX::Interp::WithCallbacks',
        'the requester object';
}

sub change_comp : Callback {
    my $self = shift;
    $self->requester->comp_path($self->value);
}

1;

##############################################################################
# Now set up an emtpy callback subclass.
##############################################################################
package Params::Callback::TestObjects::Empty;
use strict;
use base 'Params::Callback::TestObjects';
__PACKAGE__->register_subclass( class_key => $base_key . 'Empty');
1;

##############################################################################
# Now set up an a subclass that overrides a parent method.
##############################################################################
package Params::Callback::TestObjects::Sub;
use strict;
use base 'Params::Callback::TestObjects';
__PACKAGE__->register_subclass( class_key => $base_key . 'Sub');

# Try a method with the same name as one in the parent, and which
# calls the super method.
sub inherit : Callback {
    my $self = shift;
    $self->SUPER::inherit;
    my $params = $self->params;
    $params->{result} .= ' and ';
    $params->{result} .= UNIVERSAL::isa($self, 'Params::Callback::TestObjects')
      ? 'Yes' : 'No';
}

# Try a totally new method.
sub subsimple : Callback {
    my $self = shift;
    my $params = $self->params;
    $params->{result} = 'Subsimple Success';
}

# Try a totally new method.
sub simple : Callback {
    my $self = shift;
    my $params = $self->params;
    $params->{result} = 'Oversimple Success';
}

1;

##############################################################################
# Meanwhile, back at the ranch...
##############################################################################
package main;

# Keep track of who's who.
my %classes = ( $base_key           => 'Params::Callback::TestObjects',
                $base_key . 'Sub'   => 'Params::Callback::TestObjects::Sub',
                $base_key . 'Empty' => 'Params::Callback::TestObjects::Empty');

my $outbuf;
my %mason_params = (comp_root  => catdir($Bin, qw(htdocs)),
                    out_method => \$outbuf);

use_ok('MasonX::Interp::WithCallbacks');

my $all = 'ALL';
for my $key ($base_key, $base_key . "Empty", $all) {
    # Create the Interp object.
    my $interp;
    if ($key eq 'ALL') {
        # Load all of the callback classes.
        ok( $interp = MasonX::Interp::WithCallbacks->new( %mason_params,
                                                          cb_classes => $key ),
            "Construct $key Interp object" );
        $key = $base_key;
    } else {
        # Load the base class and the subclass.
        ok( $interp = MasonX::Interp::WithCallbacks->new
            ( %mason_params,
              cb_classes => [$key, $base_key . 'Sub']),
            "Construct $key Interp object" );
    }

    ##########################################################################
    # Now make sure that the simple callback executes.
    $interp->exec($comp, "$key|simple_cb" => 1);
    is( $outbuf, 'Simple Success', "Check simple result" );
    $outbuf = '';

    ##########################################################################
    # And the "complete" callback.
    $interp->exec($comp, "$key|complete_cb" => 1);
    is( $outbuf, 'Complete Success', "Check complete result" );
    $outbuf = '';

    ##########################################################################
    # Check the class name.
    $interp->exec($comp, "$key|inherit_cb" => 1);
    is( $outbuf, 'Yes', "Check inherit result" );
    $outbuf = '';

    ##########################################################################
    # Check class inheritance and SUPER method calls.
    $interp->exec($comp, $base_key . "Sub|inherit_cb" => 1);
    is( $outbuf, 'Yes and Yes', "Check SUPER inherit result" );
    $outbuf = '';

    ##########################################################################
    # Try pre-execution callbacks.
    $interp->exec($comp,
                      do_upper => 1,
                      result   => 'upPer_mE');
    is( $outbuf, 'UPPER_ME', "Check pre result" );
    $outbuf = '';

    ##########################################################################
    # Try post-execution callbacks.
    $interp->exec($comp,
                      "$key|simple_cb" => 1,
                      do_lower => 1);
    is( $outbuf, 'simple success', "Check post result" );
    $outbuf = '';

    ##########################################################################
    # Try a method defined only in a subclass.
    $interp->exec($comp, $base_key . "Sub|subsimple_cb" => 1);
    is( $outbuf, 'Subsimple Success', "Check subsimple result" );
    $outbuf = '';

    ##########################################################################
    # Try a method that overrides its parent but doesn't call its parent.
    $interp->exec($comp, $base_key . "Sub|simple_cb" => 1);
    is( $outbuf, 'Oversimple Success', "Check oversimple result" );
    $outbuf = '';

    ##########################################################################
    # Try a method that overrides its parent but doesn't call its parent.
    $interp->exec($comp, $base_key . "Sub|simple_cb" => 1);
    is( $outbuf, 'Oversimple Success', "Check oversimple result" );
    $outbuf = '';

    ##########################################################################
    # Check that the proper class ojbect is constructed.
    $interp->exec($comp, "$key|class_cb" => $classes{$key});
    $outbuf = '';

    ##########################################################################
    # Check priority execution order for multiple callbacks.
    $interp->exec($comp,
                  "$key|chk_priority_cb0"  => 0,
                  "$key|chk_priority_cb2"  => 2,
                  "$key|chk_priority_cb9"  => 9,
                  "$key|chk_priority_cb7"  => 7,
                  "$key|chk_priority_cb1"  => 1,
                  "$key|chk_priority_cb4"  => 4,
                  "$key|chk_priority_cb"   => 'def');
    is($outbuf, " 0 1 2 4 5 7 9", "Check priority order result" );
    $outbuf = '';

    ##########################################################################
    # Emulate the sumission of an <input type="image" /> button.
    $interp->exec($comp,
                  "$key|simple_cb.x" => 18,
                  "$key|simple_cb.y" => 22 );
    is( $outbuf, 'Simple Success', "Check single simple result" );
    $outbuf = '';

    ##########################################################################
    # Make sure that if we abort, no more callbacks execute.
    eval { $interp->exec($comp,
                         "$key|test_abort_cb0" => 1,
                         "$key|simple_cb" => 1,
                         result => 'still here') };
    is( $outbuf, '', "Check abort result" );
    $outbuf = '';

    ##########################################################################
    # Test aborted for a false value.
    $interp->exec($comp, "$key|test_aborted_cb" => 0);
    is( $outbuf, 'no', "Check false aborted result" );
    $outbuf = '';

    ##########################################################################
    # Test aborted for a true value.
    $interp->exec($comp, "$key|test_aborted_cb" => 1);
    is( $outbuf, 'yes', "Check true aborted result" );
    $outbuf = '';

    ##########################################################################
    # Try throwing an execption.
    eval { $interp->exec($comp, "$key|exception_cb" => 1) };
    ok( my $err = $@, "Catch $key exception" );
    isa_ok($err, 'Params::Callback::Exception');
    isa_ok($err, 'Params::Callback::Exception::Execution');
    is( $err->error, $err_msg, "Check error message" );
    $outbuf = '';

    ##########################################################################
    # Try die'ing.
    eval { $interp->exec($comp, "$key|exception_cb" => 0) };
    ok( $err = $@, "Catch $key die" );
    isa_ok($err, 'Params::Callback::Exception');
    isa_ok($err, 'Params::Callback::Exception::Execution');
    like( $err->error, qr/^Error thrown by callback: $err_msg/,
        "Check die error message" );
    $outbuf = '';

    ##########################################################################
    # Make sure that the same object is called for multiple callbacks in the
    # same class.
    $interp->exec($comp,
                  "$key|same_object_cb1" => 0,
                  "$key|same_object_cb" => 1);
    $outbuf = '';

    ##########################################################################
    # Check priority 0 sticks.
    $interp->exec($comp, "$key|highest_cb" => undef);
    $outbuf = '';

    ##########################################################################
    # Requester should be WithCallbacks object.
    $interp->exec($comp, "$key|isa_interp_cb" => 1);
    $outbuf = '';

    ##########################################################################
    # Changing the comp path should change the executed component.
    $interp->exec($comp, "$key|change_comp_cb" => '/alt.mc');
    is $outbuf, 'This is the alt component.',
        'The alt component should have executed';
    $outbuf = '';
}

__END__