Commit | Line | Data |
f9c096bb |
1 | use strictures 1; |
2 | use Test::More; |
3 | use Test::Fatal; |
4 | use Package::Variant (); |
5 | |
6 | my @DECLARED; |
7 | |
8 | BEGIN { |
9 | package TestSugar; |
1abbe9d7 |
10 | use Exporter 'import'; |
f9c096bb |
11 | our @EXPORT_OK = qw( declare ); |
12 | sub declare { push @DECLARED, [@_] } |
13 | $INC{'TestSugar.pm'} = __FILE__; |
14 | } |
15 | |
16 | BEGIN { |
17 | package TestVariable; |
18 | use Package::Variant |
19 | importing => { 'TestSugar' => [qw( declare )] }, |
20 | subs => [qw( declare )]; |
21 | sub make_variant { |
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] }; |
32 | } |
33 | sub _test_class_method { |
34 | return shift; |
35 | } |
36 | $INC{'TestVariable.pm'} = __FILE__; |
37 | } |
38 | |
b6086e0d |
39 | my $variant = do { |
40 | package TestScopeA; |
41 | use TestVariable; |
42 | TestVariable(3..7); |
43 | }; |
f9c096bb |
44 | |
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'; |
49 | |
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'; |
1abbe9d7 |
54 | is_deeply shift(@DECLARED), [class => 'TestVariable'], |
f9c096bb |
55 | 'class method resolution'; |
56 | is scalar(@DECLARED), 0, 'proxy sub called right amount of times'; |
57 | |
b6086e0d |
58 | use TestVariable as => 'RenamedVar'; |
59 | is exception { |
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'; |
63 | |
815b5be2 |
64 | my @imported; |
65 | BEGIN { |
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; |
73 | use Package::Variant |
74 | importing => [ |
efaab257 |
75 | 'TestImportableA', |
76 | 'TestImportableB', |
815b5be2 |
77 | ]; |
78 | sub make_variant { } |
79 | $INC{'TestArrayImports.pm'} = __FILE__; |
80 | } |
81 | |
82 | use TestArrayImports; |
83 | TestArrayImports(23); |
84 | |
85 | is_deeply [@imported], [qw( TestImportableA TestImportableB )], |
86 | 'multiple imports in the right order'; |
87 | |
203d81fc |
88 | BEGIN { |
89 | package TestSingleImport; |
90 | use Package::Variant importing => 'TestImportableA'; |
91 | sub make_variant { } |
92 | $INC{'TestSingleImport.pm'} = __FILE__; |
93 | } |
94 | |
95 | @imported = (); |
96 | |
97 | use TestSingleImport; |
98 | TestSingleImport(23); |
99 | |
100 | is_deeply [@imported], [qw( TestImportableA )], |
101 | 'scalar import works'; |
102 | |
115c342b |
103 | like exception { |
104 | Package::Variant->import( |
105 | importing => \'foo', subs => [qw( foo )], |
106 | ); |
107 | }, qr/importing.+option.+hash.+array/i, 'invalid "importing" option'; |
108 | |
109 | like exception { |
110 | Package::Variant->import( |
111 | importing => { foo => \'bar' }, subs => [qw( bar )], |
112 | ); |
203d81fc |
113 | }, qr/import.+argument.+foo.+not.+array/i, 'invalid import argument list'; |
114 | |
115 | like exception { |
116 | Package::Variant->import( |
117 | importing => [ foo => ['bar'], ['bam'], subs => [qw( bar )] ], |
118 | ); |
119 | }, qr/value.+3.+importing.+not.+string/i, 'importing array invalid key'; |
120 | |
121 | like exception { |
122 | Package::Variant->import( |
123 | importing => [ foo => \'bam', subs => [qw( bar )] ], |
124 | ); |
125 | }, qr/value.+2.+foo.+importing.+array/i, 'importing array invalid list'; |
115c342b |
126 | |
f9c096bb |
127 | done_testing; |