Commit | Line | Data |
41888e7d |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 16; |
7 | use Test::Exception; |
8 | |
9 | |
10 | |
11 | =pod |
12 | |
13 | This test demonstrates the ability to extend |
14 | Mouse meta-level classes using Mouse itself. |
15 | |
16 | =cut |
17 | |
18 | { |
19 | package My::Meta::Class; |
20 | use Mouse; |
21 | |
22 | extends 'Mouse::Meta::Class'; |
23 | |
24 | around 'create_anon_class' => sub { |
25 | my $next = shift; |
26 | my ($self, %options) = @_; |
27 | $options{superclasses} = [ 'Mouse::Object' ] |
28 | unless exists $options{superclasses}; |
29 | $next->($self, %options); |
30 | }; |
31 | } |
32 | |
33 | my $anon = My::Meta::Class->create_anon_class(); |
34 | isa_ok($anon, 'My::Meta::Class'); |
35 | isa_ok($anon, 'Mouse::Meta::Class'); |
36 | isa_ok($anon, 'Class::MOP::Class'); |
37 | |
38 | is_deeply( |
39 | [ $anon->superclasses ], |
40 | [ 'Mouse::Object' ], |
41 | '... got the default superclasses'); |
42 | |
43 | { |
44 | package My::Meta::Attribute::DefaultReadOnly; |
45 | use Mouse; |
46 | |
47 | extends 'Mouse::Meta::Attribute'; |
48 | |
49 | around 'new' => sub { |
50 | my $next = shift; |
51 | my ($self, $name, %options) = @_; |
52 | $options{is} = 'ro' |
53 | unless exists $options{is}; |
54 | $next->($self, $name, %options); |
55 | }; |
56 | } |
57 | |
58 | { |
59 | my $attr = My::Meta::Attribute::DefaultReadOnly->new('foo'); |
60 | isa_ok($attr, 'My::Meta::Attribute::DefaultReadOnly'); |
61 | isa_ok($attr, 'Mouse::Meta::Attribute'); |
62 | isa_ok($attr, 'Class::MOP::Attribute'); |
63 | |
64 | ok($attr->has_reader, '... the attribute has a reader (as expected)'); |
65 | ok(!$attr->has_writer, '... the attribute does not have a writer (as expected)'); |
66 | ok(!$attr->has_accessor, '... the attribute does not have an accessor (as expected)'); |
67 | } |
68 | |
69 | { |
70 | my $attr = My::Meta::Attribute::DefaultReadOnly->new('foo', (is => 'rw')); |
71 | isa_ok($attr, 'My::Meta::Attribute::DefaultReadOnly'); |
72 | isa_ok($attr, 'Mouse::Meta::Attribute'); |
73 | isa_ok($attr, 'Class::MOP::Attribute'); |
74 | |
75 | ok(!$attr->has_reader, '... the attribute does not have a reader (as expected)'); |
76 | ok(!$attr->has_writer, '... the attribute does not have a writer (as expected)'); |
77 | ok($attr->has_accessor, '... the attribute does have an accessor (as expected)'); |
78 | } |
79 | |