1# IO::Callback 1.08 t/constructor-args.t
2# Check that invalid constructor args are caught
3# Check that extra constructor args are passed on to the callback
4
5use strict;
6use warnings;
7
8use Test::More;
9use Test::NoWarnings;
10use Test::Exception;
11
12use IO::Callback;
13
14my $fh_src = IO::Callback->new('<', sub {});
15
16my %constructor_source = (
17    package => 'IO::Callback',
18    object  => $fh_src,
19);
20
21my @extra_constructor_args = (
22    [],
23    [[]],
24    [{}],
25    [0],
26    [[0]],
27    [[1,2,3]],
28    [undef],
29    [[undef]],
30    [1, 2, 3],
31    [undef, undef, undef],
32    [{}, [], {}, \$fh_src],
33);
34my $consarg;
35
36sub read_callback {
37    is flat(@_), flat(@$consarg), "flattened constructor args consistent";
38    return;
39}
40
41plan tests => 2 * (@extra_constructor_args + 10) + 1;
42
43while ( my ($src_name, $src) = each %constructor_source ) {
44    foreach my $c (@extra_constructor_args) {
45        $consarg = $c;
46        IO::Callback->new("<", \&read_callback, @$consarg)->getc;
47    }
48
49    my $res;
50
51    throws_ok { $res = $src->new }
52      q{/^mode missing in IO::Callback::new at /}, "$src_name no mode no sub";
53
54    throws_ok { $res = $src->new(undef) }
55      q{/^mode missing in IO::Callback::new at /}, "$src_name undef mode no sub";
56
57    throws_ok { $res = $src->new('r') }
58      q{/^invalid mode "r" in IO::Callback::new at /}, "$src_name invalid mode no sub";
59
60    throws_ok { $res = $src->new(undef, undef) }
61      q{/^mode missing in IO::Callback::new at /}, "$src_name undef mode undef sub";
62
63    throws_ok { $res = $src->new('r', undef) }
64      q{/invalid mode "r" in IO::Callback::new at /}, "$src_name invalid mode undef sub";
65
66    throws_ok { $res = $src->new('r', 'not a coderef') }
67      q{/^invalid mode "r" in IO::Callback::new at /}, "$src_name invalid mode invalid sub";
68
69    throws_ok { $res = $src->new('r', sub {}) }
70      q{/^invalid mode "r" in IO::Callback::new at /}, "$src_name invalid mode valid sub";
71
72    throws_ok { $res = $src->new('<') }
73      q{/^coderef missing in IO::Callback::new at /}, "$src_name valid mode no sub";
74
75    throws_ok { $res = $src->new('<', undef) }
76      q{/^coderef missing in IO::Callback::new at /}, "$src_name valid mode undef sub";
77
78    throws_ok { $res = $src->new('<', 1) }
79      q{/^non-coderef second argument in IO::Callback::new at /}, "$src_name valid mode invalid sub";
80}
81
82sub flat {
83    return join ",", map { defined() ? "{$_}" : "undef" } @ARGV;
84}
85
86