File Coverage

testlib/App/MaMGal/TestHelper.pm
Criterion Covered Total %
statement 68 132 51.5
branch 15 30 50.0
condition 3 10 30.0
subroutine 13 30 43.3
pod 0 14 0.0
total 99 216 45.8


line stmt bran cond sub pod time code
1             # mamgal - a program for creating static image galleries
2             # Copyright 2008-2009 Marcin Owsiany
3             # See the README file for license information
4             package App::MaMGal::TestHelper;
5 10     10   683528 use Test::MockObject;
  10         32862  
  10         69  
6 10     10   310 use Test::More;
  10         20  
  10         121  
7 10     10   3360 use base 'Exporter';
  10         41  
  10         916  
8 10     10   56 use Carp;
  10         21  
  10         20470  
9             @EXPORT = qw(get_mock_entry get_mock_iif get_mock_datetime_parser get_mock_formatter get_mock_localeenv get_mock_cc prepare_test_data get_mock_mplayer_wrapper get_mock_logger logged_only_ok logged_exception_only_ok get_mock_exception get_mock_fh printed_only_ok);
10              
11             sub get_mock_entry
12             {
13 0 0   0 0 0 my $class = shift or croak "class required";
14 0         0 my $mock_entry = Test::MockObject->new
15             ->mock('set_root')
16             ->mock('make')
17             ->mock('add_tools');
18 0         0 $mock_entry->set_isa($class);
19 0         0 return $mock_entry;
20             }
21              
22             sub get_mock_fh {
23 5     5 0 5063 my $fh = Test::MockObject->new->mock('printf');
24 5         208 return $fh;
25             }
26              
27             sub get_mock_iif {
28 0     0 0 0 my $f = Test::MockObject->new->mock('read', sub { Test::MockObject->new });
  0     0   0  
29 0         0 $f->set_isa('App::MaMGal::ImageInfoFactory');
30 0         0 $f
31             }
32              
33             sub get_mock_logger {
34 110     110 0 63656 my $l = Test::MockObject->new
35             ->mock('log_message')
36             ->mock('log_exception');
37 110         6043 $l->set_isa('App::MaMGal::Logger');
38 110         1817 $l
39             }
40              
41             sub get_mock_datetime_parser {
42 42     42 0 84647 my $p = Test::MockObject->new->mock('parse');
43 42         1678 $p->set_isa('Image::EXIF::DateTime::Parser');
44 42         787 $p
45             }
46              
47             sub get_mock_formatter {
48 0     0 0 0 my @methods = @_;
49 0         0 my $mf = Test::MockObject->new();
50 0         0 $mf->set_isa('App::MaMGal::Formatter');
51 0     0   0 $mf->mock($_, sub { "whatever" }) for @methods;
  0         0  
52 0         0 return $mf;
53             }
54              
55             sub get_mock_localeenv {
56 0     0 0 0 my $ml = Test::MockObject->new();
57 0         0 $ml->set_isa('App::MaMGal::LocaleEnv');
58 0     0   0 $ml->mock('get_charset', sub { "ISO-8859-123" });
  0         0  
59 0         0 $ml->mock('set_locale');
60 0     0   0 $ml->mock('format_time', sub { "12:12:12" });
  0         0  
61 0     0   0 $ml->mock('format_date', sub { "18 dec 2004" });
  0         0  
62 0         0 return $ml;
63             }
64              
65             sub get_mock_mplayer_wrapper {
66 0     0 0 0 my $mmw = Test::MockObject->new;
67 0         0 $mmw->set_isa('App::MaMGal::MplayerWrapper');
68 0         0 my $mock_image = Test::MockObject->new;
69 0         0 $mock_image->set_isa('Image::Magick');
70 0     0   0 $mock_image->mock('Get', sub { '100', '100' });
  0         0  
71 0     0   0 $mock_image->mock('Scale', sub { undef });
  0         0  
72 0     0   0 $mock_image->mock('Write', sub { system('touch', $_[1] ) });
  0         0  
73 0     0   0 $mmw->mock('snapshot', sub { $mock_image });
  0         0  
74 0         0 return $mmw;
75             }
76              
77             sub get_mock_cc($) {
78 0     0 0 0 my $ret = shift;
79 0         0 my $mcc = Test::MockObject->new;
80 0         0 $mcc->set_isa('App::MaMGal::CommandChecker');
81 0     0   0 $mcc->mock('is_available', sub { $ret });
  0         0  
82             }
83              
84             sub prepare_test_data {
85             # We have to create empty directories, because git does not track them
86 5     5 0 5054 for my $dir (qw(empty one_dir one_dir/subdir more/subdir/lost+found)) {
87 20 50 0     377 mkdir("td.in/$dir") or die "td.in/$dir: $!" unless -d "td.in/$dir";
88             }
89             # We have to create and populate directories with spaces in their
90             # names, because perl's makemaker does not like them
91 5 50       83 mkdir "td.in/more/zzz another subdir" unless -d "td.in/more/zzz another subdir";
92 5 50       82 my $orig_size = -s "td.in/p.png" or die "Unable to stat td.in/p.png";
93 5         77 my $dest_size = -s 'td.in/more/zzz another subdir/p.png';
94 5 50 33     45 unless ($dest_size and $orig_size == $dest_size) {
95 0         0 system('cp', '-a', 'td.in/p.png', 'td.in/more/zzz another subdir/p.png');
96             }
97             # We also need to create our test symlinks, because MakeMaker does not like them
98 5         41 for my $pair ([qw(td.in/symlink_broken broken)], [qw(td.in/symlink_pic_noext one_pic/a1.png)], [qw(td.in/symlink_to_empty empty)], [qw(td.in/symlink_to_empty_file empty_file)], [qw(td.in/symlink_pic.png one_pic/a1.png)]) {
99 25         38 my ($link, $dest) = @$pair;
100 25 50 0     307 symlink($dest, $link) or die "Failed to symlink [$dest] to [$link]" unless -l $link;
101             }
102             # Finally, purge and copy a clean version of the test data into "td"
103 5 50       157441 system('rm -rf td ; cp -a td.in td') == 0 or die "Test data preparation failed: $?";
104             }
105              
106             sub logged_only_ok($$;$)
107             {
108 2     2 0 18 my $mock = shift;
109 2         6 my $re = shift;
110 2         5 my $prefix = shift;
111 2         4 my $level = $Test::Builder::Level;
112 2         5 local $Test::Builder::Level = $level + 1;
113 2         10 my ($name, $args) = $mock->next_call;
114 2         35 is($name, 'log_message', 'expected method was called');
115 2         1417 like($args->[1], $re, 'message as expected');
116 2         964 is($args->[2], $prefix, 'prefix as expected');
117 2         942 ($name, $args) = $mock->next_call;
118 2         34 is($name, undef, 'no other logging method was called');
119 2 50       802 return unless defined $name;
120 0         0 is($args->[1], undef, 'no args were passed either');
121 0         0 is($args->[2], undef, 'no args were passed either');
122 0         0 is($args->[3], undef, 'no args were passed either');
123             }
124              
125             sub printed_only_ok($$;$)
126             {
127 12     12 0 5809 my $mock = shift;
128 12         17 my $re = shift;
129 12         18 my $level = $Test::Builder::Level;
130 12         22 local $Test::Builder::Level = $level + 1;
131 12         14 my ($name, $args);
132 12 100 66     76 if (ref $re and ref $re eq 'ARRAY') {
133 1         3 foreach my $line_re (@$re) {
134 10         3919 ($name, $args) = $mock->next_call;
135 10         520 is($name, 'printf', "expected method was called (checking $line_re)");
136 10         4226 is($args->[1], "%s%s\n", "format string as expected (for $line_re)");
137 10 50       5982 like((($args->[2] ? $args->[2] : '') . ($args->[3] ? $args->[3] : '')), $line_re, 'message as expected');
    50          
138             }
139             } else {
140 11         38 ($name, $args) = $mock->next_call;
141 11         224 is($name, 'printf', 'expected method was called');
142 11         6797 is($args->[1], "%s%s\n", 'format string as expected');
143 11 100       5917 like((($args->[2] ? $args->[2] : '') . ($args->[3] ? $args->[3] : '')), $re, 'message as expected');
    50          
144             }
145 12         6554 ($name, $args) = $mock->next_call;
146 12         198 is($name, undef, 'no other logging method was called');
147 12 50       5483 return unless defined $name;
148 0         0 is($args->[1], undef, 'no args were passed either');
149 0         0 is($args->[2], undef, 'no args were passed either');
150 0         0 is($args->[3], undef, 'no args were passed either');
151             }
152              
153             sub logged_exception_only_ok($$;$)
154             {
155 0     0 0 0 my $mock = shift;
156 0         0 my $ex = shift;
157 0         0 my $prefix = shift;
158 0         0 my $level = $Test::Builder::Level;
159 0         0 local $Test::Builder::Level = $level + 1;
160 0         0 my ($name, $args) = $mock->next_call;
161 0         0 is($name, 'log_exception');
162 0         0 is($args->[1], $ex);
163 0         0 is($args->[2], $prefix);
164 0         0 ($name, $args) = $mock->next_call;
165 0         0 is($name, undef, 'no other logging method was called');
166 0 0       0 return unless defined $name;
167 0         0 is($args->[1], undef, 'no args were passed either');
168 0         0 is($args->[2], undef, 'no args were passed either');
169 0         0 is($args->[3], undef, 'no args were passed either');
170             }
171              
172             sub get_mock_exception($)
173             {
174 5     5 0 2949 my $class = shift;
175 5         17 my $e = Test::MockObject->new;
176 5         43 $e->set_isa($class);
177 5     8   106 $e->mock('message', sub { 'foo bar' });
  8         461  
178 5     2   134 $e->mock('interpolated_message', sub { 'foo bar baz' });
  2         122  
179 5         102 return $e;
180             }
181              
182             1;