X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F01simple.t;h=4bb45482daa5b7a43d155f9231ee5df5b0987271;hb=185a0b334446d0d1642be7e89c34da88f3b0bff6;hp=2d518734430c11e5ccd99b36197485bffd317b66;hpb=815b5be2c946367c4d74c815599ea9d706c0f5e3;p=p5sagit%2FPackage-Variant.git diff --git a/t/01simple.t b/t/01simple.t index 2d51873..4bb4548 100644 --- a/t/01simple.t +++ b/t/01simple.t @@ -36,25 +36,30 @@ BEGIN { $INC{'TestVariable.pm'} = __FILE__; } -my $variant = do { +my ($variant, $named_variant) = do { package TestScopeA; use TestVariable; - TestVariable(3..7); + (TestVariable(3..7), TestVariable_named("Name", 3..7)); }; -ok defined($variant), 'new variant is a defined value'; -ok length($variant), 'new variant has length'; -is $variant->target, $variant, 'target was new variant'; -is_deeply $variant->args, [3..7], 'correct arguments received'; - -is_deeply shift(@DECLARED), [target => $variant], - 'target passed via proxy'; -is_deeply shift(@DECLARED), [args => [3..7]], - 'arguments passed via proxy'; -is_deeply shift(@DECLARED), [class => 'TestVariable'], - 'class method resolution'; +for ($variant, $named_variant) { + ok defined($_), 'new variant is a defined value'; + ok length($_), 'new variant has length'; + is $_->target, $_, 'target was new variant'; + is_deeply $_->args, [3..7], 'correct arguments received'; + + is_deeply shift(@DECLARED), [target => $_], + 'target passed via proxy'; + is_deeply shift(@DECLARED), [args => [3..7]], + 'arguments passed via proxy'; + is_deeply shift(@DECLARED), [class => 'TestVariable'], + 'class method resolution'; +} + is scalar(@DECLARED), 0, 'proxy sub called right amount of times'; +ok $named_variant->isa("Name"), 'created class can be named'; + use TestVariable as => 'RenamedVar'; is exception { my $renamed = RenamedVar(9..12); @@ -72,8 +77,8 @@ BEGIN { package TestArrayImports; use Package::Variant importing => [ - TestImportableA => undef, - TestImportableB => undef, + 'TestImportableA', + 'TestImportableB', ]; sub make_variant { } $INC{'TestArrayImports.pm'} = __FILE__; @@ -85,4 +90,58 @@ TestArrayImports(23); is_deeply [@imported], [qw( TestImportableA TestImportableB )], 'multiple imports in the right order'; +BEGIN { + package TestSingleImport; + use Package::Variant importing => 'TestImportableA'; + sub make_variant { } + $INC{'TestSingleImport.pm'} = __FILE__; +} + +@imported = (); + +use TestSingleImport; +TestSingleImport(23); + +is_deeply [@imported], [qw( TestImportableA )], + 'scalar import works'; + +@imported = (); + +TestSingleImport::->build_variant; + +is_deeply [@imported], [qw( TestImportableA )], + 'build_variant works'; + +@imported = (); + +is( TestSingleImport::->build_named_variant("Named"), "Named", + "build_named_variant applies name" ); + +is_deeply [@imported], [qw( TestImportableA )], + 'build_variant works'; + +like exception { + Package::Variant->import( + importing => \'foo', subs => [qw( foo )], + ); +}, qr/importing.+option.+hash.+array/i, 'invalid "importing" option'; + +like exception { + Package::Variant->import( + importing => { foo => \'bar' }, subs => [qw( bar )], + ); +}, qr/import.+argument.+foo.+not.+array/i, 'invalid import argument list'; + +like exception { + Package::Variant->import( + importing => [ foo => ['bar'], ['bam'], subs => [qw( bar )] ], + ); +}, qr/value.+3.+importing.+not.+string/i, 'importing array invalid key'; + +like exception { + Package::Variant->import( + importing => [ foo => \'bam', subs => [qw( bar )] ], + ); +}, qr/value.+2.+foo.+importing.+array/i, 'importing array invalid list'; + done_testing;