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