6 use Test::More tests => 45;
14 Moose::Exporter->setup_import_methods(
16 with_meta => ['with_meta1'],
17 with_caller => ['default_export1'],
18 as_is => ['default_export2'],
19 groups => { all_group => [':all'],
20 just_one => ['default_export1'] }
23 sub default_export1 { 1 }
24 sub default_export2 { 2 }
27 my ($meta, $code) = @_;
35 ExGroups1->import(':all_group');
37 ::can_ok( __PACKAGE__, 'with_meta1' );
38 ::can_ok( __PACKAGE__, 'default_export1' );
39 ::can_ok( __PACKAGE__, 'default_export2' );
40 ::can_ok( __PACKAGE__, 'has' );
43 eval q/$meta = with_meta1 { return 'coderef'; }/;
44 ::is($@, '', 'calling with_meta1 with prototype is not an error');
45 ::isa_ok( $meta, 'Moose::Meta::Class', 'with_meta first argument' );
46 ::is( prototype( __PACKAGE__->can('with_meta1') ),
47 prototype( ExGroups1->can('with_meta1') ),
48 'using correct prototype on with_meta function' );
50 ExGroups1->unimport();
52 ::ok( ! __PACKAGE__->can('with_meta1'), __PACKAGE__.'::with_meta1() has been cleaned' );
53 ::ok( ! __PACKAGE__->can('default_export1'), __PACKAGE__.'::default_export1() has been cleaned' );
54 ::ok( ! __PACKAGE__->can('default_export2'), __PACKAGE__.'::default_export2() has been cleaned' );
55 ::ok( ! __PACKAGE__->can('has'), __PACKAGE__.'::has() has been cleaned' );
61 ExGroups1->import(':just_one');
63 ::can_ok( __PACKAGE__, 'default_export1' );
64 ::ok( ! __PACKAGE__->can('default_export2'), __PACKAGE__.'::default_export2() was not imported' );
65 ::ok( ! __PACKAGE__->can('has'), __PACKAGE__.'::has() was not imported' );
67 ExGroups1->unimport();
69 ::ok( ! __PACKAGE__->can('default_export1'), __PACKAGE__.'::default_export1() has been cleared' );
76 Moose::Exporter->setup_import_methods(
77 also => ['ExGroups1'],
78 as_is => ['exgroups2_as_is'],
79 with_caller => ['exgroups2_with_caller'],
80 groups => { default => ['exgroups2_as_is'],
81 code_group => \&generate_group,
82 parent1 => [qw(:ExGroups1 :code_group)],
83 parent2 => [qw(:all)] }
86 sub exgroups2_as_is { 3 }
89 my ($caller, $group_name, $args, $context) = @_;
91 ::is($group_name, 'code_group', 'original name is passed to group code');
92 ::is($args->{install_as}, $caller . '_code', 'group code arguments match caller');
93 ::is($context->{from}, __PACKAGE__, 'defined package name is passed to group code');
95 return { $args->{install_as} => \&exported_by_group };
98 sub exported_by_group (&) {
99 my ($caller, $coderef) = @_;
109 ::can_ok( __PACKAGE__, 'exgroups2_as_is' );
110 ::ok( ! __PACKAGE__->can('exgroups2_with_caller'), '"default" group is no longer "all"' );
114 package UseCodeGroup;
116 ExGroups2->import(':code_group', { install_as => (my $export_name = __PACKAGE__.'_code') });
118 ::can_ok( __PACKAGE__, $export_name );
119 ::ok( &UseCodeGroup_code() eq __PACKAGE__, 'code group exports act like "with_caller" subs' );
120 ::lives_ok(sub { UseCodeCodeGroup_code { return 'code block'; } }, 'code group exports keep their prototypes');
122 ::ok( ! __PACKAGE__->can('exgroups2_as_is'), 'code group will not automatically export any symbols' );
126 ::ok( ! __PACKAGE__->can($export_name),
127 'dynamically-named '. __PACKAGE__."::$export_name() has been cleared" );
133 ExGroups2->import(':parent1', { install_as => (my $export_name = __PACKAGE__.'_code') });
135 ::can_ok( __PACKAGE__, $export_name );
136 ::can_ok( __PACKAGE__, 'default_export1' );
137 ::can_ok( __PACKAGE__, 'default_export2' );
138 ::can_ok( __PACKAGE__, 'has' );
142 ::ok( ! __PACKAGE__->can($export_name), __PACKAGE__."::$export_name() has been cleared" );
143 ::ok( ! __PACKAGE__->can('default_export1'), __PACKAGE__.'::default_export1() has been cleaned' );
144 ::ok( ! __PACKAGE__->can('default_export2'), __PACKAGE__.'::default_export2() has been cleaned' );
145 ::ok( ! __PACKAGE__->can('has'), __PACKAGE__.'::has() has been cleaned' );
151 ExGroups2->import(':parent2', { install_as => (my $export_name = __PACKAGE__.'_code') });
153 ::ok( ! __PACKAGE__->can($export_name), '"all" group will not call code groups' );
154 ::can_ok( __PACKAGE__, 'exgroups2_as_is' );
155 ::can_ok( __PACKAGE__, 'exgroups2_with_caller' );
156 ::can_ok( __PACKAGE__, 'default_export1' );
157 ::can_ok( __PACKAGE__, 'has' );
161 ::ok( ! __PACKAGE__->can('exgroups2_as_is'), __PACKAGE__.'::exgroups2_as_is() has been cleaned' );
162 ::ok( ! __PACKAGE__->can('exgroups2_with_caller'), __PACKAGE__.'::exgroups2_with_caller() has been cleaned' );
163 ::ok( ! __PACKAGE__->can('default_export1'), __PACKAGE__.'::default_export1() has been cleaned' );
164 ::ok( ! __PACKAGE__->can('has'), __PACKAGE__.'::has() has been cleaned' );