line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Test::VirtualModule |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 DESCRIPTION |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
This package allows you to generate perl modules on flight for unit-testing. This package is usable |
8
|
|
|
|
|
|
|
when you have complex environment with some tricky perl modules inside of it, which can't be installed without full environment. |
9
|
|
|
|
|
|
|
And you unable to write unit tests. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
This module allows you to cheat and tell to perl that these modules already loaded. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# load virtual module |
16
|
|
|
|
|
|
|
use Test::VirtualModule qw/BlahBlahBlah::FooBar/; |
17
|
|
|
|
|
|
|
# import mocked module, it's ok |
18
|
|
|
|
|
|
|
use BlahBlahBlah::FooBar; |
19
|
|
|
|
|
|
|
# Mock constructor |
20
|
|
|
|
|
|
|
Test::VirtualModule->mock_sub('BlahBlahBlah::FooBar', |
21
|
|
|
|
|
|
|
new => sub { |
22
|
|
|
|
|
|
|
my $self = {}; |
23
|
|
|
|
|
|
|
bless $self, 'BlahBlahBlah::FooBar'; |
24
|
|
|
|
|
|
|
return $self; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
# create object |
28
|
|
|
|
|
|
|
my $object = BlahBlahBlah::FooBar->new(); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
That's all. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=over |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
package Test::VirtualModule; |
37
|
2
|
|
|
2
|
|
1261
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
73
|
|
38
|
2
|
|
|
2
|
|
8
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
48
|
|
39
|
2
|
|
|
2
|
|
17
|
use Carp; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
626
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $VERSION = 0.01; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub import { |
44
|
2
|
|
|
2
|
|
16
|
my ($caller, @module_list) = @_; |
45
|
|
|
|
|
|
|
|
46
|
2
|
|
|
|
|
5
|
my %hash = map{s/\s+//gs;($_=>1)}@module_list; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
47
|
2
|
100
|
|
|
|
18
|
return unless %hash; |
48
|
|
|
|
|
|
|
unshift @INC, sub { |
49
|
12
|
|
|
12
|
|
96184
|
my ($self, $package) = @_; |
50
|
12
|
|
|
|
|
32
|
$package =~ s|/|::|gs; |
51
|
12
|
|
|
|
|
32
|
$package =~ s|\.pm||s; |
52
|
12
|
100
|
|
|
|
6273
|
return unless $hash{$package}; |
53
|
1
|
|
|
|
|
8
|
my $text = qq|package $package;1;|; |
54
|
1
|
|
|
1
|
|
26
|
open my $fh, '<', \$text; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
4
|
|
55
|
1
|
|
|
|
|
531
|
return $fh; |
56
|
1
|
|
|
|
|
27
|
}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item B |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Alows you to mock subroutines of specified module. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Test::VirtualModule->mock_sub( |
65
|
|
|
|
|
|
|
'SomeModule', |
66
|
|
|
|
|
|
|
get_property => sub { |
67
|
|
|
|
|
|
|
return 1; |
68
|
|
|
|
|
|
|
}, |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
SomeModule->get_property(1); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub mock_sub { |
75
|
1
|
|
|
1
|
1
|
398
|
my ($caller, $module, %subs) = @_; |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
2
|
|
12
|
no warnings qw/redefine/; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
96
|
|
78
|
2
|
|
|
2
|
|
8
|
no strict qw/refs/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
258
|
|
79
|
|
|
|
|
|
|
|
80
|
1
|
|
|
|
|
5
|
for my $name (keys %subs) { |
81
|
1
|
50
|
33
|
|
|
10
|
if (!$subs{$name} || ref $subs{$name} ne 'CODE') { |
82
|
0
|
|
|
|
|
0
|
croak "Wrong args"; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
2
|
*{$module . "::$name"} = $subs{$name}; |
|
1
|
|
|
|
|
8
|
|
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
1
|
|
|
|
|
4
|
return 1; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=back |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |