File: 05_typelike.t

package info (click to toggle)
libparams-util-perl 1.102-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: perl: 5,398; makefile: 3
file content (127 lines) | stat: -rw-r--r-- 2,369 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use strict;

BEGIN
{
    $|  = 1;
    $^W = 1;
    $ENV{PERL_PARAMS_UTIL_PP} ||= 1;
}

use Test::More tests => 44;
use Scalar::Util 'refaddr';
use File::Spec::Functions ':ALL';
use Params::Util qw{_ARRAYLIKE _HASHLIKE};

# Tests that two objects are the same object
sub addr
{
    my $have = shift;
    my $want = shift;
    is(refaddr($have), refaddr($want), 'Objects are the same object');
}

my $listS = bless \do { my $i }
  => 'Foo::Listy';
my $hashS = bless \do { my $i }
  => 'Foo::Hashy';
my $bothS = bless \do { my $i }
  => 'Foo::Bothy';

my $listH = bless {} => 'Foo::Listy';
my $hashH = bless {} => 'Foo::Hashy';
my $bothH = bless {} => 'Foo::Bothy';

my $listA = bless [] => 'Foo::Listy';
my $hashA = bless [] => 'Foo::Hashy';
my $bothA = bless [] => 'Foo::Bothy';

my @data = (    # A  H
    [undef, 0, 0, 'undef'],
    [
        1000 => 0,
        0, '1000'
    ],
    [
        'Foo' => 0,
        0, '"Foo"'
    ],
    [
        [] => 1,
        0, '[]'
    ],
    [
        {} => 0,
        1, '{}'
    ],
    [
        $listS => 1,
        0, 'scalar-based Foo::Listy'
    ],
    [
        $hashS => 0,
        1, 'scalar-based Foo::Hashy'
    ],
    [
        $bothS => 1,
        1, 'scalar-based Foo::Bothy'
    ],
    [
        $listH => 1,
        1, 'hash-based Foo::Listy'
    ],
    [
        $hashH => 0,
        1, 'hash-based Foo::Hashy'
    ],
    [
        $bothH => 1,
        1, 'hash-based Foo::Bothy'
    ],
    [
        $listA => 1,
        0, 'array-based Foo::Listy'
    ],
    [
        $hashA => 1,
        1, 'array-based Foo::Hashy'
    ],
    [
        $bothA => 1,
        1, 'array-based Foo::Bothy'
    ],
);

for my $t (@data)
{
    is(_ARRAYLIKE($t->[0]) ? 1 : 0, $t->[1], "$t->[3] " . ($t->[1] ? 'is' : "isn't") . ' @ish');
    if (_ARRAYLIKE($t->[0]))
    {
        addr(_ARRAYLIKE($t->[0]), $t->[0]);
    }
    is(_HASHLIKE($t->[0]) ? 1 : 0, $t->[2], "$t->[3] " . ($t->[2] ? 'is' : "isn't") . ' %ish');
    if (_HASHLIKE($t->[0]))
    {
        addr(_HASHLIKE($t->[0]), $t->[0]);
    }
}

package Foo;
# this package is totally unremarkable;

package Foo::Listy;
use overload
  '@{}'    => sub { [] },
  fallback => 1;

package Foo::Hashy;
use overload
  '%{}'    => sub { {} },
  fallback => 1;

package Foo::Bothy;
use overload
  '@{}'    => sub { [] },
  '%{}'    => sub { {} },
  fallback => 1;