File: blessed.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (56 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download | duplicates (6)
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
#!./perl

use strict;
use warnings;

use Test::More tests => 12;
use Scalar::Util qw(blessed);

my $t;

ok(!defined blessed(undef), 'undef is not blessed');
ok(!defined blessed(1),     'Numbers are not blessed');
ok(!defined blessed('A'),   'Strings are not blessed');
ok(!defined blessed({}),    'Unblessed HASH-ref');
ok(!defined blessed([]),    'Unblessed ARRAY-ref');
ok(!defined blessed(\$t),   'Unblessed SCALAR-ref');

my $x;

$x = bless [], "ABC";
is(blessed($x), "ABC", 'blessed ARRAY-ref');

$x = bless {}, "DEF";
is(blessed($x), "DEF", 'blessed HASH-ref');

$x = bless {}, "0";
cmp_ok(blessed($x), "eq", "0", 'blessed HASH-ref');

{
  my $blessed = do {
    my $depth;
    no warnings 'redefine';
    local *UNIVERSAL::can = sub { die "Burp!" if ++$depth > 2; blessed(shift) };
    $x = bless {}, "DEF";
    blessed($x);
  };
  is($blessed, "DEF", 'recursion of UNIVERSAL::can');
}

{
  package Broken;
  sub isa { die };
  sub can { die };

  my $obj = bless [], __PACKAGE__;
  ::is( ::blessed($obj), __PACKAGE__, "blessed on broken isa() and can()" );
}

SKIP: {
  # Unicode package names only supported in perl 5.16 onwards
  skip "Unicode package names are not supported", 1 if $] < 5.016;

  my $utf8_pack= "X\x{100}";
  my $obj= bless {}, $utf8_pack;
  ::is( ::blessed($obj), $utf8_pack, "blessed preserves utf8ness for utf8 class names" );
}