Skip tests for strict constructor on Moose
[gitmo/Mouse.git] / t / 001_mouse / 100-meta-class.t
CommitLineData
c3398f5b 1#!/usr/bin/env perl
2use strict;
3use warnings;
612d3e1a 4use Test::More tests => 26;
8e64d0fa 5use Test::Exception;
6{
c3398f5b 7 package Class;
8 use Mouse;
8e64d0fa 9 use Scalar::Util qw(blessed weaken); # import external functions
c3398f5b 10
11 has pawn => (
12 is => 'rw',
13 predicate => 'has_pawn',
14 );
15
8e64d0fa 16 use constant MY_CONST => 42;
17
18 sub stub;
19 sub stub_with_attr :method;
20
c3398f5b 21 no Mouse;
8e64d0fa 22}
23{
24 package Child;
25 use Mouse;
26 use Carp qw(carp croak); # import extenral functions
27
28 extends 'Class';
29
30 has bishop => (
31 is => 'rw',
32 );
33
34 sub child_method{ }
35}
c3398f5b 36
37my $meta = Class->meta;
306290e8 38isa_ok($meta, 'Mouse::Meta::Class');
c3398f5b 39
40is_deeply([$meta->superclasses], ['Mouse::Object'], "correctly inherting from Mouse::Object");
41
42my $meta2 = Class->meta;
43is($meta, $meta2, "same metaclass instance");
44
8e64d0fa 45can_ok($meta, qw(
46 name meta
47 has_attribute get_attribute get_attribute_list get_all_attributes
48 has_method get_method get_method_list get_all_methods
49));
c3398f5b 50
986ff64a 51ok($meta->has_attribute('pawn'));
c3398f5b 52my $attr = $meta->get_attribute('pawn');
306290e8 53isa_ok($attr, 'Mouse::Meta::Attribute');
c3398f5b 54is($attr->name, 'pawn', 'got the correct attribute');
55
c68b4110 56my $list = [$meta->get_attribute_list];
57is_deeply($list, [ 'pawn' ], "attribute list");
58
986ff64a 59ok(!$meta->has_attribute('nonexistent_attribute'));
60
8e64d0fa 61ok($meta->has_method('pawn'));
62lives_and{
63 ok($meta->get_method('pawn'));
64 is($meta->get_method('pawn')->name, 'pawn');
65 is($meta->get_method('pawn')->package_name, 'Class');
66};
67
68is( join(' ', sort $meta->get_method_list),
69 join(' ', sort qw(meta pawn has_pawn MY_CONST stub stub_with_attr))
70);
71
72eval q{
c3398f5b 73 package Class;
74 use Mouse;
75 no Mouse;
8e64d0fa 76};
c3398f5b 77
78my $meta3 = Class->meta;
79is($meta, $meta3, "same metaclass instance, even if use Mouse is performed again");
80
81is($meta->name, 'Class', "name for the metaclass");
82
c3398f5b 83
84my $child_meta = Child->meta;
306290e8 85isa_ok($child_meta, 'Mouse::Meta::Class');
c3398f5b 86
87isnt($meta, $child_meta, "different metaclass instances for the two classes");
88
89is_deeply([$child_meta->superclasses], ['Class'], "correct superclasses");
8e64d0fa 90
91
92ok($child_meta->has_attribute('bishop'));
93ok($child_meta->has_method('child_method'));
94
95
96is( join(' ', sort $child_meta->get_method_list),
97 join(' ', sort qw(meta bishop child_method))
98);
612d3e1a 99
100can_ok($child_meta, 'find_method_by_name');
101is $child_meta->find_method_by_name('child_method')->fully_qualified_name, 'Child::child_method';
102is $child_meta->find_method_by_name('pawn')->fully_qualified_name, 'Class::pawn';
103
104
0fc9e959 105is( join(' ', sort map{ $_->fully_qualified_name } grep{ $_->package_name ne 'Mouse::Object' } $child_meta->get_all_methods),
106 join(' ', sort qw(
107 Child::bishop Child::child_method Child::meta
108
109 Class::MY_CONST Class::has_pawn Class::pawn Class::stub Class::stub_with_attr
110 ))
111);
112