more testing for different @INC hook types
[scpubgit/Object-Remote.git] / t / sender.t
1 use strictures 1;
2 use Test::More;
3
4 $ENV{OBJECT_REMOTE_TEST_LOGGER} = 1;
5
6 use Object::Remote::Connector::Local;
7 use Object::Remote;
8 use Object::Remote::ModuleSender;
9
10 $ENV{PERL5LIB} = join(
11   ':', ($ENV{PERL5LIB} ? $ENV{PERL5LIB} : ()), qw(lib)
12 );
13
14 my $mod_content = do {
15   open my $fh, '<', 't/lib/ORTestClass.pm'
16     or die "can't read ORTestClass.pm: $!";
17   local $/;
18   <$fh>
19 };
20 my $modules = {
21   'ORTestClass.pm' => $mod_content,
22 };
23
24 sub TestModuleProvider::INC {
25   my ($self, $module) = @_;
26   if (my $data = $self->{modules}{$module}) {
27     open my $fh, '<', \$data
28       or die "Unable to open in-memory file: $!";
29     return $fh;
30   }
31   return;
32 }
33
34 my %sources = (
35   basic => [ 't/lib' ],
36   sub => [ sub {
37     if (my $data = $modules->{$_[1]}) {
38       open my $fh, '<', \$data
39         or die "Unable to open in-memory file: $!";
40       return $fh;
41     }
42     return;
43   } ],
44   dynamic_array => [ [ sub {
45     my $mods = $_[0][1];
46     if (my $data = $mods->{$_[1]}) {
47       open my $fh, '<', \$data
48         or die "Unable to open in-memory file: $!";
49       return $fh;
50     }
51     return;
52   }, $modules ] ],
53   object => [ bless { modules => $modules }, 'TestModuleProvider' ],
54   filter_sub => [ sub {
55     if (my $data = $modules->{$_[1]}) {
56       my @lines = split /\n/, $data;
57       my $read = join("\n", 0..$#lines);
58       open my $fh, '<', \$read
59         or die "welp $!";
60       return ($fh, sub {
61         chomp;
62         my $ret = $_ != $#lines ? 1 : 0;
63         $_ = $lines[$_];
64         return $ret;
65       });
66     }
67     return;
68   } ],
69   feed_sub => [ sub {
70     if (my $data = $modules->{$_[1]}) {
71       my @lines = split /(\n)/, $data;
72       return sub {
73         $_ = shift @lines;
74         return @lines ? 1 : 0;
75       };
76     }
77     return;
78   } ],
79 );
80
81 for my $source (sort keys %sources) {
82   my $ms = Object::Remote::ModuleSender->new(
83     dir_list => $sources{$source},
84   );
85   my $connection = Object::Remote::Connector::Local->new(
86                   module_sender => $ms,
87                   )->connect;
88
89   my $counter = Object::Remote->new(
90     connection => $connection,
91     class => 'ORTestClass'
92   );
93
94   isnt($$, $counter->pid, "$source sender: Different pid on the other side");
95
96   is($counter->counter, 0, "$source sender: Counter at 0");
97
98   is($counter->increment, 1, "$source sender: Increment to 1");
99
100   is($counter->counter, 1, "$source sender: Counter at 1");
101 }
102
103 done_testing;