File: extends.t

package info (click to toggle)
libtest-mockobject-perl 1.20200122-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 1,260; makefile: 2
file content (217 lines) | stat: -rw-r--r-- 5,257 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 41;
use Test::Exception;

my $module = 'Test::MockObject::Extends';
use_ok( $module ) or exit;

my $tme = $module->new();
isa_ok( $tme, 'Test::MockObject' );

$tme    = $module->new( 'Test::Builder' );
ok( $tme->isa( 'Test::Builder' ),
	'passing a class name to new() should set inheritance properly' );

$tme      = $module->new( 'CPAN' );
ok( $INC{'CPAN.pm'},
	'new() should load parent module unless already loaded' );

package Some::Class;

@Some::Class::ISA = 'Another::Class';

sub path
{
	return $_[0]->{path};
}

sub foo
{
	return 'original';
}

sub bar
{
	return 'original';
}

package Another::Class;

package main;

# fake that we have loaded these
$INC{'Some/Class.pm'}    = 1;
$INC{'Another/Class.pm'} = 1;

$tme       = $module->new( 'Some::Class' );
my $result = $tme->set_always( bar => 'mocked' );
is( $tme->bar(), 'mocked',   'mock() should override method in parent' );
is( $tme->foo(), 'original', '... calling original methods in parent'  );
is( $result,           $tme, '... returning invocant'                  );

$result = $tme->unmock( 'bar' );
is( $tme->bar(), 'original', 'unmock() should remove method overriding' );
is( $result,           $tme, '... returning invocant'                   );

$result = $tme->mock( pass_self => sub
{
	is( shift,   $tme, '... and should pass along invocant' );
	is( $result, $tme, '... returning invocant'             );
});

$tme->pass_self();
my ($method, $args) = $tme->next_call();
is( $method, 'bar', '... logging methods appropriately' );

my $sc      = bless { path => 'my path' }, 'Some::Class';
my $mock_sc = $module->new( $sc );
is( $mock_sc->path(), 'my path',
	'... should wrap existing object appropriately' );
isa_ok( $mock_sc, 'Some::Class' )
	or diag( '... marking isa() appropriately on mocked object' );
isa_ok( $mock_sc, 'Another::Class' )
	or diag( '... and delegating isa() appropriately on parent classes' );

ok( ! $mock_sc->isa( 'No::Class' ),
	'... returning the right result even when the class is not a parent' );

$tme->set_always( -foo => 11 );
is( $tme->foo(), 11, 'unlogged methods should work' );
ok( ! $tme->called( 'foo' ), '... and logging should not happen for them' );

{
	my $warnings         = '';
	local $SIG{__WARN__} = sub { $warnings .= shift };
	$tme->set_always( foo => 12 );
	is( $warnings, '', '... not throwing redefinition warnings' );
}

$tme->set_always( foo => 12 );
is( $tme->foo(), 12,         '... allowing overriding with logged versions' );
ok( $tme->called( 'foo' ),   '... with logging happening then, obviously'   );

package Parent;

$INC{'Parent.pm'} = 1;

use vars '$somethingnasty';
$somethingnasty = '';

sub new      { bless {}, $_[0] }

sub mockthis { $somethingnasty = 1 }

sub AUTOLOAD { return $_[0]->mockthis() }

package main;

my $parent = Parent->new();
my $extend = Test::MockObject::Extends->new( $parent );
$extend->mock( 'mockthis', sub { return 'foo' } );
is( $extend->foo(), 'foo', 'Mocking worked' );
ok( ! $Parent::somethingnasty, "Method didn't trigger bad method" );

package Foo;

@Foo::ISA = 'Parent';

my ($called_foo, $called_autoload, $method_name);

use vars '$AUTOLOAD';

BEGIN
{
	$called_foo      = 0;
	$called_autoload = 0;
	$method_name     = '';
}

sub new
{
	bless {}, $_[0];
}

sub foo
{
	$called_foo++;
	return 'foo';
}

sub AUTOLOAD
{
	$called_autoload++;
	$method_name = $Foo::AUTOLOAD;
	return 'autoload';
}

package main;

my $object = Foo->new();
isa_ok( $object, 'Foo' );

my $mock;
lives_ok { $mock = Test::MockObject::Extends->new( $object ) }
	'Creating a wrapped module should not die';
isa_ok( $mock, 'Foo'   );

# Call foo()
is( $mock->foo(),     'foo', 'foo() should return as expected' );
is( $called_foo,          1, '... calling the method'          );
is( $called_autoload,     0, '... not touching AUTOLOAD()'     );
is( $Foo::AUTOLOAD,   undef, '... or $Foo::AUTOLOAD'           );

# Call an autoloaded method
is( $mock->bar(),     'autoload', 'bad() should returns as expected'        );
is( $called_autoload,          1, '... calling AUTOLOAD()'                  );
is( $method_name,     'Foo::bar', '... with the appropriate $Foo::AUTOLOAD' );

# get the parents of the mocked object (to work with SUPER)
$result = [ $mock->__get_parents() ];
is_deeply( $result, [qw( Parent )],
	'__get_parents() should return a list of parents of the wrapped object' );


package FooNoAutoload;

my ($called_fooNA, $called_autoloadNA, $method_nameNA);

sub new
{
	bless {}, $_[0];
}

sub fooNA
{
	$called_fooNA++;
	return 'fooNA';
}

package main;

BEGIN
{
	$called_fooNA      = 0;
	$called_autoloadNA = 0;
	$method_nameNA     = '';
}

$object = FooNoAutoload->new();
isa_ok( $object, 'FooNoAutoload' );

undef $mock;
lives_ok { $mock = Test::MockObject::Extends->new( $object ) }
	'Creating a wrapped module should not die';
isa_ok( $mock, 'FooNoAutoload'   ); #37

# Call foo()
is( $mock->fooNA(),     'fooNA', 'fooNA() should return as expected' );
is( $called_fooNA,          1, '... calling the method'          );
is( $called_autoloadNA,     0, '... not touching AUTOLOAD()'     );

# Call a non-existent method
dies_ok (sub{ $mock->bar()},
    '... should die if calling a non-mocked and non-AUTOLOADED method' );