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