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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#!perl -T
use strict;
use warnings;
=head1 TEST PURPOSE
These tests check export group expansion, specifically the expansion of groups
that use group generators.
=cut
# XXX: The framework is stolen from expand-group. I guess it should be
# factored out. Whatever. -- rjbs, 2006-03-12
use Test::More tests => 12;
BEGIN { use_ok('Sub::Exporter'); }
my $alfa = sub { 'alfa' };
my $bravo = sub { 'bravo' };
my $returner = sub {
my ($class, $group, $arg, $collection) = @_;
my %given = (
class => $class,
group => $group,
arg => $arg,
collection => $collection,
);
return {
foo => sub { return { name => 'foo', %given }; },
bar => sub { return { name => 'bar', %given }; },
};
};
my $config = {
exports => [ ],
groups => {
alphabet => sub { { A => $alfa, b => $bravo } },
broken => sub { [ qw(this is broken because it is not a hashref) ] },
generated => $returner,
nested => [qw( :generated )],
},
collectors => [ 'col1' ],
};
my @single_tests = (
# [ comment, \@group, \@output ]
# [ "simple group 1", [ ':A' => undef ] => [ [ a => undef ] ] ],
[
"simple group generator",
[ -alphabet => undef ],
[ [ A => $alfa ], [ b => $bravo ] ],
],
[
"simple group generator with prefix",
[ -alphabet => { -prefix => 'prefix_' } ],
[ [ prefix_A => $alfa ], [ prefix_b => $bravo ] ],
],
);
for my $test (@single_tests) {
my ($label, $given, $expected) = @$test;
my @got = Sub::Exporter::_expand_group(
'Class',
$config,
$given,
{},
);
is_deeply(
[ sort { lc $a->[0] cmp lc $b->[0] } @got ],
$expected,
"expand_group: $label",
);
}
for my $test (@single_tests) {
my ($label, $given, $expected) = @$test;
my $got = Sub::Exporter::_expand_groups(
'Class',
$config,
[ $given ],
);
is_deeply(
[ sort { lc $a->[0] cmp lc $b->[0] } @$got ],
$expected,
"expand_groups: $label [single test]",
);
}
my @multi_tests = (
# [ $comment, \@groups, \@output ]
);
for my $test (@multi_tests) {
my ($label, $given, $expected) = @$test;
my $got = Sub::Exporter::_expand_groups(
'Class',
$config,
$given,
);
is_deeply($got, $expected, "expand_groups: $label");
}
##
eval {
Sub::Exporter::_expand_groups('Class', $config, [[ -broken => undef ]])
};
like($@,
qr/did not return a hash/,
"exception on non-hashref groupgen return",
);
##
{
my $got = Sub::Exporter::_expand_groups(
'Class',
$config,
[ [ -alphabet => undef ] ],
{},
);
my %code = map { $_->[0] => $_->[1] } @$got;
my $a = $code{A};
my $b = $code{b};
is($a->(), 'alfa', "generated 'a' sub does what we think");
is($b->(), 'bravo', "generated 'b' sub does what we think");
}
{
my $got = Sub::Exporter::_expand_groups(
'Class',
$config,
[ [ -generated => { xyz => 1 } ] ],
{ col1 => { value => 2 } },
);
my %code = map { $_->[0] => $_->[1] } @$got;
for (qw(foo bar)) {
is_deeply(
$code{$_}->(),
{
name => $_,
class => 'Class',
group => 'generated',
arg => { xyz => 1 },
collection => { col1 => { value => 2 } },
},
"generated foo does what we expect",
);
}
}
{
my $got = Sub::Exporter::_expand_groups(
'Class',
$config,
[ [ -nested => { xyz => 1 } ] ],
{ col1 => { value => 2 } },
);
my %code = map { $_->[0] => $_->[1] } @$got;
for (qw(foo bar)) {
is_deeply(
$code{$_}->(),
{
name => $_,
class => 'Class',
group => 'generated',
arg => { xyz => 1 },
collection => { col1 => { value => 2 } },
},
"generated foo (via nested group) does what we expect",
);
}
}
|