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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
#!perl -T
use strict;
use warnings;
=head1 TEST PURPOSE
These tests check export group expansion, name prefixing, and option merging.
=cut
use Test::More tests => 55;
BEGIN { use_ok('Sub::Exporter'); }
my $import_target;
my $config = {
exports => [ qw(a b c) ],
groups => {
A => [ 'a' ],
B => [ qw(b c) ],
C => [ qw(a b :C) ],
D => [ qw(:A :B) ],
a_as_b => [ a => { -as => 'b' } ],
prefixed_A => [ -A => { -prefix => 'alfa_' } ],
suffixed_A => [ -A => { -suffix => '_yankee' } ],
diprefixed_A => [ -prefixed_A => { -prefix => 'bravo_' } ],
disuffixed_A => [ -suffixed_A => { -suffix => '_zulu' } ],
presuffixed_A=> [ -A => { -prefix => 'freakin_', -suffix => '_right' } ],
a_to_subref => [ a => { -as => \$import_target }, 'b' ],
prefixed_a_s => [ -a_to_subref => { -prefix => 'alfa_' } ],
}
};
my @single_tests = (
[ "simple group 1", [ ':A' => undef ] => [ [ a => undef ] ] ],
[ "simple group 2", [ ':B' => undef ] => [ [ b => undef ], [ c => undef ] ] ],
[
"group of groups",
[ ':D' => undef ],
[ [ a => undef ], [ b => undef ], [ c => undef ] ],
],
[
"recursive group",
[ ':C' => undef ],
[ [ a => undef ], [b => undef ] ],
],
[
"group with empty args",
[ -A => { } ],
[ [ a => undef ] ],
],
[
"group with prefix",
[ -A => { -prefix => 'alpha_' } ],
[ [ a => { -as => 'alpha_a' } ] ],
],
[
"group with suffix",
[ -A => { -suffix => '_import' } ],
[ [ a => { -as => 'a_import' } ] ],
],
[
"recursive group with prefix",
[ -C => { -prefix => 'kappa_' } ],
[ [ a => { -as => 'kappa_a' } ], [ b => { -as => 'kappa_b' } ] ],
],
[
"recursive group with suffix",
[ -C => { -suffix => '_etc' } ],
[ [ a => { -as => 'a_etc' } ], [ b => { -as => 'b_etc' } ] ],
],
[
"group that renames",
[ -a_as_b => undef ],
[ [ a => { -as => 'b' } ] ],
],
[
"group that renames, with options",
[ -a_as_b => { foo => 10 } ],
[ [ a => { -as => 'b', foo => 10 } ] ],
],
[
"group that renames, with a prefix",
[ -a_as_b => { -prefix => 'not_really_' } ],
[ [ a => { -as => 'not_really_b' } ] ],
],
[
"group that renames, with a suffix",
[ -a_as_b => { -suffix => '_or_not' } ],
[ [ a => { -as => 'b_or_not' } ] ],
],
[
"group that renames, with a prefix and suffix",
[ -a_as_b => { -prefix => 'not_really_' } ],
[ [ a => { -as => 'not_really_b' } ] ],
],
[
"recursive group with a built-in prefix",
[ -prefixed_A => undef ],
[ [ a => { -as => 'alfa_a' } ] ],
],
[
"recursive group with built-in and passed-in prefix",
[ -prefixed_A => { -prefix => 'bravo_' } ],
[ [ a => { -as => 'bravo_alfa_a' } ] ],
],
[
"recursive group with built-in and passed-in suffix",
[ -suffixed_A => { -suffix => '_zulu' } ],
[ [ a => { -as => 'a_yankee_zulu' } ] ],
],
[
"multi-prefixed group",
[ -diprefixed_A => undef ],
[ [ a => { -as => 'bravo_alfa_a' } ] ],
],
[
"multi-suffixed group",
[ -disuffixed_A => undef ],
[ [ a => { -as => 'a_yankee_zulu' } ] ],
],
[
"multi-prefixed group with prefix",
[ -diprefixed_A => { -prefix => 'charlie_' } ],
[ [ a => { -as => 'charlie_bravo_alfa_a' } ] ],
],
[
"group with built-in prefix and suffix",
[ -presuffixed_A => undef ],
[ [ a => { -as => 'freakin_a_right' } ] ],
],
[
"group with built-in prefix and suffix, plus prefix",
[ -presuffixed_A => { -prefix => 'totally_' } ],
[ [ a => { -as => 'totally_freakin_a_right' } ] ],
],
[
"group with built-in prefix and suffix, plus suffix",
[ -presuffixed_A => { -suffix => '_dude' } ],
[ [ a => { -as => 'freakin_a_right_dude' } ] ],
],
[
"group with built-in prefix and suffix, plus prefix and suffix",
[ -presuffixed_A => { -prefix => 'totally_', -suffix => '_dude' } ],
[ [ a => { -as => 'totally_freakin_a_right_dude' } ] ],
],
[
"group that exports to scalar (unusual)",
[ -a_to_subref => undef ],
[ [ a => { -as => \$import_target } ], [ b => undef ] ],
],
[
"group that exports to scalar, with prefix",
[ -a_to_subref => { -prefix => 'jubju' } ],
[ [ a => { -as => \$import_target } ], [ b => { -as => 'jubjub' } ] ],
],
);
for my $test (@single_tests) {
my ($label, $given, $expected) = @$test;
my @got = Sub::Exporter::_expand_group(
'Class',
$config,
$given,
{},
);
is_deeply(\@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($got, $expected, "expand_groups: $label [single test]");
}
my @multi_tests = (
[
"group and export",
[ [ ':A', undef ], [ c => undef ] ],
[ [ a => undef ], [ c => undef ] ]
],
[
"two groups with different merges",
[ [ -A => { -prefix => 'A_' } ], [ -prefixed_A => { -suffix => '_p' } ] ],
[
[ a => { -as => 'A_a' } ],
[ a => { -as => 'alfa_a_p' } ],
]
],
);
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");
}
|