04efe4b4d3b1821b1ebea74aaa828f4e94f4362d
[gitmo/MooseX-Emulate-Class-Accessor-Fast.git] / t / accessors.t
1 #!perl
2 use strict;
3 use Test::More tests => 33;
4 use Test::Exception;
5
6 use Class::MOP;
7
8 #1
9 require_ok("MooseX::Adopt::Class::Accessor::Fast");
10
11 my $class = "Testing::Class::Accessor::Fast";
12
13 {
14   my $infinite_loop_indicator = 0;
15   my $meta = Class::MOP::Class->create(
16     $class,
17     superclasses => ['Class::Accessor::Fast'],
18     methods => {
19       car => sub { shift->_car_accessor(@_); },
20       mar => sub { return "Overloaded"; },
21       test => sub {
22         die('Infinite loop detected') if $infinite_loop_indicator++;
23         $_[0]->_test_accessor((@_ > 1 ? @_ : ()));
24       }
25     }
26   );
27
28   $class->mk_accessors(qw( foo bar yar car mar test));
29   $class->mk_ro_accessors(qw(static unchanged));
30   $class->mk_wo_accessors(qw(sekret double_sekret));
31   $class->follow_best_practice;
32   $class->mk_accessors(qw( best));
33 }
34
35 my %attrs = map{$_->name => $_} $class->meta->compute_all_applicable_attributes;
36
37 #2
38 is(keys %attrs, 11, 'Correct number of attributes');
39
40 #3-12
41 ok(exists $attrs{$_}, "Attribute ${_} created")
42   for qw( foo bar yar car mar static unchanged sekret double_sekret best );
43
44 #13-21
45 ok($class->can("_${_}_accessor"), "Alias method (_${_}_accessor) for ${_} created")
46   for qw( foo bar yar car mar static unchanged sekret double_sekret );
47
48 #22-24
49 is( $attrs{$_}->accessor, $_, "Accessor ${_} created" )
50   for qw( foo bar yar);
51
52 #25,26
53 ok( !$attrs{$_}->has_accessor, "Accessor ${_} not created" )
54   for qw( car mar);
55
56 #27,28
57 is( $attrs{$_}->reader, $_, "Reader ${_} created")
58   for qw( static unchanged );
59
60 #29,30
61 is( $attrs{$_}->writer, $_, "Writer ${_} created")
62   for qw(sekret double_sekret);
63
64 #31,32
65 is( $attrs{'best'}->reader, 'get_best', "Reader get_best created");
66 is( $attrs{'best'}->writer, 'set_best', "Writer set_best created");
67
68 #33
69 lives_ok{ $class->new->test(1) } 'no auto-reference to accessors from aliases';