File Coverage

blib/lib/Mock/App/Git/Workflow/Repository.pm
Criterion Covered Total %
statement 59 60 98.3
branch 11 16 68.7
condition 5 12 41.6
subroutine 12 12 100.0
pod 4 4 100.0
total 91 104 87.5


line stmt bran cond sub pod time code
1             package Mock::App::Git::Workflow::Repository;
2              
3             # Created on: 2014-09-05 04:34:42
4             # Create by: Ivan Wills
5             # $Id$
6             # $Revision$, $HeadURL$, $Date$
7             # $Revision$, $Source$, $Date$
8              
9 22     22   5012 use strict;
  22         50  
  22         673  
10 22     22   116 use warnings;
  22         48  
  22         664  
11 22     22   119 use version;
  22         47  
  22         149  
12 22     22   1502 use Carp qw/carp croak cluck confess longmess/;
  22         53  
  22         1554  
13 22     22   254 use Data::Dumper qw/Dumper/;
  22         155  
  22         1107  
14 22     22   131 use English qw/ -no_match_vars /;
  22         108  
  22         190  
15 22     22   7869 use base qw/Exporter/;
  22         50  
  22         15617  
16              
17             our $VERSION = version->new(1.1.16);
18             our @EXPORT_OK = qw//;
19             our %EXPORT_TAGS = ();
20             #our @EXPORT = qw//;
21             my $git;
22              
23             sub new {
24 22     22 1 53 my $class = shift;
25 22         49 my %param = @_;
26 22         50 my $self = \%param;
27 22         66 $self->{data} = [];
28              
29 22         56 bless $self, $class;
30              
31 22         224 return $git = $self;
32             }
33              
34             sub git {
35 22   33 22 1 357 return $git || __PACKAGE__->new;
36             }
37              
38             sub mock_add {
39 117     117 1 9005 my $self = shift;
40 117 50 66     766 confess "Data not Hashes!\n" . Dumper(\@_) if @_ && ref $_[0] ne 'HASH';
41 117         205 push @{ $self->{data} }, @_;
  117         392  
42             }
43              
44             sub mock_reset {
45 110     110 1 5726 my $self = shift;
46 110         199 @{ $self->{data} } = ();
  110         430  
47             }
48              
49             our $AUTOLOAD;
50             sub AUTOLOAD {
51 493     493   943 my $self = shift;
52 493         755 my $called = $AUTOLOAD;
53 493         2169 $called =~ s/.*:://;
54 493         1029 $called =~ s/_/-/g;
55              
56 493 50       1036 return if $called eq 'DESTROY';
57              
58 493         1516 my $cmd = "git $called " . (join ' ', @_);
59 493 100       737 if ( !@{ $self->{data} } ) {
  493         1318  
60 2         7 confess "No data setup for `$cmd`\n\t# ".(join "\n\t# ", reverse @{ $self->{ran} })."\n\t";
  2         578  
61             }
62 491         690 push @{ $self->{ran} }, $cmd;
  491         1050  
63              
64 491 50       1188 confess "Data not set up correctly! Not an ARRAY of HASHes\n" if ref $self->{data} ne "ARRAY";
65 491 50       2476 confess "Data not set up correctly! Not an Array of Hashes\n" if ref $self->{data}[0] ne "HASH";
66 491         688 my ($action, $return) = each %{ shift @{ $self->{data} } };
  491         614  
  491         1588  
67              
68             # sanity check
69 491 100       1046 if ($action ne $called) {
70             confess "Expected mock data for '$called' but got data for '$action'!\n"
71             . Dumper($cmd, $return)
72             . "\t# "
73 2         8 . (join "\n\t# ", reverse @{ $self->{ran} })
  2         420  
74             . "\n\t";
75             }
76              
77 489 100       976 if (wantarray) {
78 229 50 33     1323 if ( !$return || !ref $return || ref $return ne 'ARRAY' ) {
      33        
79 0         0 confess "Returning Mock for `$cmd` with scalar value when expecting an array\n" . Dumper($return), "\t";
80             }
81 229         951 return @$return;
82             }
83             else {
84             #cluck "Returning mock for `$cmd`\n" . Dumper($return), "\t";
85 260         890 return $return;
86             }
87             }
88              
89             1;
90              
91             __END__
92              
93             =head1 NAME
94              
95             Mock::App::Git::Workflow::Repository - Mock of a git repository
96              
97             =head1 VERSION
98              
99             This documentation refers to Mock::App::Git::Workflow::Repository version 1.1.16
100              
101             =head1 SYNOPSIS
102              
103             use Mock::App::Git::Workflow::Repository;
104              
105             # Brief but working code example(s) here showing the most common usage(s)
106             # This section will be as far as many users bother reading, so make it as
107             # educational and exemplary as possible.
108              
109             =head1 DESCRIPTION
110              
111             =head1 SUBROUTINES/METHODS
112              
113             =head2 C<new ()>
114              
115             Create a new Mock::App::Git::Workflow::Repository
116              
117             =head2 C<git ()>
118              
119             return the last created Mock::App::Git::Workflow::Repository
120              
121             =head2 C<mock_add (@data)>
122              
123             push data to be returned when methods are called
124              
125             =head2 C<mock_reset ()>
126              
127             Clear out any mock data, useful if tests have added too much data
128              
129             =head1 DIAGNOSTICS
130              
131             =head1 CONFIGURATION AND ENVIRONMENT
132              
133             =head1 DEPENDENCIES
134              
135             =head1 INCOMPATIBILITIES
136              
137             =head1 BUGS AND LIMITATIONS
138              
139             There are no known bugs in this module.
140              
141             Please report problems to Ivan Wills (ivan.wills@gmail.com).
142              
143             Patches are welcome.
144              
145             =head1 AUTHOR
146              
147             Ivan Wills - (ivan.wills@gmail.com)
148              
149             =head1 LICENSE AND COPYRIGHT
150              
151             Copyright (c) 2014 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
152             All rights reserved.
153              
154             This module is free software; you can redistribute it and/or modify it under
155             the same terms as Perl itself. See L<perlartistic>. This program is
156             distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
157             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
158             PARTICULAR PURPOSE.
159              
160             =cut