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