Commit | Line | Data |
3f7759c1 |
1 | use strict; |
2 | use warnings; |
7716a8f9 |
3 | use Test::More tests => 32; |
3f7759c1 |
4 | use Test::Exception; |
5 | |
6 | require Class::MOP; |
7 | use lib 't/lib'; |
8 | |
9 | ok(!Class::MOP::is_class_loaded(), "is_class_loaded with no argument returns false"); |
10 | ok(!Class::MOP::is_class_loaded(''), "can't load the empty class"); |
11 | ok(!Class::MOP::is_class_loaded(\"foo"), "can't load a class name reference??"); |
12 | |
2c0fb064 |
13 | ok(!Class::MOP::_is_valid_class_name(undef), 'undef is not a valid class name'); |
14 | ok(!Class::MOP::_is_valid_class_name(''), 'empty string is not a valid class name'); |
15 | ok(!Class::MOP::_is_valid_class_name(\"foo"), 'a reference is not a valid class name'); |
16 | ok(!Class::MOP::_is_valid_class_name('bogus name'), q{'bogus name' is not a valid class name}); |
17 | ok(Class::MOP::_is_valid_class_name('Foo'), q{'Foo' is a valid class name}); |
18 | ok(Class::MOP::_is_valid_class_name('Foo::Bar'), q{'Foo::Bar' is a valid class name}); |
19 | ok(Class::MOP::_is_valid_class_name('Foo_::Bar2'), q{'Foo_::Bar2' is a valid class name}); |
b3b7a216 |
20 | throws_ok { Class::MOP::load_class('bogus name') } qr/Invalid class name \(bogus name\)/; |
3f7759c1 |
21 | |
9bbf7fe2 |
22 | throws_ok { |
23 | Class::MOP::load_class('__PACKAGE__') |
24 | } qr/__PACKAGE__\.pm.*\@INC/, 'errors sanely on __PACKAGE__.pm'; |
25 | |
7716a8f9 |
26 | Class::MOP::load_class('BinaryTree'); |
3f7759c1 |
27 | can_ok('BinaryTree' => 'traverse'); |
28 | |
29 | do { |
30 | package Class; |
31 | sub method {} |
32 | }; |
33 | |
4c7e2bf4 |
34 | |
7716a8f9 |
35 | { |
36 | local $@; |
37 | eval { Class::MOP::load_class('Class') }; |
38 | ok( ! $@, 'load_class never dies' ); |
39 | } |
4c7e2bf4 |
40 | |
41 | ok( !Class::MOP::does_metaclass_exist("Class"), "no metaclass for non MOP class" ); |
3f7759c1 |
42 | |
43 | throws_ok { |
44 | Class::MOP::load_class('FakeClassOhNo'); |
202ccce0 |
45 | } |
46 | qr/Can't locate /; |
3f7759c1 |
47 | |
48 | throws_ok { |
49 | Class::MOP::load_class('SyntaxError'); |
202ccce0 |
50 | } |
51 | qr/Missing right curly/; |
3f7759c1 |
52 | |
fea44045 |
53 | throws_ok { |
44da14be |
54 | delete $INC{'SyntaxError.pm'}; |
55 | Class::MOP::load_first_existing_class( |
56 | 'FakeClassOhNo', 'SyntaxError', 'Class' |
57 | ); |
58 | } |
2beec805 |
59 | qr/Missing right curly/, |
44da14be |
60 | 'load_first_existing_class does not pass over an existing (bad) module'; |
61 | |
62 | throws_ok { |
fea44045 |
63 | Class::MOP::load_class('This::Does::Not::Exist'); |
202ccce0 |
64 | } |
65 | qr/Could not load class \(This::Does::Not::Exist\) because :/, |
66 | 'Many Moose tests rely on the exact formatting of this error'; |
fea44045 |
67 | |
9e275e86 |
68 | { |
69 | package Other; |
70 | use constant foo => "bar"; |
71 | } |
72 | |
b3b7a216 |
73 | lives_ok { |
2c0fb064 |
74 | ok(Class::MOP::is_class_loaded("Other"), 'is_class_loaded(Other)'); |
b3b7a216 |
75 | } |
76 | "a class with just constants is still a class"; |
4c7e2bf4 |
77 | |
78 | { |
79 | package Lala; |
80 | use metaclass; |
81 | } |
82 | |
5a24cf8a |
83 | lives_ok { |
1d8153bd |
84 | is(Class::MOP::load_first_existing_class("Lala", "Does::Not::Exist"), "Lala", 'load_first_existing_class 1/2 params ok, class name returned'); |
85 | is(Class::MOP::load_first_existing_class("Does::Not::Exist", "Lala"), "Lala", 'load_first_existing_class 2/2 params ok, class name returned'); |
5a24cf8a |
86 | } 'load_classes works'; |
1d8153bd |
87 | |
5a24cf8a |
88 | throws_ok { |
063ad0c5 |
89 | Class::MOP::load_first_existing_class("Does::Not::Exist", "Also::Does::Not::Exist") |
5a24cf8a |
90 | } qr/Could not load class \(Does::Not::Exist.*Could not load class \(Also::Does::Not::Exist/s, 'Multiple non-existant classes cause exception'; |
91 | |
b8575233 |
92 | { |
93 | sub whatever { |
94 | TestClassLoaded::this_method_does_not_even_exist(); |
95 | } |
96 | |
97 | ok( ! Class::MOP::is_class_loaded('TestClassLoaded'), |
98 | 'the mere mention of TestClassLoaded in the whatever sub does not make us think it has been loaded' ); |
99 | } |
5a24cf8a |
100 | |
7f820be6 |
101 | { |
102 | require TestClassLoaded::Sub; |
103 | ok( ! Class::MOP::is_class_loaded('TestClassLoaded'), |
104 | 'requiring TestClassLoaded::Sub does not make us think TestClassLoaded is loaded' ); |
105 | } |
106 | |
107 | { |
108 | require TestClassLoaded; |
109 | ok( Class::MOP::is_class_loaded('TestClassLoaded'), |
110 | 'We see that TestClassLoaded is loaded after requiring it (it has methods but no $VERSION or @ISA)' ); |
111 | } |
112 | |
113 | { |
114 | require TestClassLoaded2; |
115 | ok( Class::MOP::is_class_loaded('TestClassLoaded2'), |
116 | 'We see that TestClassLoaded2 is loaded after requiring it (it has a $VERSION but no methods or @ISA)' ); |
117 | } |
118 | |
119 | { |
120 | require TestClassLoaded3; |
121 | ok( Class::MOP::is_class_loaded('TestClassLoaded3'), |
122 | 'We see that TestClassLoaded3 is loaded after requiring it (it has an @ISA but no methods or $VERSION)' ); |
123 | } |
d9d8a21b |
124 | |
125 | { |
126 | { |
127 | package Not::Loaded; |
128 | our @ISA; |
129 | } |
130 | |
131 | ok( ! Class::MOP::is_class_loaded('Not::Loaded'), |
132 | 'the mere existence of an @ISA for a package does not mean a class is loaded' ); |
133 | } |
134 | |
135 | { |
136 | { |
137 | package Loaded::Ish; |
138 | our @ISA = 'Foo'; |
139 | } |
140 | |
141 | ok( Class::MOP::is_class_loaded('Loaded::Ish'), |
142 | 'an @ISA with members does mean a class is loaded' ); |
143 | } |
144 | |