summaryrefslogtreecommitdiffstats
path: root/t/003-subclassing.t
blob: 6a562ee4e00ad71f7f58259872ee9428ad34ebc8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 10;

use MooseX::Module::Refresh;

use File::Temp 'tempdir';
my $tmp = tempdir( CLEANUP => 1 );

my $file = $tmp."/".'FooBar.pm';
my $subfile = $tmp."/".'FooBarSub.pm';
push @INC, $tmp;

write_out($file, <<".");
package FooBar;
use Moose;
has foo => (is => 'ro', clearer => 'clear_foo');
sub bar { 'bar' }
1;
.

write_out($subfile, <<".");
package FooBarSub;
use Moose;
extends 'FooBar';
around foo => sub { my (\$orig, \$self) = \@_; return \$self->\$orig . 'sub' };
around bar => sub { my (\$orig, \$self) = \@_; return \$self->\$orig . 'sub' };
1;
.

use_ok('FooBar', "Required our dummy module");
use_ok('FooBarSub', "Required our dummy subclass");

my $r = MooseX::Module::Refresh->new();

my $foobar = FooBarSub->new(foo => 'FOO');
is($foobar->bar, 'barsub', "We got the right result");
is($foobar->foo, 'FOOsub', "We got the right result");

write_out($file, <<".");
package FooBar; 
has baz => (is => 'ro', predicate => 'foo');
sub bar { 'baz' }
1;
.

$foobar = FooBarSub->new(foo => 'FOO');
is($foobar->bar, 'barsub', "We got the right result, still");
is($foobar->foo, 'FOOsub', "We got the right result, still");

$r->refresh;

$foobar = FooBarSub->new(baz => 'FOO');
is($foobar->bar, 'bazsub', "We got the right new result");
is($foobar->baz, 'FOO', "We got the right new result");
is($foobar->foo, '1sub', "We got the right new result");
ok(!$foobar->can('clear_foo'), "the clear_foo method was removed");

sub write_out {
    my $file = shift;
    local *FH;
    open FH, "> $file" or die "Cannot open $file: $!";
    print FH $_[0];
    close FH;
}

END {
    unlink $file, $subfile;
}