Commit | Line | Data |
---|---|---|
9e60f0d8 | 1 | package Test::MooseX::Compiler; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use autodie; | |
6 | ||
7 | use Exporter qw( import ); | |
8 | use File::Temp qw( tempdir ); | |
9 | use Module::Runtime qw( module_notional_filename ); | |
10 | use Path::Class qw( dir ); | |
27543eae | 11 | use Test::More; |
9e60f0d8 | 12 | |
13 | our @EXPORT_OK = qw( | |
27543eae | 14 | code_compiles_ok |
9e60f0d8 | 15 | save_class |
f56affb6 | 16 | save_fragment |
9e60f0d8 | 17 | ); |
18 | ||
19 | my $Dir = dir( tempdir( CLEANUP => 1 ) ); | |
20 | ||
f56affb6 | 21 | sub save_fragment { |
9e60f0d8 | 22 | my $class = shift; |
23 | my $code = shift; | |
24 | ||
6029ad7b | 25 | my $full_code = <<"EOF"; |
26 | package $class; | |
27 | ||
28 | use strict; | |
29 | use warnings; | |
30 | ||
31 | $code | |
32 | ||
33 | 1; | |
34 | EOF | |
35 | ||
f56affb6 | 36 | save_class($class, $full_code); |
37 | } | |
38 | ||
39 | sub 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 | 62 | sub 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 | 78 | 1; |