This file is indexed.

/usr/share/perl5/Lire/Test/Mock.pm is in lire 2:2.1.1-2.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package Lire::Test::Mock;

use strict;

use vars qw/%FACTORIES %FACTORY_RESULTS %MOCK_INSTANCES/;

use Class::Inner;
use Devel::Symdump;
use Lire::Utils qw/check_param check_object_param/;
use Carp;

%FACTORIES = ();
%MOCK_INSTANCES = ();
%FACTORY_RESULTS = ();

=pod

=head1 NAME

Lire::Test::Mock - Create mock object

=head1 SYNOPSIS

  use Lire::Report;
  use Lire::Test::Mock;
  use Lire::DlfResult;

  my $mock = new Lire::Test::Mock( 'Lire::Report' );
  $mock->set_result( 'timestamp', $time );
  $mock->timestamp(); # will return $time
  $mock->get_calls(); # [ 'timestamp' ]
  $mock->get_invocation( 'timestamp', 0 ); # [ $mock ]

=head1 DESCRIPTION

This class makes it easy to defined mock objects. Mock objects are
objects which offers the same interface than another object but which
do not share its functionality. This makes it easier to test objects
which requires fixtures which have lots of dependencies.

The mock object can be used to collect information about calls made on
the object. Returns value for such method invocation can also be
specified.

=head2 new( $class, 'method' => $result, 'method' => $result )

Creates a new mock object that will wrap $class. Any other keyword
arguments will be use to initialize the result of methods call. See
set_result() for information on how this works.


=cut

sub new {
    my ( $self, $class, %results ) = @_;

    my $methods = _create_methods( $class );
    $methods->{'new'} = sub { return bless( {},  shift ) };
    $self = new Class::Inner( 'parent' => $class,
                              'methods' => $methods );
    _init_mock( $self, $class, 'new' );

    $self->set_result( %results )
      if %results;

    return $self;
}


=pod

=head2 new_proxy( $class, @constructor_params )

This creates mock object which for the base $class. A proxy mock
object will still monitor calls to the object but the real methods
will be invoked, unless a result was specified using set_result(). Any
remaining parameters will be passed to the new() method which should
be defined in the class.

=head2 new_proxy( $instance )

Makes a Lire::Test::Mock object which is a clone of $instance.

=cut

sub new_proxy {
    my $pkg = shift;
    my $class = shift;

    check_param( $class, 'class' );

    return new_proxy_instance Lire::Test::Mock( $class )
      if ref $class;

    my $args = [ @_ ];
    my $methods = _create_methods( $class );
    my $self = new Class::Inner( 'parent' => $class,
                                 'args' => $args,
                                 'methods' => $methods );
    _init_mock( $self, $class );
    $self->{'_proxy'} = 1;

    return $self;
}

sub new_proxy_instance {
    my ( $pkg, $proto ) = @_;

    check_object_param( $proto, 'proto', 'HASH' );
    my $class = ref $proto;
    my $methods = _create_methods( $class );
    $methods->{'new'} = sub { bless $proto, shift };
    my $self = new Class::Inner( 'parent' => $class,
                                 'methods' => $methods );
    _init_mock( $self, $class );
    $self->{'_proxy'} = 1;

    return $self;
}

=pod

=head2 is_proxy()

Returns whether this mock object will proxy to the real methods
when no results was defined for a specific method.

=cut

sub is_proxy {
    return $_[0]{'_proxy'};
}

sub _init_mock {
    my ( $mock, $class ) = @_;

    $mock->{'_base'} = $class;
    $mock->{'_results'} = {};
    $mock->{'_calls'} = [];
    $mock->{'_invocations'} = {};
    $mock->{'_proxy'} = 0;

    foreach my $name ( list_methods( $class ) ) {
        next if $name eq 'new';
        $mock->{'_invocations'}{$name} = [];
    }

    return;
}

sub _create_methods {
    my $class = $_[0];

    my %methods = ();
    foreach my $name ( list_methods( $class ) ) {
        next if $name eq 'new';
        next if $name eq 'DESTROY';
        $methods{$name} = gen_mock_method( $name );
    }
    $methods{'get_calls'} = \&get_calls;
    $methods{'set_result'} = \&set_result;
    $methods{'get_invocation'} = \&get_invocation;
    $methods{'invocation_count'} = \&invocation_count;
    $methods{'is_proxy'} = \&is_proxy;

    return \%methods;
}

sub gen_mock_method {
    my $name = $_[0];

    return sub { my $self = $_[0];

                 push @{$self->{'_calls'}}, $name;
                 push @{$self->{'_invocations'}{$name}}, [ @_ ];

                 if ( $self->{'_proxy'} &&
                      ! defined $self->{'_results'}{$name} )
                 {
                     my $method = UNIVERSAL::can( $self->{'_base'},
                                                  $name );
                     return $method->( @_ );
                 }
                 return unless exists $self->{'_results'}{$name};

                 my $value = $self->{'_results'}{$name};
                 return $value->( @_ ) if ref $value && ref $value eq 'CODE';
                 return $value;
             };
}

