summaryrefslogtreecommitdiff
path: root/t/003-load-class.t
blob: 325b63e805d895c2414f390744007104fc875026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use strict;
use warnings;
use Test::More 0.88;
use lib 't/lib';
use Test::Class::Load ':all';
use Test::Fatal;

is( load_class('Class::Load::OK'), 'Class::Load::OK', 'loaded class OK' );
is( $Class::Load::ERROR, undef, 'ERROR is undef' );

like(
    exception {
        load_class('Class::Load::Nonexistent');
    },
    qr{^Can't locate Class/Load/Nonexistent.pm in \@INC},
    'threw exception for nonexistent class'
);

like(
    $Class::Load::ERROR,
    qr{^Can't locate Class/Load/Nonexistent.pm in \@INC},
    'ERROR message for nonexistent class',
);

ok( load_class('Class::Load::OK'), 'loaded class OK' );
is( $Class::Load::ERROR, undef, 'ERROR is undef' );

like(
    exception {
        load_class('Class::Load::SyntaxError');
    },
    qr{^Missing right curly or square bracket at },
    'exception contains syntax error message'
);

like(
    $Class::Load::ERROR,
    qr{^Missing right curly or square bracket at },
    'ERROR contains syntax error message'
);

ok( is_class_loaded('Class::Load::OK') );
ok( !is_class_loaded('Class::Load::Nonexistent') );
ok( !is_class_loaded('Class::Load::SyntaxError') );

do {

    package Class::Load::Inlined;
    sub inlined { 1 }
};

is(
    load_class('Class::Load::Inlined'),
    'Class::Load::Inlined',
    'loaded class Inlined'
);
is( $Class::Load::ERROR, undef );
ok( is_class_loaded('Class::Load::Inlined') );

like(
    exception {
        load_class( 'Class::Load::VersionCheck', { -version => 43 } );
    },
    qr/^Class::Load::VersionCheck version 43 required/,
    'got expected error for load_class with explicit version'
);

is(
    load_class( 'Class::Load::VersionCheck', { -version => 41 } ),
    'Class::Load::VersionCheck',
    'loaded class with version check'
);

is(
    load_class( 'Class::Load::VersionCheck2', { -version => 41 } ),
    'Class::Load::VersionCheck2',
    'loaded class with version check'
);

like(
    exception {
        load_class( 'Class::Load::VersionCheck2', { -version => 43 } );
    },
    qr/^Class::Load::VersionCheck2 version 43 required/,
    'got expected error for load_class with explicit version (after class has been loaded into memory)'
);

like(
    exception {
        load_class('__PACKAGE__');
    },
    qr/__PACKAGE__\.pm.*\@INC/,
    'errors sanely on __PACKAGE__.pm'
);

done_testing;