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