sub list_methods {
    my $class = $_[0];
    check_param( $class, 'class' );

    my @methods = map { $_=~ s/^$class\:://;
                        $_
                    } Devel::Symdump->functions( $class );

    no strict 'refs';
    foreach my $base ( @{"$class\::ISA"} ) {
        push @methods, list_methods( $base );
    }
    my %methods = map { $_ => 1 } @methods;
    return keys %methods
}


=pod

=head2 get_calls()

Returns an array reference containing all the methods called on the
object.

=cut

sub get_calls {
    return [ @{$_[0]{'_calls'}} ];
}

=pod

=head2 invocation_count( $method )

Returns the number of time $method was called.

=cut

sub invocation_count {
    my ( $self, $method ) = @_;

    check_param( $method, 'method' );
    croak "no method '$method' defined in $self->{'_base'}"
      unless exists $self->{'_invocations'}{$method};

    return scalar @{$self->{'_invocations'}{$method}};
}

=pod

=head2 get_invocation( $method, $index )

Returns the parameter that were given when method $method was called.

=cut

sub get_invocation {
    my ( $self, $method, $index ) = @_;

    check_param( $method, 'method' );
    $index ||= 0;

    croak "no method '$method' defined in $self->{'_base'}"
      unless exists $self->{'_invocations'}{$method};

    croak "no invocation $index for method '$method'"
      unless $index < @{$self->{'_invocations'}{$method}};

    return $self->{'_invocations'}{$method}[$index];
}


=pod

=head2 set_result( method => $result, ... )

This assign the result $result to $method. If $result is a code
reference, it will be invoked with the same argument than the method
to compute the result.

=cut

sub set_result {
    my ( $self, %results ) = @_;

    foreach my $method ( keys %results ) {
        croak "no method '$method' defined in $self->{'_base'}"
          unless exists $self->{'_invocations'}{$method};
        $self->{'_results'}{$method} = $results{$method};
    }

    return;
}


=pod

=head1 USING MOCK FACTORIES

Sometime, it is not possible to instatiate a proxy or mock object
during fixture setup. This will usually happen when the object which
we want to track access to is instantiated by the method under test.
In these cases, one can use the set_mock_factory() class method to
change the factory method to one that will return a proxy instance
instead of a real instance. One should call reset_factories() during
tear_down() so that the real factory method become directly accessible
once again.

=head2 set_mock_factory( $class, %results )

Make the new() method of package $class returns proxy Lire::Test::Mock
instance. The created instances will be accessible through the
mock_instances() method. Any other argument will be passed to the
set_result() method when the mock instance is created.

=cut

sub set_mock_factory {
    my ( $pkg, $class, %results ) = @_;

    check_param( $class, 'class' );

    return if exists $FACTORIES{$class};

    my $factory = $class->can( 'new' );
    croak "no new() method in class $class"
      unless $factory;

    no strict 'refs';
    $FACTORIES{$class} = *{"${class}\::new"}{'CODE'};
    $FACTORY_RESULTS{$class} = \%results;
    $MOCK_INSTANCES{$class} = [];
    no warnings 'redefine';
    *{"${class}\::new"} =
      sub { my $self = $factory->( @_ );
            my $mock = new_proxy_instance Lire::Test::Mock( $self );
            $mock->set_result( %{$FACTORY_RESULTS{$class}} );
            push @{$mock->{'_calls'}}, 'new';
            push @{$mock->{'_invocations'}{'new'}}, [ @_ ];
            push @{$MOCK_INSTANCES{$class}}, $mock;
            return $mock;
        };
    return;
}


=pod

=head2 mock_instances( $class )

Returns an array reference containing all the instance that were
created by the installed mock factory in $class. This method will
throw an exception if now mock factory was installed for class $class.

=cut

sub mock_instances {
    my ( $pkg, $class ) = @_;

    check_param( $class, 'class' );

    croak "no mock factory installed for class '$class'"
      unless exists $MOCK_INSTANCES{$class};

    return $MOCK_INSTANCES{$class};
}

=pod

=head2 reset_factories()

Removes all mock factories that were set up using set_mock_factory().

=cut

sub reset_factories {
    my $pkg = $_[0];

    while ( my ( $class, $factory ) = each %FACTORIES ) {
        no strict 'refs';
        no warnings 'redefine';
        if ( $factory ) {
            *{"$class\::new"} = $factory;
        } else {
            delete "$class\::"->{'new'};
        }
    }
    %FACTORIES = ();
    %MOCK_INSTANCES = ();
    %FACTORY_RESULTS = ();

    return;
}


1;

__END__

=pod

=head1 SEE ALSO

Test::Unit::TestCase(3pm)

=head1 VERSION

$Id: Mock.pm,v 1.5 2006/07/23 13:16:32 vanbaal Exp $

=head1 AUTHORS

Francis J. Lacoste <flacoste@logreport.org>

=head1 COPYRIGHT

Copyright (C) 2004 Stichting LogReport Foundation LogReport@LogReport.org

This file is part of Lire.

Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html. 

=cut