t/10-usage.t
changeset 0 f65c3dd5742a
child 1 82e2b6a853a7
equal deleted inserted replaced
-1:000000000000 0:f65c3dd5742a
       
     1 #! perl
       
     2 
       
     3 use strict;
       
     4 use warnings;
       
     5 use Test::More;
       
     6 use Test::Exception;
       
     7 
       
     8 use_ok 'Scalar::RefType' or BAIL_OUT q{Can't load the module};
       
     9 isa_ok tie(my $h => 'Scalar::RefType', {}) => 'Scalar::RefType';
       
    10 isa_ok tie(my $a => 'Scalar::RefType', []) => 'Scalar::RefType';
       
    11 isa_ok tie(my $s => 'Scalar::RefType', \(undef)) => 'Scalar::RefType';
       
    12 isa_ok tie(my $p => 'Scalar::RefType', undef)    => 'Scalar::RefType';
       
    13 
       
    14 subtest 'NAMES' => sub {
       
    15     isa_ok tie(my $h => 'Scalar::RefType', ref {}) => 'Scalar::RefType';
       
    16     isa_ok tie(my $a => 'Scalar::RefType', ref []) => 'Scalar::RefType';
       
    17     isa_ok tie(my $s => 'Scalar::RefType', ref \(undef)) => 'Scalar::RefType';
       
    18     isa_ok tie(my $p => 'Scalar::RefType', '') => 'Scalar::RefType';
       
    19 
       
    20     is tied($h)->{type}, 'HASH'   => 'is a hash ref';
       
    21     is tied($a)->{type}, 'ARRAY'  => 'is a array ref';
       
    22     is tied($s)->{type}, 'SCALAR' => 'is a scalar';
       
    23     is tied($p)->{type}, ''       => 'is a "plain"';
       
    24 
       
    25 };
       
    26 
       
    27 is tied($h)->{type}, 'HASH'   => 'is a hash ref';
       
    28 is tied($a)->{type}, 'ARRAY'  => 'is a array ref';
       
    29 is tied($s)->{type}, 'SCALAR' => 'is a scalar';
       
    30 is tied($p)->{type}, ''       => 'is a "plain"';
       
    31 
       
    32 is ref($h = { a => 'A' }), 'HASH' => 'hash: assignment';
       
    33 is ref($a = ['a', 'b']), 'ARRAY' => 'array: assignment';
       
    34 is ref($s = \6), 'SCALAR' => 'scalar: assignment';
       
    35 is ref($p = 6), '' => 'plain: assignment';
       
    36 
       
    37 is $h->{a}, 'A' => 'hash: got the value';
       
    38 is $a->[1], 'b' => 'array: got the value';
       
    39 is $$s, 6, => 'scalar: got the value';
       
    40 is $p,  6, => 'plain: got the value';
       
    41 
       
    42 throws_ok { $h = [] } qr/invalid/ => 'hash: dies on invalid type';
       
    43 throws_ok { $h = 6 } qr/invalid/  => 'hash: dies on invalid type';
       
    44 
       
    45 done_testing;