move tests over and fix up load_optional_class
[p5sagit/Class-C3-Componentised.git] / t / 01-basic.t
1 use strict;
2 use warnings;
3
4 use FindBin;
5 use Test::More;
6 use Test::Exception;
7 use Class::Inspector;
8
9 use lib "$FindBin::Bin/lib";
10
11 plan tests => 22;
12
13 BEGIN {
14   package TestPackage::A;
15   sub some_method {}
16 }
17
18 use_ok('MyModule');
19
20 MyModule->load_components('Foo');
21
22 # Clear down inc so ppl dont mess us up with installing modules that we
23 # expect not to exist
24 #@INC = ();
25 # This breaks Carp1.08/perl 5.10.0; bah
26
27 throws_ok { MyModule->load_components('+ClassC3ComponentFooThatShouldntExist'); } qr/^Can't locate ClassC3ComponentFooThatShouldntExist.pm in \@INC/;
28
29 is(MyModule->new->message, "Foo MyModule", "it worked");
30
31 ok( MyModule->ensure_class_found('MyModule::Plugin::Foo'),
32     'loaded package MyModule::Plugin::Foo was found' );
33 ok( !Class::Inspector->loaded('MyModule::OwnComponent'),
34     'MyModule::OwnComponent not loaded yet' );
35 ok( MyModule->ensure_class_found('MyModule::OwnComponent'),
36     'package MyModule::OwnComponent was found' );
37 ok( !Class::Inspector->loaded('MyModule::OwnComponent'),
38     'MyModule::OwnComponent not loaded by ensure_class_found()' );
39 ok( MyModule->ensure_class_found('TestPackage::A'),
40     'anonymous package TestPackage::A found' );
41 ok( !MyModule->ensure_class_found('FAKE::WONT::BE::FOUND'),
42         'fake package not found' );
43
44 # Test load_optional_class
45 my $retval = eval { MyModule->load_optional_class('ANOTHER::FAKE::PACKAGE') };
46 ok( !$@, 'load_optional_class on a nonexistent class did not throw' );
47 ok( !$retval, 'nonexistent package not loaded' );
48 $retval = eval { MyModule->load_optional_class('MyModule::OwnComponent') };
49 ok( !$@, 'load_optional_class on an existing class did not throw' );
50 ok( $retval, 'MyModule::OwnComponent loaded' );
51 eval { MyModule->load_optional_class('MyModule::ErrorComponent') };
52 like( $@, qr/did not return a true value/,
53       'MyModule::ErrorComponent threw ok' );
54
55 # Simulate a PAR environment
56
57   my @code;
58   local @INC = @INC;
59   unshift @INC, sub {
60     if ($_[1] eq 'VIRTUAL/PAR/PACKAGE.pm') {
61       return (sub { return 0 unless @code; $_ = shift @code; 1; } );
62     }
63     else {
64       return ();
65     }
66   };
67
68   $retval = eval { MyModule->load_optional_class('FAKE::PAR::PACKAGE') };
69   ok( !$@, 'load_optional_class on a nonexistent PAR class did not throw' );
70   ok( !$retval, 'nonexistent PAR package not loaded' );
71   
72
73   # simulate a class which does load but does not return true
74   @code = (
75     q/package VIRTUAL::PAR::PACKAGE;/,
76     q/0;/,
77   );
78
79   $retval = eval { MyModule->load_optional_class('VIRTUAL::PAR::PACKAGE') };
80   ok( $@, 'load_optional_class of a no-true-returning PAR module did throw' );
81   ok( !$retval, 'no-true-returning PAR package not loaded' );
82   
83   # simulate a normal class (no one adjusted %INC so it will be tried again
84   @code = (
85     q/package VIRTUAL::PAR::PACKAGE;/,
86     q/1;/,
87   );
88
89   $retval = eval { MyModule->load_optional_class('VIRTUAL::PAR::PACKAGE') };
90   ok( !$@, 'load_optional_class of a PAR module did not throw' );
91   ok( $retval, 'PAR package "loaded"' );
92   
93   # see if we can still load stuff with the coderef present
94   $retval = eval { MyModule->load_optional_class('Class::C3') };
95   ok( !$@, 'load_optional_class did not throw' ) || diag $@;
96   ok( $retval, 'Class::C3 loaded' );
97 }