added test for renamable import
[p5sagit/Package-Variant.git] / t / 01simple.t
index 2b33250..6cb6406 100644 (file)
@@ -1,36 +1,64 @@
 use strictures 1;
-use Test::More qw(no_plan);
+use Test::More;
+use Test::Fatal;
+use Package::Variant ();
+
+my @DECLARED;
 
 BEGIN {
-  package My::Role::OnOff;
+  package TestSugar;
+  use Exporter 'import';
+  our @EXPORT_OK = qw( declare );
+  sub declare { push @DECLARED, [@_] }
+  $INC{'TestSugar.pm'} = __FILE__;
+}
 
+BEGIN {
+  package TestVariable;
   use Package::Variant
-    importing => { 'Moo::Role' => [] },
-    subs => [ qw(has before after around) ];
-
+    importing => { 'TestSugar' => [qw( declare )] },
+    subs      => [qw( declare )];
   sub make_variant {
-    my ($me, $into, %args) = @_;
-    my $name = $args{name};
-    has $name => (is => 'rw');
-    install "${name}_on" => sub { shift->$name(1); };
-    install "${name}_off" => sub { shift->$name(0); };
+    my ($class, $target, @args) = @_;
+    ::ok(__PACKAGE__->can('install'), 'install() is available')
+      or ::BAIL_OUT('install() subroutine was not exported!');
+    ::ok(__PACKAGE__->can('declare'), 'declare() import is available')
+      or ::BAIL_OUT('proxy declare() subroutine was not exported!');
+    declare target => $target;
+    declare args   => [@args];
+    declare class  => $class->_test_class_method;
+    install target => sub { $target };
+    install args   => sub { [@args] };
   }
-  $INC{"My/Role/OnOff.pm"} = __FILE__;
+  sub _test_class_method {
+    return shift;
+  }
+  $INC{'TestVariable.pm'} = __FILE__;
 }
 
-BEGIN {
-  package LightSwitch;
+my $variant = do {
+    package TestScopeA;
+    use TestVariable;
+    TestVariable(3..7);
+};
 
-  use My::Role::OnOff;
-  use Moo;
+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';
 
-  with OnOff(name => 'lights');
-}
+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';
+is scalar(@DECLARED), 0, 'proxy sub called right amount of times';
 
-my $lights = LightSwitch->new;
+use TestVariable as => 'RenamedVar';
+is exception {
+  my $renamed = RenamedVar(9..12);
+  is_deeply $renamed->args, [9..12], 'imported generator can be renamed';
+}, undef, 'no errors for renamed usage';
 
-is($lights->lights, undef, 'Initial state');
-is($lights->lights_on, 1, 'Turn on');
-is($lights->lights, 1, 'On');
-is($lights->lights_off, 0, 'Turn off');
-is($lights->lights, 0, 'Off');
+done_testing;