4 use Package::Variant ();
10 use Exporter 'import';
11 our @EXPORT_OK = qw( declare );
12 sub declare { push @DECLARED, [@_] }
13 $INC{'TestSugar.pm'} = __FILE__;
19 importing => { 'TestSugar' => [qw( declare )] },
20 subs => [qw( declare )];
22 my ($class, $target, @args) = @_;
23 ::ok(__PACKAGE__->can('install'), 'install() is available')
24 or ::BAIL_OUT('install() subroutine was not exported!');
25 ::ok(__PACKAGE__->can('declare'), 'declare() import is available')
26 or ::BAIL_OUT('proxy declare() subroutine was not exported!');
27 declare target => $target;
28 declare args => [@args];
29 declare class => $class->_test_class_method;
30 install target => sub { $target };
31 install args => sub { [@args] };
33 sub _test_class_method {
36 $INC{'TestVariable.pm'} = __FILE__;
45 ok defined($variant), 'new variant is a defined value';
46 ok length($variant), 'new variant has length';
47 is $variant->target, $variant, 'target was new variant';
48 is_deeply $variant->args, [3..7], 'correct arguments received';
50 is_deeply shift(@DECLARED), [target => $variant],
51 'target passed via proxy';
52 is_deeply shift(@DECLARED), [args => [3..7]],
53 'arguments passed via proxy';
54 is_deeply shift(@DECLARED), [class => 'TestVariable'],
55 'class method resolution';
56 is scalar(@DECLARED), 0, 'proxy sub called right amount of times';
58 use TestVariable as => 'RenamedVar';
60 my $renamed = RenamedVar(9..12);
61 is_deeply $renamed->args, [9..12], 'imported generator can be renamed';
62 }, undef, 'no errors for renamed usage';
66 package TestImportableA;
67 sub import { push @imported, shift }
68 $INC{'TestImportableA.pm'} = __FILE__;
69 package TestImportableB;
70 sub import { push @imported, shift }
71 $INC{'TestImportableB.pm'} = __FILE__;
72 package TestArrayImports;
79 $INC{'TestArrayImports.pm'} = __FILE__;
85 is_deeply [@imported], [qw( TestImportableA TestImportableB )],
86 'multiple imports in the right order';
89 package TestSingleImport;
90 use Package::Variant importing => 'TestImportableA';
92 $INC{'TestSingleImport.pm'} = __FILE__;
100 is_deeply [@imported], [qw( TestImportableA )],
101 'scalar import works';
105 TestSingleImport::->build_variant;
107 is_deeply [@imported], [qw( TestImportableA )],
108 'build_variant works';
111 Package::Variant->import(
112 importing => \'foo', subs => [qw( foo )],
114 }, qr/importing.+option.+hash.+array/i, 'invalid "importing" option';
117 Package::Variant->import(
118 importing => { foo => \'bar' }, subs => [qw( bar )],
120 }, qr/import.+argument.+foo.+not.+array/i, 'invalid import argument list';
123 Package::Variant->import(
124 importing => [ foo => ['bar'], ['bam'], subs => [qw( bar )] ],
126 }, qr/value.+3.+importing.+not.+string/i, 'importing array invalid key';
129 Package::Variant->import(
130 importing => [ foo => \'bam', subs => [qw( bar )] ],
132 }, qr/value.+2.+foo.+importing.+array/i, 'importing array invalid list';