22
26
like($@, qr/Invalid package name/, ' ... croaks if package is undefined');
26
Test::MockModule->new('CGI', no_auto => 1);
27
ok(!$INC{'CGI.pm'}, '... no_auto prevents module being loaded');
30
my $mcgi = Test::MockModule->new('CGI');
31
ok($INC{'CGI.pm'}, '... module loaded if !$VERSION');
32
ok($mcgi->isa('Test::MockModule'), '... returns a Test::MockModule object');
33
my $mcgi2 = Test::MockModule->new('CGI');
35
"... returns existing object if there's already one for the package");
38
ok($mcgi->can('get_package'), 'get_package');
39
is($mcgi->get_package, 'CGI', '... returns the package name');
43
CGI->Vars; CGI->param;
45
ok($mcgi->can('mock'), 'mock()');
46
eval {$mcgi->mock(q[p-ram])};
48
like($@, qr/Invalid subroutine name: /,
49
'... dies if a subroutine name is invalid');
51
my $orig_param = \&CGI::param;
52
$mcgi->mock('param', sub {return qw(abc def)});
53
my @params = CGI::param();
54
is_deeply(\@params, ['abc', 'def'],
55
'... replaces the subroutine with a mocked sub');
57
$mcgi->mock('param' => undef);
58
@params = CGI::param();
59
is_deeply(\@params, [], '... which is an empty sub if !defined');
61
$mcgi->mock(param => 'The quick brown fox jumped over the lazy dog');
62
my $a2z = CGI::param();
63
is($a2z, 'The quick brown fox jumped over the lazy dog',
64
'... or a subroutine returning the supplied value');
67
$mcgi->mock(param => $ref);
68
@params = CGI::param();
70
'... given a reference, install a sub that returns said reference');
72
my $blessed_code = bless sub { return 'Hello World' }, 'FOO';
73
$mcgi->mock(param => $blessed_code);
74
@params = CGI::param();
75
is($params[0], 'Hello World', '... a blessed coderef is properly detected');
77
$mcgi->mock(Just => 'another', Perl => 'Hacker');
78
@params = (CGI::Just(), CGI::Perl());
79
is_deeply(\@params, ['another', 'Hacker'],
80
'... can mock multiple subroutines at a time');
84
ok($mcgi->can('original'), 'original()');
85
is($mcgi->original('param'), $orig_param,
86
'... returns the original subroutine');
88
local $SIG{__WARN__} = sub {$warn = shift};
89
$mcgi->original('Vars');
90
like($warn, qr/ is not mocked/, "... warns if a subroutine isn't mocked");
93
ok($mcgi->can('unmock'), 'unmock()');
94
eval {$mcgi->unmock('V@rs')};
95
like($@, qr/Invalid subroutine name/,
96
'... dies if the subroutine is invalid');
99
$mcgi->unmock('Vars');
100
like($warn, qr/ was not mocked/, "... warns if a subroutine isn't mocked");
102
$mcgi->unmock('param');
103
is(\&{"CGI::param"}, $orig_param, '... restores the original subroutine');
106
ok($mcgi->can('unmock_all'), 'unmock_all');
107
$mcgi->mock('Vars' => sub {1}, param => sub {2});
108
ok(CGI::Vars() == 1 && CGI::param() == 2,
109
'mock: can mock multiple subroutines');
110
my @orig = ($mcgi->original('Vars'), $mcgi->original('param'));
112
ok(\&CGI::Vars eq $orig[0] && \&CGI::param eq $orig[1],
113
'... removes all mocked subroutines');
116
ok($mcgi->can('is_mocked'), 'is_mocked');
117
ok(!$mcgi->is_mocked('param'), '... returns false for non-mocked sub');
118
$mcgi->mock('param', sub { return 'This sub is mocked' });
119
is(CGI::param(), 'This sub is mocked', '... mocked params');
120
ok($mcgi->is_mocked('param'), '... returns true for non-mocked sub');
30
Test::MockModule->new('ExampleModule', no_auto => 1);
31
ok(!$INC{'ExampleModule.pm'}, '... no_auto prevents module being loaded');
34
my $mcgi = Test::MockModule->new('ExampleModule');
35
ok($INC{'ExampleModule.pm'}, '... module loaded if !$VERSION');
36
ok($mcgi->isa('Test::MockModule'), '... returns a Test::MockModule object');
37
my $mcgi2 = Test::MockModule->new('ExampleModule');
39
"... returns existing object if there's already one for the package");
42
ok($mcgi->can('get_package'), 'get_package');
43
is($mcgi->get_package, 'ExampleModule', '... returns the package name');
47
ok($mcgi->can('mock'), 'mock()');
48
eval {$mcgi->mock(q[p-ram])};
50
like($@, qr/Invalid subroutine name: /,
51
'... dies if a subroutine name is invalid');
53
my $orig_param = \&ExampleModule::param;
54
$mcgi->mock('param', sub {return qw(abc def)});
55
my @params = ExampleModule::param();
56
is_deeply(\@params, ['abc', 'def'],
57
'... replaces the subroutine with a mocked sub');
59
$mcgi->mock('param' => undef);
60
@params = ExampleModule::param();
61
is_deeply(\@params, [], '... which is an empty sub if !defined');
63
$mcgi->mock(param => 'The quick brown fox jumped over the lazy dog');
64
my $a2z = ExampleModule::param();
65
is($a2z, 'The quick brown fox jumped over the lazy dog',
66
'... or a subroutine returning the supplied value');
69
$mcgi->mock(param => $ref);
70
@params = ExampleModule::param();
72
'... given a reference, install a sub that returns said reference');
74
my $blessed_code = bless sub { return 'Hello World' }, 'FOO';
75
$mcgi->mock(param => $blessed_code);
76
@params = ExampleModule::param();
77
is($params[0], 'Hello World', '... a blessed coderef is properly detected');
79
$mcgi->mock(Just => 'another', Perl => 'Hacker');
80
@params = (ExampleModule::Just(), ExampleModule::Perl());
81
is_deeply(\@params, ['another', 'Hacker'],
82
'... can mock multiple subroutines at a time');
86
ok($mcgi->can('original'), 'original()');
87
is($mcgi->original('param'), $orig_param,
88
'... returns the original subroutine');
90
local $SIG{__WARN__} = sub {$warn = shift};
91
$mcgi->original('Vars');
92
like($warn, qr/ is not mocked/, "... warns if a subroutine isn't mocked");
95
ok($mcgi->can('unmock'), 'unmock()');
96
eval {$mcgi->unmock('V@rs')};
97
like($@, qr/Invalid subroutine name/,
98
'... dies if the subroutine is invalid');
101
$mcgi->unmock('Vars');
102
like($warn, qr/ was not mocked/, "... warns if a subroutine isn't mocked");
104
$mcgi->unmock('param');
105
is(\&{"ExampleModule::param"}, $orig_param, '... restores the original subroutine');
108
ok($mcgi->can('unmock_all'), 'unmock_all');
109
$mcgi->mock('Vars' => sub {1}, param => sub {2});
110
ok(ExampleModule::Vars() == 1 && ExampleModule::param() == 2,
111
'mock: can mock multiple subroutines');
112
my @orig = ($mcgi->original('Vars'), $mcgi->original('param'));
114
ok(\&ExampleModule::Vars eq $orig[0] && \&ExampleModule::param eq $orig[1],
115
'... removes all mocked subroutines');
118
ok($mcgi->can('is_mocked'), 'is_mocked');
119
ok(!$mcgi->is_mocked('param'), '... returns false for non-mocked sub');
120
$mcgi->mock('param', sub { return 'This sub is mocked' });
121
is(ExampleModule::param(), 'This sub is mocked', '... mocked params');
122
ok($mcgi->is_mocked('param'), '... returns true for non-mocked sub');
123
isnt(CGI::param(), 'This sub is mocked',
124
'... params is unmocked when object goes out of scope');
125
isnt(ExampleModule::param(), 'This sub is mocked',
126
'... params is unmocked when object goes out of scope');
126
128
# test inherited methods
127
129
package Test_Parent;