Add all my extra tests, and fix some of them
[gitmo/MooseX-Emulate-Class-Accessor-Fast.git] / t / accessors.t
CommitLineData
c5a105b3 1#!perl
2use strict;
30cbeb5e 3use Test::More tests => 33;
4use Test::Exception;
5
6use Class::MOP;
c5a105b3 7
8#1
9require_ok("MooseX::Adopt::Class::Accessor::Fast");
10
11my $class = "Testing::Class::Accessor::Fast";
12
13{
30cbeb5e 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 );
c5a105b3 27
330a3f6b 28 $meta->make_immutable;
30cbeb5e 29 $class->mk_accessors(qw( foo bar yar car mar test));
c5a105b3 30 $class->mk_ro_accessors(qw(static unchanged));
31 $class->mk_wo_accessors(qw(sekret double_sekret));
32 $class->follow_best_practice;
33 $class->mk_accessors(qw( best));
34}
35
36my %attrs = map{$_->name => $_} $class->meta->compute_all_applicable_attributes;
37
38#2
30cbeb5e 39is(keys %attrs, 11, 'Correct number of attributes');
c5a105b3 40
41#3-12
42ok(exists $attrs{$_}, "Attribute ${_} created")
43 for qw( foo bar yar car mar static unchanged sekret double_sekret best );
44
45#13-21
30cbeb5e 46ok($class->can("_${_}_accessor"), "Alias method (_${_}_accessor) for ${_} created")
c5a105b3 47 for qw( foo bar yar car mar static unchanged sekret double_sekret );
48
49#22-24
50is( $attrs{$_}->accessor, $_, "Accessor ${_} created" )
51 for qw( foo bar yar);
52
53#25,26
54ok( !$attrs{$_}->has_accessor, "Accessor ${_} not created" )
55 for qw( car mar);
56
57#27,28
58is( $attrs{$_}->reader, $_, "Reader ${_} created")
59 for qw( static unchanged );
60
61#29,30
62is( $attrs{$_}->writer, $_, "Writer ${_} created")
63 for qw(sekret double_sekret);
64
65#31,32
66is( $attrs{'best'}->reader, 'get_best', "Reader get_best created");
67is( $attrs{'best'}->writer, 'set_best', "Writer set_best created");
30cbeb5e 68
69#33
70lives_ok{ $class->new->test(1) } 'no auto-reference to accessors from aliases';