Blame t/mockmodule.t

Packit 11f908
use warnings;
Packit 11f908
use strict;
Packit 11f908
Packit 11f908
use Test::More;
Packit 11f908
Packit 11f908
use lib "t/lib";
Packit 11f908
Packit 11f908
BEGIN {
Packit 11f908
	use_ok('Test::MockModule') or BAIL_OUT "Could not load Test::MockModule. Giving up";
Packit 11f908
}
Packit 11f908
Packit 11f908
package Test_Package;
Packit 11f908
our $VERSION=1;
Packit 11f908
sub listify {
Packit 11f908
	my ($lower, $upper) = @_;
Packit 11f908
	return ($lower .. $upper);
Packit 11f908
}
Packit 11f908
package main;
Packit 11f908
Packit 11f908
# new()
Packit 11f908
ok(Test::MockModule->can('new'), 'new()');
Packit 11f908
eval {Test::MockModule->new('Test::MockModule')};
Packit 11f908
like($@, qr/Cannot mock Test::MockModule/, '... cannot mock itself');
Packit 11f908
eval {Test::MockModule->new('12Monkeys')};
Packit 11f908
like($@, qr/Invalid package name/, ' ... croaks if package looks invalid');
Packit 11f908
eval {Test::MockModule->new()};
Packit 11f908
like($@, qr/Invalid package name/, ' ... croaks if package is undefined');
Packit 11f908
Packit 11f908
{
Packit 11f908
	{
Packit 11f908
		Test::MockModule->new('ExampleModule', no_auto => 1);
Packit 11f908
		ok(!$INC{'ExampleModule.pm'}, '... no_auto prevents module being loaded');
Packit 11f908
	}
Packit 11f908
Packit 11f908
	my $mcgi = Test::MockModule->new('ExampleModule');
Packit 11f908
	ok($INC{'ExampleModule.pm'}, '... module loaded if !$VERSION');
Packit 11f908
	ok($mcgi->isa('Test::MockModule'), '... returns a Test::MockModule object');
Packit 11f908
	my $mcgi2 = Test::MockModule->new('ExampleModule');
Packit 11f908
	is($mcgi, $mcgi2,
Packit 11f908
		"... returns existing object if there's already one for the package");
Packit 11f908
Packit 11f908
	# get_package()
Packit 11f908
	ok($mcgi->can('get_package'), 'get_package');
Packit 11f908
	is($mcgi->get_package, 'ExampleModule', '... returns the package name');
Packit 11f908
Packit 11f908
	# mock()
Packit 11f908
Packit 11f908
	ok($mcgi->can('mock'), 'mock()');
Packit 11f908
	eval {$mcgi->mock(q[p-ram])};
Packit 11f908
Packit 11f908
	like($@, qr/Invalid subroutine name: /,
Packit 11f908
		'... dies if a subroutine name is invalid');
Packit 11f908
Packit 11f908
	my $orig_param = \&ExampleModule::param;
Packit 11f908
	$mcgi->mock('param', sub {return qw(abc def)});
Packit 11f908
	my @params = ExampleModule::param();
Packit 11f908
	is_deeply(\@params, ['abc', 'def'],
Packit 11f908
		'... replaces the subroutine with a mocked sub');
Packit 11f908
Packit 11f908
	$mcgi->mock('param' => undef);
Packit 11f908
	@params = ExampleModule::param();
Packit 11f908
	is_deeply(\@params, [], '... which is an empty sub if !defined');
Packit 11f908
Packit 11f908
	$mcgi->mock(param => 'The quick brown fox jumped over the lazy dog');
Packit 11f908
	my $a2z = ExampleModule::param();
Packit 11f908
	is($a2z, 'The quick brown fox jumped over the lazy dog',
Packit 11f908
		'... or a subroutine returning the supplied value');
Packit 11f908
Packit 11f908
	my $ref = [1,2,3];
Packit 11f908
	$mcgi->mock(param => $ref);
Packit 11f908
	@params = ExampleModule::param();
Packit 11f908
	is($params[0], $ref,
Packit 11f908
		'... given a reference, install a sub that returns said reference');
Packit 11f908
Packit 11f908
	my $blessed_code = bless sub { return 'Hello World' }, 'FOO';
Packit 11f908
	$mcgi->mock(param => $blessed_code);
Packit 11f908
	@params = ExampleModule::param();
Packit 11f908
	is($params[0], 'Hello World', '... a blessed coderef is properly detected');
Packit 11f908
Packit 11f908
	$mcgi->mock(Just => 'another', Perl => 'Hacker');
Packit 11f908
	@params = (ExampleModule::Just(), ExampleModule::Perl());
Packit 11f908
	is_deeply(\@params, ['another', 'Hacker'],
Packit 11f908
		'... can mock multiple subroutines at a time');
Packit 11f908
Packit 11f908
Packit 11f908
	# original()
Packit 11f908
	ok($mcgi->can('original'), 'original()');
Packit 11f908
	is($mcgi->original('param'), $orig_param,
Packit 11f908
		'... returns the original subroutine');
Packit 11f908
	my ($warn);
Packit 11f908
	local $SIG{__WARN__} = sub {$warn = shift};
Packit 11f908
	$mcgi->original('Vars');
Packit 11f908
	like($warn, qr/ is not mocked/, "... warns if a subroutine isn't mocked");
Packit 11f908
Packit 11f908
	# unmock()
Packit 11f908
	ok($mcgi->can('unmock'), 'unmock()');
Packit 11f908
	eval {$mcgi->unmock('V@rs')};
Packit 11f908
	like($@, qr/Invalid subroutine name/,
Packit 11f908
		'... dies if the subroutine is invalid');
Packit 11f908
Packit 11f908
	$warn = '';
Packit 11f908
	$mcgi->unmock('Vars');
Packit 11f908
	like($warn, qr/ was not mocked/, "... warns if a subroutine isn't mocked");
Packit 11f908
Packit 11f908
	$mcgi->unmock('param');
Packit 11f908
	is(\&{"ExampleModule::param"}, $orig_param, '... restores the original subroutine');
Packit 11f908
Packit 11f908
	# unmock_all()
Packit 11f908
	ok($mcgi->can('unmock_all'), 'unmock_all');
Packit 11f908
	$mcgi->mock('Vars' => sub {1}, param => sub {2});
Packit 11f908
	ok(ExampleModule::Vars() == 1 && ExampleModule::param() == 2,
Packit 11f908
		'mock: can mock multiple subroutines');
Packit 11f908
	my @orig = ($mcgi->original('Vars'), $mcgi->original('param'));
Packit 11f908
	$mcgi->unmock_all();
Packit 11f908
	ok(\&ExampleModule::Vars eq $orig[0] && \&ExampleModule::param eq $orig[1],
Packit 11f908
		'... removes all mocked subroutines');
Packit 11f908
Packit 11f908
	# is_mocked()
Packit 11f908
	ok($mcgi->can('is_mocked'), 'is_mocked');
Packit 11f908
	ok(!$mcgi->is_mocked('param'), '... returns false for non-mocked sub');
Packit 11f908
	$mcgi->mock('param', sub { return 'This sub is mocked' });
Packit 11f908
	is(ExampleModule::param(), 'This sub is mocked', '... mocked params');
Packit 11f908
	ok($mcgi->is_mocked('param'), '... returns true for non-mocked sub');
Packit 11f908
Packit 11f908
	# noop()
Packit 11f908
	is(ExampleModule::cookie(), 'choc-chip', 'cookie does default behaviour');
Packit 11f908
	$mcgi->noop('cookie');
Packit 11f908
	ok($mcgi->is_mocked('cookie'), 'cookie is mocked using noop');
Packit 11f908
	$mcgi->unmock('cookie');
Packit 11f908
	$mcgi->unmock('Vars');
Packit 11f908
	$mcgi->noop('cookie', 'Vars');
Packit 11f908
	is(ExampleModule::cookie(), 1, 'now cookie does nothing');
Packit 11f908
	is(ExampleModule::Vars(), 1, 'now Vars does nothing');
Packit 11f908
}
Packit 11f908
Packit 11f908
isnt(ExampleModule::param(), 'This sub is mocked',
Packit 11f908
	'... params is unmocked when object goes out of scope');
Packit 11f908
Packit 11f908
# test inherited methods
Packit 11f908
package Test_Parent;
Packit 11f908
sub method { 1 }
Packit 11f908
package Test_Child;
Packit 11f908
@Test_Child::ISA = 'Test_Parent';
Packit 11f908
package main;
Packit 11f908
Packit 11f908
my $test_mock = Test::MockModule->new('Test_Child', no_auto => 1);
Packit 11f908
ok(Test_Child->can('method'), 'test class inherits from parent');
Packit 11f908
$test_mock->mock('method' => sub {2});
Packit 11f908
is(Test_Child->method, 2, 'mocked subclass method');
Packit 11f908
$test_mock->unmock('method');
Packit 11f908
ok(Test_Child->can('method'), 'unmocked subclass method still exists');
Packit 11f908
is(Test_Child->method, 1, 'mocked subclass method');
Packit 11f908
Packit 11f908
# test restoring non-existant functions
Packit 11f908
$test_mock->mock(ISA => sub {'basic test'});
Packit 11f908
can_ok(Test_Child => 'ISA');
Packit 11f908
is(Test_Child::ISA(), 'basic test',
Packit 11f908
	"testing a mocked sub that didn't exist before");
Packit 11f908
$test_mock->unmock('ISA');
Packit 11f908
ok(!Test_Child->can('ISA') && $Test_Child::ISA[0] eq 'Test_Parent',
Packit 11f908
	"restoring an undefined sub doesn't clear out the rest of the symbols");
Packit 11f908
Packit 11f908
done_testing;