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__;
39 my ($variant, $named_variant) = do {
42 (TestVariable(3..7), TestVariable_named("Name", 3..7));
45 for ($variant, $named_variant) {
46 ok defined($_), 'new variant is a defined value';
47 ok length($_), 'new variant has length';
48 is $_->target, $_, 'target was new variant';
49 is_deeply $_->args, [3..7], 'correct arguments received';
51 is_deeply shift(@DECLARED), [target => $_],
52 'target passed via proxy';
53 is_deeply shift(@DECLARED), [args => [3..7]],
54 'arguments passed via proxy';
55 is_deeply shift(@DECLARED), [class => 'TestVariable'],
56 'class method resolution';
59 is scalar(@DECLARED), 0, 'proxy sub called right amount of times';
61 ok $named_variant->isa("Name"), 'created class can be named';
63 use TestVariable as => 'RenamedVar';
65 my $renamed = RenamedVar(9..12);
66 is_deeply $renamed->args, [9..12], 'imported generator can be renamed';
67 }, undef, 'no errors for renamed usage';
71 package TestImportableA;
72 sub import { push @imported, shift }
73 $INC{'TestImportableA.pm'} = __FILE__;
74 package TestImportableB;
75 sub import { push @imported, shift }
76 $INC{'TestImportableB.pm'} = __FILE__;
77 package TestArrayImports;
84 $INC{'TestArrayImports.pm'} = __FILE__;
90 is_deeply [@imported], [qw( TestImportableA TestImportableB )],
91 'multiple imports in the right order';
94 package TestSingleImport;
95 use Package::Variant importing => 'TestImportableA';
97 $INC{'TestSingleImport.pm'} = __FILE__;
102 use TestSingleImport;
103 TestSingleImport(23);
105 is_deeply [@imported], [qw( TestImportableA )],
106 'scalar import works';
110 TestSingleImport::->build_variant;
112 is_deeply [@imported], [qw( TestImportableA )],
113 'build_variant works';
117 is( TestSingleImport::->build_named_variant("Named"), "Named",
118 "build_named_variant applies name" );
120 is_deeply [@imported], [qw( TestImportableA )],
121 'build_variant works';
124 Package::Variant->import(
125 importing => \'foo', subs => [qw( foo )],
127 }, qr/importing.+option.+hash.+array/i, 'invalid "importing" option';
130 Package::Variant->import(
131 importing => { foo => \'bar' }, subs => [qw( bar )],
133 }, qr/import.+argument.+foo.+not.+array/i, 'invalid import argument list';
136 Package::Variant->import(
137 importing => [ foo => ['bar'], ['bam'], subs => [qw( bar )] ],
139 }, qr/value.+3.+importing.+not.+string/i, 'importing array invalid key';
142 Package::Variant->import(
143 importing => [ foo => \'bam', subs => [qw( bar )] ],
145 }, qr/value.+2.+foo.+importing.+array/i, 'importing array invalid list';