File: all.t

package info (click to toggle)
libautobox-perl 2.84-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 528 kB
  • ctags: 816
  • sloc: perl: 1,428; ansic: 143; makefile: 2
file content (686 lines) | stat: -rw-r--r-- 29,270 bytes parent folder | download
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
package SCALAR;

sub test { __PACKAGE__ }

our $VERSION = 0.01;

package ARRAY;

sub test { __PACKAGE__ }

our $VERSION = 0.01;

package HASH;

sub test { __PACKAGE__ }

our $VERSION = 0.01;

package CODE;

sub test { __PACKAGE__ }

our $VERSION = 0.01;

package Test::SCALAR;

sub test { lc shift }

package MyScalar;

sub test { __PACKAGE__ }

package MyDefault;

sub test { __PACKAGE__ }

package MyNamespace::SCALAR;

sub test { __PACKAGE__ }

package MyNamespace::ARRAY;

sub test { __PACKAGE__ }

package MyNamespace::HASH;

sub test { __PACKAGE__ }

package MyNamespace::CODE;

sub test { __PACKAGE__ }

package Test;

sub new { bless {}, ref $_[0] || $_[0]; }
sub test { 'rubicund' }

package main;

use strict;
use warnings;

use Test::More tests => 292;

BEGIN {
    chdir 't' if -d 't';
    unshift @INC, '../lib';
    unshift @INC, '.';
}

$| = 1;

sub add { my ($x, $y) = @_; $x + $y }

