File: 05_typelike.t

package info (click to toggle)
libparams-util-perl 0.14-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 200 kB
  • ctags: 114
  • sloc: perl: 1,656; makefile: 42
file content (85 lines) | stat: -rw-r--r-- 1,956 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
#!/usr/bin/perl -w

use strict;
use lib ();
use File::Spec::Functions ':ALL';
BEGIN {
	$| = 1;
	unless ( $ENV{HARNESS_ACTIVE} ) {
		require FindBin;
		$FindBin::Bin = $FindBin::Bin; # Avoid a warning
		chdir catdir( $FindBin::Bin, updir() );
		lib->import(
			catdir('blib', 'arch'),
			catdir('blib', 'lib' ),
			catdir('lib'),
			);
	}
}

use Test::More tests => 29;
BEGIN {
	use_ok('Params::Util', qw(_ARRAYLIKE _HASHLIKE));
}

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'
  );

  is(
    _HASHLIKE( $t->[0]) ? 1 : 0,
    $t->[2],
    "$t->[3] " . ($t->[2] ? 'is' : "isn't") . ' %ish'
  );
}

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;