Work in progress on compiler. It's blowing up right now.
[gitmo/MooseX-Compiler.git] / t / lib / Test / MooseX / Compiler.pm
CommitLineData
9e60f0d8 1package Test::MooseX::Compiler;
2
3use strict;
4use warnings;
5use autodie;
6
7use Exporter qw( import );
8use File::Temp qw( tempdir );
9use Module::Runtime qw( module_notional_filename );
10use Path::Class qw( dir );
27543eae 11use Test::More;
9e60f0d8 12
13our @EXPORT_OK = qw(
27543eae 14 code_compiles_ok
9e60f0d8 15 save_class
f56affb6 16 save_fragment
9e60f0d8 17);
18
19my $Dir = dir( tempdir( CLEANUP => 1 ) );
20
f56affb6 21sub save_fragment {
9e60f0d8 22 my $class = shift;
23 my $code = shift;
24
6029ad7b 25 my $full_code = <<"EOF";
26package $class;
27
28use strict;
29use warnings;
30
31$code
32
331;
34EOF
35
f56affb6 36 save_class($class, $full_code);
37}
38
39sub save_class {
40 my $class = shift;
41 my $code = shift;
42
9e60f0d8 43 {
44 local $@;
f56affb6 45 eval $code;
9e60f0d8 46 die $@ if $@;
47 }
48
49 my $pm_file = module_notional_filename($class);
50 my $path = $Dir->file($pm_file);
51 $path->dir()->mkpath( 0, 0755 );
52
53 open my $fh, '>', $path;
f56affb6 54 print {$fh} $code;
9e60f0d8 55 close $fh;
56
57 $INC{$pm_file} = $path;
58
59 return $pm_file;
60}
61
27543eae 62sub code_compiles_ok {
63 my $code = shift;
64
65 local $Test::Builder::Level = $Test::Builder::Level + 1;
66
67 my $e;
68 {
69 local $@;
70 local $SIG{__DIE__};
71 eval $code;
72 $e = $@;
73 }
74
75 is( $e, q{}, 'code compiled ok' );
76}
77
9e60f0d8 781;