6e88af9cd16328e396e2d7af90bd87ca7a6e4732
[gitmo/Moose.git] / t / 060_compat / 001_module_refresh_compat.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib', 'lib';
7
8 use Test::More;
9 use Test::Exception;
10
11 use File::Spec;
12 use File::Temp 'tempdir';
13
14 BEGIN {
15     eval "use Module::Refresh;";
16     plan skip_all => "Module::Refresh is required for this test" if $@;
17 }
18
19 =pod
20
21 First lets test some of our simple example modules ...
22
23 =cut
24
25 my @modules = qw[Foo Bar MyMooseA MyMooseB MyMooseObject];
26
27 do {
28     use_ok($_);
29
30     is($_->meta->name, $_, '... initialized the meta correctly');
31
32     lives_ok {
33         Module::Refresh->new->refresh_module($_ . '.pm')
34     } '... successfully refreshed ' . $_;
35 } foreach @modules;
36
37 =pod
38
39 Now, lets try something a little trickier
40 and actually change the module itself.
41
42 =cut
43
44 my $dir = tempdir( "MooseTest-XXXXX", CLEANUP => 1, TMPDIR => 1 );
45 push @INC, $dir;
46
47 my $test_module_file = File::Spec->catdir($dir, 'TestBaz.pm');
48
49 my $test_module_source_1 = q|
50 package TestBaz;
51 use Moose;
52 has 'foo' => (is => 'ro', isa => 'Int');
53 1;
54 |;
55
56 my $test_module_source_2 = q|
57 package TestBaz;
58 use Moose;
59 extends 'Foo';
60 has 'foo' => (is => 'rw', isa => 'Int');
61 1;
62 |;
63
64 {
65     open FILE, ">", $test_module_file
66         || die "Could not open $test_module_file because $!";
67     print FILE $test_module_source_1;
68     close FILE;
69 }
70
71 use_ok('TestBaz');
72 is(TestBaz->meta->name, 'TestBaz', '... initialized the meta correctly');
73 ok(TestBaz->meta->has_attribute('foo'), '... it has the foo attribute as well');
74 ok(!TestBaz->isa('Foo'), '... TestBaz is not a Foo');
75
76 {
77     open FILE, ">", $test_module_file
78         || die "Could not open $test_module_file because $!";
79     print FILE $test_module_source_2;
80     close FILE;
81 }
82
83 lives_ok {
84     Module::Refresh->new->refresh_module($test_module_file)
85 } '... successfully refreshed ' . $test_module_file;
86
87 is(TestBaz->meta->name, 'TestBaz', '... initialized the meta correctly');
88 ok(TestBaz->meta->has_attribute('foo'), '... it has the foo attribute as well');
89 ok(TestBaz->isa('Foo'), '... TestBaz is a Foo');
90
91 unlink $test_module_file;
92
93 done_testing;