Work in progress on compiler. It's blowing up right now.
[gitmo/MooseX-Compiler.git] / t / constructor.t
CommitLineData
f56affb6 1use strict;
2use warnings;
3
4use lib 't/lib';
5
27543eae 6use Test::MooseX::Compiler qw( save_fragment code_compiles_ok );
f56affb6 7use Test::More 0.88;
8
9use MooseX::Compiler;
10
11{
12 my $code = <<'EOF';
13use Moose;
14
15sub foo { 42 }
16EOF
17
18 my $class = 'Test::Class1';
19 save_fragment( $class, $code );
20
21 my $compiler = MooseX::Compiler->new(
22 class => $class,
23 );
24
25 my $compiled = $compiler->compile_class();
5405fa51 26
f56affb6 27 like(
28 $compiled,
29 qr/sub new {.+\n}\n/s,
30 'compiled code has a constructor'
31 );
32
33 for my $module (qw( Scalar::Util Moose::Error::Util Carp )) {
34 like(
35 $compiled,
36 qr/^use \Q$module\E \(\);/m,
37 "compiled code loads $module"
38 );
39 }
40}
41
5405fa51 42{
43 my $code = <<'EOF';
44use Moose;
45
46has a1 => (
47 is => 'ro',
5405fa51 48 default => 42,
49);
50
27543eae 51sub foo { 84 }
5405fa51 52EOF
53
54 my $class = 'Test::Class2';
55 save_fragment( $class, $code );
56
57 my $compiler = MooseX::Compiler->new(
27543eae 58 class => $class,
59 rename_to => 'Test::Class2::Compiled',
5405fa51 60 );
61
62 my $compiled = $compiler->compile_class();
5405fa51 63
64 like(
65 $compiled,
66 qr/sub new {.+\n}\n/s,
67 'compiled code has a constructor'
68 );
27543eae 69
70 code_compiles_ok($compiled);
71
72 my $obj = Test::Class2::Compiled->new();
73 isa_ok( $obj, 'Test::Class2::Compiled', 'compiled object constructor' );
74 is( $obj->a1(), 'a1 attr defaults to 42' );
75 is( $obj->foo(), 84, 'foo method works' );
5405fa51 76}
77
f56affb6 78done_testing();