Refactor testing code a class before and after it's regenerated into a function
[gitmo/MooseX-Antlers.git] / t / one.t
CommitLineData
a39b801f 1use strict;
2use warnings FATAL => 'all';
5bd097e7 3use MooseX::Antlers::Compiler;
a39b801f 4use lib 't/lib';
5use Test::More;
064721e6 6use Test::Exception;
7use Class::Unload;
3691d03f 8use IO::All;
9use Data::Dumper::Concise;
10use Data::Dump::Streamer;
5bd097e7 11
3691d03f 12sub dump_meta {
13 my $meta = $_[0];
14 local $meta->{methods}{meta};
15 join '', Dump($meta);
16}
17
064721e6 18sub foo_called {
19 &cmp_ok(One->get_called_foo, '==', @_); # cmp_ok has a $$$;$ proto
20}
21
22sub test_One {
23
24 ok(One->can('foo'), 'foo accessor installed');
25
26 dies_ok { One->new } 'foo is required';
27
28 foo_called(0 => 'trigger not called yet');
29
30 my $one = One->new(foo => 1);
31
32 foo_called(1 => 'trigger called once (constructor)');
33
34 cmp_ok($one->foo, '==', 1, 'read ok');
35
36 foo_called(1 => 'trigger not called for read');
37
38 $one->foo(2);
39
40 foo_called(2 => 'trigger called for setter');
41}
42
a80b827d 43my $class = 'One';
44test_class($class, \&test_One);
064721e6 45
a80b827d 46sub test_class {
47 my ($class, $test) = @_;
064721e6 48
a80b827d 49 my $compiler = MooseX::Antlers::Compiler->load_with_compiler($class);
a39b801f 50
a80b827d 51 # FIXME - foo
52 my %orig_src = map +($_ => join '', Dump($class->can($_))), qw(new DESTROY foo);
a39b801f 53
a80b827d 54 $class->meta->get_method_list; # fill cache
064721e6 55
a80b827d 56 my $orig_meta = dump_meta $class->meta;
064721e6 57
a80b827d 58 $test->($class);
064721e6 59
a80b827d 60 my $compiled = $compiler->compiled_source;
de39aa89 61
a80b827d 62 #warn $compiled; done_testing; exit 0;
064721e6 63
a80b827d 64 Class::Unload->unload($class);
65 Class::MOP::remove_metaclass_by_name($class);
3691d03f 66
a80b827d 67 io("/tmp/$class.pmc")->print($compiled);
064721e6 68
a80b827d 69 require "/tmp/$class.pmc";
064721e6 70
a80b827d 71 #eval "no warnings; $compiled";
72
73 #die "Shit. failed.\n\n${compiled}\n\nError: $@" if $@;
064721e6 74
a80b827d 75 my %compiled_src = map +($_ => join '', Dump($class->can($_))), qw(new DESTROY foo);
3691d03f 76
a80b827d 77 # FIXME - foo
78 foreach my $method (qw(new DESTROY foo)) {
79 is($compiled_src{$method}, $orig_src{$method}, "${method} restored ok");
80 }
a39b801f 81
a80b827d 82 my $compiled_meta = dump_meta $class->meta;
3691d03f 83
a80b827d 84 $test->($class);
3691d03f 85
a80b827d 86 #io('orig')->print($orig_meta);
87 #io('comp')->print($compiled_meta);
a39b801f 88
a80b827d 89 is($orig_meta, $compiled_meta, 'metaclass restored ok');
90
91 Class::Unload->unload($class);
92 Class::MOP::remove_metaclass_by_name($class);
93}
a39b801f 94
95done_testing;