my $undef           = undef;
my $int             = 3;
my $float           = 3.1415927;
my $string          = 'Hello, world!';
my $array           = [ 0 .. 9 ];
my $hash            = { 0 .. 9 };
my $code            = \&add;
my $error           = qr{Can't (call|locate object) method "test" (without a|via) package\b};
my $string_error    = qr{Can't locate object method "test" via package "Hello, world!"};
my $unblessed_error = qr{Can't call method "test" on unblessed reference};
my $undef_error     = qr{Can't call method "[^"]+" on an undefined value};

# test no args
{
    use autobox;    # don't use use_ok(...) as this needs to be loaded at compile time

    my $object = Test->new();
    is(ref $object, 'Test', 'no args: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'no args: $object->test() - object not autoboxed');

    is(3->test(),    'SCALAR', 'no args: integer literal');
    is((-3)->test(), 'SCALAR', 'no args: negative integer literal');
    is((+3)->test(), 'SCALAR', 'no args: positive integer literal');
    is($int->test(), 'SCALAR', 'no args: $integer');

    is(3.1415927->test(),    'SCALAR', 'no args: float literal');
    is((-3.1415927)->test(), 'SCALAR', 'no args: negative float literal');
    is((+3.1415927)->test(), 'SCALAR', 'no args: positive float literal');
    is($float->test(),       'SCALAR', 'no args: $float');

    is('Hello, world'->test(), 'SCALAR', 'no args: single quoted string literal');
    is("Hello, world"->test(), 'SCALAR', 'no args: double quoted string literal');
    is($string->test(),        'SCALAR', 'no args: $string');

    is([ 0 .. 9 ]->test(), 'ARRAY', 'no args: ARRAY ref');
    is($array->test(),     'ARRAY', 'no args: $array');

    is({ 0 .. 9 }->test(), 'HASH', 'no args: HASH ref');
    is($hash->test(),      'HASH', 'no args: $hash');

    is((\&add)->test(), 'CODE', 'no args: CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'CODE', 'no args: ANON sub');
    is($code->test(), 'CODE', 'no args: $code');
}

# test override package
{
    use autobox SCALAR => 'MyScalar';

    my $object = Test->new();
    is(ref $object, 'Test', 'override package: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'override package: $object->test() - object not autoboxed');

    is(3->test(),    'MyScalar', 'override package: integer literal');
    is((-3)->test(), 'MyScalar', 'override package: negative integer literal');
    is((+3)->test(), 'MyScalar', 'override package: positive integer literal');
    is($int->test(), 'MyScalar', 'override package: $integer');

    is(3.1415927->test(),    'MyScalar', 'override package: float literal');
    is((-3.1415927)->test(), 'MyScalar', 'override package: negative float literal');
    is((+3.1415927)->test(), 'MyScalar', 'override package: positive float literal');
    is($float->test(),       'MyScalar', 'override package: $float');

    is('Hello, world'->test(), 'MyScalar', 'override package: single quoted string literal');
    is("Hello, world"->test(), 'MyScalar', 'override package: double quoted string literal');
    is($string->test(),        'MyScalar', 'override package: $string');
}

# test override namespace
{
    use autobox SCALAR => 'MyNamespace::';

    my $object = Test->new();
    is(ref $object, 'Test', 'override namespace: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'override namespace: $object->test() - object not autoboxed');

    is(3->test(),    'MyNamespace::SCALAR', 'override namespace: integer literal');
    is((-3)->test(), 'MyNamespace::SCALAR', 'override namespace: negative integer literal');
    is((+3)->test(), 'MyNamespace::SCALAR', 'override namespace: positive integer literal');
    is($int->test(), 'MyNamespace::SCALAR', 'override namespace: $int');

    is(3.1415927->test(),    'MyNamespace::SCALAR', 'override namespace: float literal');
    is((-3.1415927)->test(), 'MyNamespace::SCALAR', 'override namespace: negative float literal');
    is((+3.1415927)->test(), 'MyNamespace::SCALAR', 'override namespace: positive float literal');
    is($float->test(),       'MyNamespace::SCALAR', 'override namespace: $float');

    is('Hello, world'->test(), 'MyNamespace::SCALAR', 'override namespace: single quoted string literal');
    is("Hello, world"->test(), 'MyNamespace::SCALAR', 'override namespace: double quoted string literal');
    is($string->test(),        'MyNamespace::SCALAR', 'override namespace: $string');
}

# test default package
{
    use autobox DEFAULT => 'MyDefault';

    my $object = Test->new();
    is(ref $object, 'Test', 'default package: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'default package: $object->test() - object not autoboxed');

    is(3->test(),    'MyDefault', 'default package: integer literal');
    is((-3)->test(), 'MyDefault', 'default package: negative integer literal');
    is((+3)->test(), 'MyDefault', 'default package: positive integer literal');
    is($int->test(), 'MyDefault', 'default package: $int');

    is(3.1415927->test(),    'MyDefault', 'default package: float literal');
    is((-3.1415927)->test(), 'MyDefault', 'default package: negative float literal');
    is((+3.1415927)->test(), 'MyDefault', 'default package: positive float literal');
    is($float->test(),       'MyDefault', 'default package: $float');

    is('Hello, world'->test(), 'MyDefault', 'default package: single quoted string literal');
    is("Hello, world"->test(), 'MyDefault', 'default package: double quoted string literal');
    is($string->test(),        'MyDefault', 'default package: $string');

    is([ 0 .. 9 ]->test(), 'MyDefault', 'default package: ARRAY ref');
    is($array->test(),     'MyDefault', 'default package: $array');

    is({ 0 .. 9 }->test(), 'MyDefault', 'default package: HASH ref');
    is($hash->test(),      'MyDefault', 'default package: $hash');

    is((\&add)->test(), 'MyDefault', 'default package: CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'MyDefault', 'default package: ANON sub');
    is($code->test(), 'MyDefault', 'default package: $code');
}

# test default namespace
{
    use autobox DEFAULT => 'MyNamespace::';

    my $object = Test->new();
    is(ref $object, 'Test', 'default namespace: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'default namespace: $object->test() - object not autoboxed');

    is(3->test(),    'MyNamespace::SCALAR', 'default namespace: integer literal');
    is((-3)->test(), 'MyNamespace::SCALAR', 'default namespace: negative integer literal');
    is((+3)->test(), 'MyNamespace::SCALAR', 'default namespace: positive integer literal');
    is($int->test(), 'MyNamespace::SCALAR', 'default namespace: $int');

    is(3.1415927->test(),    'MyNamespace::SCALAR', 'default namespace: float literal');
    is((-3.1415927)->test(), 'MyNamespace::SCALAR', 'default namespace: negative float literal');
    is((+3.1415927)->test(), 'MyNamespace::SCALAR', 'default namespace: positive float literal');
    is($float->test(),       'MyNamespace::SCALAR', 'default namespace: $float');

    is('Hello, world'->test(), 'MyNamespace::SCALAR', 'default namespace: single quoted string literal');
    is("Hello, world"->test(), 'MyNamespace::SCALAR', 'default namespace: double quoted string literal');
    is($string->test(),        'MyNamespace::SCALAR', 'default namespace: $string');

    is([ 0 .. 9 ]->test(), 'MyNamespace::ARRAY', 'default namespace: ARRAY ref');
    is($array->test(),     'MyNamespace::ARRAY', 'default namespace: $array');

    is({ 0 .. 9 }->test(), 'MyNamespace::HASH', 'default namespace: HASH ref');
    is($hash->test(),      'MyNamespace::HASH', 'default namespace: $hash');

    is((\&add)->test(), 'MyNamespace::CODE', 'default namespace: CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'MyNamespace::CODE', 'default namespace: ANON sub');
    is($code->test(), 'MyNamespace::CODE', 'default namespace: $code');
}

# test default undef
{
    use autobox DEFAULT => undef;

    eval { $int->test() };
    ok(($@ && ($@ =~ /^$error/)), 'default undef: $int');

    eval { $float->test() };
    ok(($@ && ($@ =~ /^$error/)), 'default undef: $float');

    eval { $string->test() };
    ok(($@ && ($@ =~ /^$string_error/)), 'default undef: $string');

    eval { $array->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'default undef: $array');

    eval { $hash->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'default undef: $hash');

    eval { $code->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'default undef: $code');
}

# test all 1
{
    use autobox
      SCALAR  => 'MyScalar',         # specific package
      HASH    => undef,              # don't autobox
      DEFAULT => 'MyNamespace::';    # use MyNamespace:: namespace for CODE

    my $object = Test->new();
    is(ref $object, 'Test', 'test all 1: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'test all 1: $object->test() - object not autoboxed');

    is(3->test(),    'MyScalar', 'test all 1: integer literal');
    is((-3)->test(), 'MyScalar', 'test all 1: negative integer literal');
    is((+3)->test(), 'MyScalar', 'test all 1: positive integer literal');
    is($int->test(), 'MyScalar', 'test all 1: $int');

    is(3.1415927->test(),    'MyScalar', 'test all 1: float literal');
    is((-3.1415927)->test(), 'MyScalar', 'test all 1: negative float literal');
    is((+3.1415927)->test(), 'MyScalar', 'test all 1: positive float literal');
    is($float->test(),       'MyScalar', 'test all 1: $float');

    is('Hello, world'->test(), 'MyScalar', 'test all 1: single quoted string literal');
    is("Hello, world"->test(), 'MyScalar', 'test all 1: double quoted string literal');
    is($string->test(),        'MyScalar', 'test all 1: $string');

    eval { ({ 0 .. 9 })->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 1: HASH ref: not autoboxed');

    is((\&add)->test(), 'MyNamespace::CODE', 'test all 1: CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'MyNamespace::CODE', 'test all 1: ANON sub');
    is($code->test(), 'MyNamespace::CODE', 'test all 1: $code');
}

# test all 2
{
    use autobox
      SCALAR  => 'MyScalar',     # specific package
      HASH    => undef,          # don't autobox
      DEFAULT => 'MyDefault';    # use MyDefault package for CODE

    my $object = Test->new();
    is(ref $object, 'Test', 'test all 2: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'test all 2: $object->test() - object not autoboxed');

    is(3->test(),    'MyScalar', 'test all 2: integer literal');
    is((-3)->test(), 'MyScalar', 'test all 2: negative integer literal');
    is((+3)->test(), 'MyScalar', 'test all 2: positive integer literal');
    is($int->test(), 'MyScalar', 'test all 2: $int');

    is(3.1415927->test(),    'MyScalar', 'test all 2: float literal');
    is((-3.1415927)->test(), 'MyScalar', 'test all 2: negative float literal');
    is((+3.1415927)->test(), 'MyScalar', 'test all 2: positive float literal');
    is($float->test(),       'MyScalar', 'test all 2: $float');

    is('Hello, world'->test(), 'MyScalar', 'test all 2: single quoted string literal');
    is("Hello, world"->test(), 'MyScalar', 'test all 2: double quoted string literal');
    is($string->test(),        'MyScalar', 'test all 2: $string');

    eval { ({ 0 .. 9 })->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 2: HASH ref: not autoboxed');

    is((\&add)->test(), 'MyDefault', 'test all 2: CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'MyDefault', 'test all 2: ANON sub');
    is($code->test(), 'MyDefault', 'test all 2: $code');
}

# test all 3
{
    use autobox
      SCALAR  => 'MyScalar',    # specific package
      HASH    => undef,         # don't autobox
      DEFAULT => 'CODE';        # use CODE package for CODE

    my $object = Test->new();
    is(ref $object, 'Test', 'test all 3: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'test all 3: $object->test() - object not autoboxed');

    is(3->test(),    'MyScalar', 'test all 3: integer literal');
    is((-3)->test(), 'MyScalar', 'test all 3: negative integer literal');
    is((+3)->test(), 'MyScalar', 'test all 3: positive integer literal');
    is($int->test(), 'MyScalar', 'test all 3: $int');

    is(3.1415927->test(),    'MyScalar', 'test all 3: float literal');
    is((-3.1415927)->test(), 'MyScalar', 'test all 3: negative float literal');
    is((+3.1415927)->test(), 'MyScalar', 'test all 3: positive float literal');
    is($float->test(),       'MyScalar', 'test all 3: $float');

    is('Hello, world'->test(), 'MyScalar', 'test all 3: single quoted string literal');
    is("Hello, world"->test(), 'MyScalar', 'test all 3: double quoted string literal');
    is($string->test(),        'MyScalar', 'test all 3: $string');

    eval { ({ 0 .. 9 })->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 3: HASH ref: not autoboxed');

    is((\&add)->test(), 'CODE', 'test all 3: CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'CODE', 'test all 3: ANON sub');
    is($code->test(), 'CODE', 'test all 3: $code');
}

# test all 4
{
    use autobox
      SCALAR  => 'MyScalar',    # specific package
      DEFAULT => undef;         # don't autobox ARRAY, HASH or CODE

    my $object = Test->new();
    is(ref $object, 'Test', 'test all 4: Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'test all 4: $object->test() - object not autoboxed');

    is(3->test(),    'MyScalar', 'test all 4: integer literal');
    is((-3)->test(), 'MyScalar', 'test all 4: negative integer literal');
    is((+3)->test(), 'MyScalar', 'test all 4: positive integer literal');
    is($int->test(), 'MyScalar', 'test all 4: $int');

    is(3.1415927->test(),    'MyScalar', 'test all 4: float literal');
    is((-3.1415927)->test(), 'MyScalar', 'test all 4: negative float literal');
    is((+3.1415927)->test(), 'MyScalar', 'test all 4: positive float literal');
    is($float->test(),       'MyScalar', 'test all 4: $float');

    is('Hello, world'->test(), 'MyScalar', 'test all 4: single quoted string literal');
    is("Hello, world"->test(), 'MyScalar', 'test all 4: double quoted string literal');
    is($string->test(),        'MyScalar', 'test all 4: $string');

    eval { ({ 0 .. 9 })->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 4: HASH ref: not autoboxed');

    eval { (\&add)->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 4: CODE ref: not autoboxed');

    eval { sub { $_[0] + $_[1] }->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 4: ANON sub: not autoboxed');

    eval { $code->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'test all 4: $code: not autoboxed');
}

# test autobox not used
{
    eval { $int->test() };
    ok(($@ && ($@ =~ /^$error/)), 'autobox not used: $int');

    eval { $float->test() };
    ok(($@ && ($@ =~ /^$error/)), 'autobox not used: $float');

    eval { $string->test() };
    ok(($@ && ($@ =~ /^$string_error/)), 'autobox not used: $string');

    eval { $array->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'autobox not used: $array');

    eval { $hash->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'autobox not used: $hash');

    eval { $code->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'autobox not used: $code');
}

# test no autobox
{
    use autobox;

    no autobox;

    eval { $int->test() };
    ok(($@ && ($@ =~ /^$error/)), 'no autobox: $int');

    eval { $float->test() };
    ok(($@ && ($@ =~ /^$error/)), 'no autobox: $float');

    eval { $string->test() };
    ok(($@ && ($@ =~ /^$string_error/)), 'no autobox: $string');

    eval { $array->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'no autobox: $array');

    eval { $hash->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'no autobox: $hash');

    eval { $code->test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), 'no autobox: $code');
}

# test nested
{
    use autobox;

    my $object = Test->new();
    is(ref $object, 'Test', 'nested (outer): Test->new() - bareword not autoboxed');

    my $result = $object->test();
    is($result, 'rubicund', 'nested (outer): $object->test() - object not autoboxed');

    is(3->test(),    'SCALAR', 'nested (outer): integer literal');
    is((-3)->test(), 'SCALAR', 'nested (outer): negative integer literal');
    is((+3)->test(), 'SCALAR', 'nested (outer): positive integer literal');
    is($int->test(), 'SCALAR', 'nested (outer): $integer');

    {
        use autobox DEFAULT => 'MyNamespace::';

        is('Hello, world'->test(), 'SCALAR', 'nested (inner): single quoted string literal');
        is("Hello, world"->test(), 'SCALAR', 'nested (inner): double quoted string literal');
        is($string->test(),        'SCALAR', 'nested (inner): $string');
        is([ 0 .. 9 ]->test(),     'ARRAY',  'nested (inner): ARRAY ref');
        is($array->test(),         'ARRAY',  'nested (inner): $array');

        ok('Hello, world'->autobox_class->isa('SCALAR'), 'nested (inner): single quoted string literal isa(OUTER)');
        ok("Hello, world"->autobox_class->isa('SCALAR'), 'nested (inner): double quoted string literal isa(OUTER)');
        ok($string->autobox_class->isa('SCALAR'),        'nested (inner): $string isa(OUTER)');
        ok([ 0 .. 9 ]->autobox_class->isa('ARRAY'),      'nested (inner): ARRAY ref isa(OUTER)');
        ok($array->autobox_class->isa('ARRAY'),          'nested (inner): $array isa(OUTER)');

        ok('Hello, world'->autobox_class->isa('MyNamespace::SCALAR'), 'nested (inner): single quoted string literal isa(INNER)');
        ok("Hello, world"->autobox_class->isa('MyNamespace::SCALAR'), 'nested (inner): double quoted string literal isa(INNER)');
        ok($string->autobox_class->isa('MyNamespace::SCALAR'),        'nested (inner): $string isa(INNER)');
        ok([ 0 .. 9 ]->autobox_class->isa('MyNamespace::ARRAY'),      'nested (inner): ARRAY ref isa(INNER)');
        ok($array->autobox_class->isa('MyNamespace::ARRAY'),          'nested (inner): $array isa(INNER)');
    }

    is('Hello, world'->autobox_class->isa('MyNamespace::SCALAR'), '', 'nested (outer): single quoted string literal !isa(INNER)');
    is("Hello, world"->autobox_class->isa('MyNamespace::SCALAR'), '', 'nested (outer): double quoted string literal !isa(INNER)');
    is($string->autobox_class->isa('MyNamespace::SCALAR'),        '', 'nested (outer): $string !isa(INNER)');
    is([ 0 .. 9 ]->autobox_class->isa('MyNamespace::ARRAY'),      '', 'nested (outer): ARRAY ref !isa(INNER)');
    is($array->autobox_class->isa('MyNamespace::ARRAY'),          '', 'nested (outer): $array !isa(INNER)');

    is({ 0 .. 9 }->test(), 'HASH', 'nested (outer): HASH ref');
    is($hash->test(),      'HASH', 'nested (outer): $hash');

    is((\&add)->test(), 'CODE', 'nested (outer): CODE ref');
    is(sub { $_[0] + $_[1] }->test(), 'CODE', 'nested (outer): ANON sub');
    is($code->test(), 'CODE', 'nested (outer): $code');
}

# test can
{
    use autobox;

    is(3->autobox_class->can('test'),    \&SCALAR::test, 'can: integer literal');
    is((-3)->autobox_class->can('test'), \&SCALAR::test, 'can: negative integer literal');
    is((+3)->autobox_class->can('test'), \&SCALAR::test, 'can: positive integer literal');
    is($int->autobox_class->can('test'), \&SCALAR::test, 'can: $integer');

    is(3.1415927->autobox_class->can('test'),    \&SCALAR::test, 'can: float literal');
    is((-3.1415927)->autobox_class->can('test'), \&SCALAR::test, 'can: negative float literal');
    is((+3.1415927)->autobox_class->can('test'), \&SCALAR::test, 'can: positive float literal');
    is($float->autobox_class->can('test'),       \&SCALAR::test, 'can: $float');

    is('Hello, world'->autobox_class->can('test'), \&SCALAR::test, 'can: single quoted string literal');
    is("Hello, world"->autobox_class->can('test'), \&SCALAR::test, 'can: double quoted string literal');
    is($string->autobox_class->can('test'),        \&SCALAR::test, 'can: $string');

    is([ 0 .. 9 ]->autobox_class->can('test'), \&ARRAY::test, 'can: ARRAY ref');
    is($array->autobox_class->can('test'),     \&ARRAY::test, 'can: $array');

    is({ 0 .. 9 }->autobox_class->can('test'), \&HASH::test, 'can: HASH ref');
    is($hash->autobox_class->can('test'),      \&HASH::test, 'can: $hash');

    is((\&add)->autobox_class->can('test'), \&CODE::test, 'can: CODE ref');
    is(sub { $_[0] + $_[1] }->autobox_class->can('test'), \&CODE::test, 'can: ANON sub');
    is($code->autobox_class->can('test'), \&CODE::test, 'can: $code');
}

# test isa: also ensures isa() isn't simply
# returning true unconditionally
{
    use autobox;

    ok(3->autobox_class->isa('SCALAR'),    'isa SCALAR: integer literal');
    ok(3->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: integer literal');
    is(3->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: integer literal');

    ok((-3)->autobox_class->isa('SCALAR'),    'isa SCALAR: negative integer literal');
    ok((-3)->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: negative integer literal');
    is((-3)->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: negative integer literal');

    ok((+3)->autobox_class->isa('SCALAR'),    'isa SCALAR: positive integer literal');
    ok((+3)->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: positive integer literal');
    is((+3)->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: positive integer literal');

    ok($int->autobox_class->isa('SCALAR'),    'isa SCALAR: $integer');
    ok($int->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: $integer');
    is($int->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: $integer');

    ok(3.1415927->autobox_class->isa('SCALAR'),    'isa SCALAR: float literal');
    ok(3.1415927->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: float literal');
    is(3.1415927->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: float literal');

    ok((-3.1415927)->autobox_class->isa('SCALAR'),    'isa SCALAR: negative float literal');
    ok((-3.1415927)->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: negative float literal');
    is((-3.1415927)->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: negative float literal');

    ok((+3.1415927)->autobox_class->isa('SCALAR'),    'isa SCALAR: positive float literal');
    ok((+3.1415927)->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: positive float literal');
    is((+3.1415927)->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: positive float literal');

    ok($float->autobox_class->isa('SCALAR'),    'isa SCALAR: $float');
    ok($float->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: $float');
    is($float->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: $float');

    ok('Hello, world'->autobox_class->isa('SCALAR'),    'isa SCALAR: single quoted string literal');
    ok('Hello, world'->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: single quoted string literal');
    is('Hello, world'->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: single quoted string literal');

    ok("Hello, world"->autobox_class->isa('SCALAR'),    'isa SCALAR: double quoted string literal');
    ok("Hello, world"->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: double quoted string literal');
    is("Hello, world"->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: double quoted string literal');

    ok($string->autobox_class->isa('SCALAR'),    'isa SCALAR: $string');
    ok($string->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: $string');
    is($string->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: $string');

    ok([ 0 .. 9 ]->autobox_class->isa('ARRAY'),     'isa ARRAY: ARRAY ref');
    ok([ 0 .. 9 ]->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: ARRAY ref');
    is([ 0 .. 9 ]->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: ARRAY ref');

    ok($array->autobox_class->isa('ARRAY'),     'isa ARRAY: $array');
    ok($array->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: $array');
    is($array->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: $array');

    ok({ 0 .. 9 }->autobox_class->isa('HASH'),      'isa HASH: HASH ref');
    ok({ 0 .. 9 }->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: HASH ref');
    is({ 0 .. 9 }->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: HASH ref');

    ok($hash->autobox_class->isa('HASH'),      'isa HASH: $hash');
    ok($hash->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: $hash');
    is($hash->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: $hash');

    ok((\&add)->autobox_class->isa('CODE'),      'isa CODE: CODE ref');
    ok((\&add)->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: CODE ref');
    is((\&add)->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: CODE ref');

    ok(sub { $_[0] + $_[1] }->autobox_class->isa('CODE'), 'isa CODE: ANON sub');
    ok(sub { $_[0] + $_[1] }->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: ANON sub');
    is(sub { $_[0] + $_[1] }->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: ANON sub');

    ok($code->autobox_class->isa('CODE'),      'isa CODE: $code');
    ok($code->autobox_class->isa('UNIVERSAL'), 'isa UNIVERSAL: $code');
    is($code->autobox_class->isa('UNKNOWN'), '', 'isa UNKNOWN: $code');
}

# test undef: by default, undef shouldn't be autoboxed...
{
    use autobox;

    eval { undef->test() };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef: undef->test()');

    eval { $undef->test() };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef: $undef->test()');

    eval { undef->autobox_class->isa('SCALAR') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef: undef->autobox_class->isa(...)');

    eval { $undef->autobox_class->isa('SCALAR') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef: $undef->autobox_class->isa(...)');

    eval { undef->autobox_class->can('test') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef: undef->autobox_class->can(...)');

    eval { $undef->autobox_class->can('test') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef: $undef->autobox_class->can(...)');
}

# test undef 2: not even if DEFAULT is specified
{
    use autobox DEFAULT => 'SCALAR';

    eval { undef->test() };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef with DEFAULT: undef->test()');

    eval { $undef->test() };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef with DEFAULT: $undef->test()');

    eval { undef->autobox_class->isa('SCALAR') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef with DEFAULT: undef->autobox_class->isa(...)');

    eval { $undef->autobox_class->isa('SCALAR') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef with DEFAULT: $undef->autobox_class->isa(...)');

    eval { undef->autobox_class->can('test') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef with DEFAULT: undef->autobox_class->can(...)');

    eval { $undef->autobox_class->can('test') };
    ok(($@ && ($@ =~ /^$undef_error/)), 'undef with DEFAULT: $undef->autobox_class->can(...)');
}

# test undef: but undef support can be enabled
{
    use autobox UNDEF => 'MyDefault';
    is(undef->test(),  'MyDefault', 'handle undef: undef');
    is($undef->test(), 'MyDefault', 'handle undef: $undef');
}

# verify expected behaviour when autobox isn't enabled (workaround for a %^H bug)
{
    use autobox_scope_1;
    eval { autobox_scope_1::test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), '[]->test() raises an exception in a required module when autobox is not used');
}

# make sure behaviour is the same when autobox is enabled (workaround for a %^H bug)
{
    use autobox;
    use autobox_scope_2;
    eval { autobox_scope_2::test() };
    ok(($@ && ($@ =~ /^$unblessed_error/)), '[]->test() raises an exception in a required module when autobox is used');
}

# make sure enterscope/leavescope have popped the stack back so that the default op_method_named is restored
(my $isa_error = $error) =~ s/test/autobox_class/;
eval { 3->autobox_class->isa('SCALAR') };
ok(($@ && ($@ =~ /^$isa_error/)), "3->autobox_class->isa('SCALAR') raises an exception outside a lexical scope in which autobox is loaded");

1;