Same/TextUI.pm

filter: Statementstmt 98.6% 148 / 150 Branchbran 90.0% 54 / 60 Conditioncond 92.0% 23 / 25 MC/DCmcdc 87.5% 14 / 16 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 95.5% 257 / 269 CC 43 SCAR 26.6
CC
43
Cov
96%
_draw_match_side_by_side
27.0
_draw_range_and_context
6.0
_get_start_context
4.0
CRAP 14.3
↑ Summary Same.pm »
1 =head1 NAME
2  
3 Text::Same::TextUI
4  
5 =head1 DESCRIPTION
6  
7 functions for outputting the results of a comparison made with Text::Same::compare();
8  
9 =head1 SYNOPSIS
10  
11 Usage : use Text::Same::TextUI;
12 ...
13 my $matchmap = compare(\%options, $file1, $file2);
14 my @source1_non_matches = $matchmap->source1_non_matches;
15 my @source2_non_matches = $matchmap->source2_non_matches;
16 draw_non_matches(\%options, \@source1_non_matches, $matchmap->source1);
17 draw_non_matches(\%options, \@source2_non_matches, $matchmap->source2);
18  
19  
20 =head1 METHODS
21  
22 See below. Methods private to this module are prefixed by an
23 underscore.
24  
25 =cut
26  
27 package Text::Same::TextUI;
28  
29 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use vars qw($VERSION @EXPORT @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 @ISA = qw( Exporter );
32 2 @EXPORT = qw( draw_match draw_non_match );
33  
34 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 $VERSION = '0.07';
39  
40 2 ▶use Text::Same::ChunkedSource;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Text::Same::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 =head2 draw_non_match
44  
45 Title : draw_non_match
46 Usage : draw_non_match(\%options, $source, $non_match);
47 Function: return a string suitable to output that is a representation of
48 a non matching region (range of chunk indexes) in a particular
49 source
50 Args : %options - settings to use
51 $source - the ChunkedSource that this non-match came from (for
52 looking up the actual chunks/lines for the range of
53 indexes)
54 $non_match - a Range object representing the non-matching chunks
55  
56 =cut
57  
58 sub draw_non_match
59 30 ▶{
Subroutine draw_non_match: called CC=3 SCAR=11.0
Pod documented
60 30 my $options = shift;
61 30 my $source = shift;
62 30 my $non_match_range = shift;
63 30 my $screen_width = 78;
64  
65 30 ▶ if (defined $options->{term_width}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"term_width"}) 0 30 30
66 0 $screen_width = $options->{term_width} - 2;
67 }
68  
69 30 my $start = $non_match_range->start();
70 30 my $end = $non_match_range->end();
71  
72 30 my $ret = " " . ($start+1) . ".." . ($end+1) . ":\n";
73 30 my @match_chunks = _get_match_chunks($options, $start, $end, $source);
74  
75 30 my $padding = " ";
76  
77 30 for my $match_chunk (@match_chunks) {
78 30 $match_chunk = substr $match_chunk, 0, $screen_width - length $padding;
79 30 $ret .= $padding;
80 30 $ret .= $match_chunk;
81 30 $ret .= "\n";
82 }
83  
84 30 return $ret;
85 }
86  
87 =head2 draw_match
88  
89 Title : draw_match
90 Usage : draw_match(\%options, $match);
91 Function: return a string suitable to output that is a representation of
92 a match between two sources
93 Args : %options - settings to use
94 $match - a Match object representing the matching chunks
95  
96 =cut
97  
98 sub draw_match
99 70 ▶{
Subroutine draw_match: called CC=2 SCAR=6.9
Pod documented
100 70 my $options = shift;
101 70 my $match = shift;
102  
103 70 ▶ if ($options->{side_by_side}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'side_by_side'}) { } 35 35 70
104 35 _draw_match_side_by_side($options, $match);
105 } else {
106 35 _draw_match_vertically($options, $match);
107 }
108 }
109  
110 sub _draw_match_side_by_side
111 35 ▶{
Subroutine _draw_match_side_by_side: called CC=27 SCAR=33.0
112 35 my $options = shift;
113 35 my $match = shift;
114  
115 35 my $min1 = $match->min1;
116 35 my $max1 = $match->max1;
117 35 my $min2 = $match->min2;
118 35 my $max2 = $match->max2;
119  
120 35 my $width = _get_term_width($options);
121  
122 35 my $half_width = int($width / 2 - 6);
123  
124 35 my $ret = "match " . ($min1+1) . ".." . ($max1+1) . "==" .
125 ($min2+1) . ".." . ($max2+1) . "\n";
126  
127 35 my @start_context1 = _get_start_context($options, $min1, $match->source1);
128 35 my @start_context2 = _get_start_context($options, $min2, $match->source2);
129 35 ▶ my $max_start_context_len =
Branch: 1 branch
BranchTrueFalseTotal
scalar @start_context1 > scalar @start_context2 ? : 0 35 35
130 scalar(@start_context1) > scalar(@start_context2) ?
131 scalar(@start_context1) : scalar(@start_context2);
132  
133 35 my $context_format_str =
134 " %-${half_width}.${half_width}s %-${half_width}.${half_width}s\n";
135 35 my $match_format_str =
136 " %-${half_width}.${half_width}s %s %-${half_width}.${half_width}s\n";
137 35 my $i;
138 35 ▶ for ($i = 0; $i < $max_start_context_len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max_start_context_len) 105 35 140
139 105 ▶ if (defined $start_context1[$i] || defined $start_context2[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start_context1[$i] or defined $start_context2[$i]) 63 42 105
Condition100%3/3
defined $start_context1[$i] or defined $start_context2[$i]
l!l&&r!l&&!r
511242
MC/DC100%2/2
defined $start_context1[$i] or defined $start_context2[$i]
defined $start_context1[$i] defined $start_context2[$i]
Truth table100%3/3
A or B
A= defined $start_context1[$i]B= defined $start_context2[$i]
ABresult
1X1
011
000
140 63 ▶ $ret .= sprintf $context_format_str,
Branch: 2 branches
BranchTrueFalseTotal
defined $start_context1[$i] ? : 51 12 63
defined $start_context2[$i] ? : 54 9 63
141 (defined $start_context1[$i] ? $start_context1[$i] : ""),
142 (defined $start_context2[$i] ? $start_context2[$i] : "");
143 }
144 }
145  
146 35 my @match_chunks1 = _get_match_chunks($options, $min1, $max1,
147 $match->source1);
148 35 my @match_chunks2 = _get_match_chunks($options, $min2, $max2,
149 $match->source2);
150 35 my $next_indicator = undef;
151  
152 35 ▶ for ($i = 0; $i<scalar(@match_chunks1) or $i<scalar(@match_chunks2); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @match_chunks1 or $i < scalar @match_chunks2) 118 35 153
Condition100%3/3
$i < scalar @match_chunks1 or $i < scalar @match_chunks2
l!l&&r!l&&!r
116235
MC/DC100%2/2
$i < scalar @match_chunks1 or $i < scalar @match_chunks2
$i < scalar @match_chunks1 $i < scalar @match_chunks2
Truth table100%3/3
A or B
A= $i < scalar @match_chunks1B= $i < scalar @match_chunks2
ABresult
1X1
011
000
153 131 my $left_chunk = $match_chunks1[$i];
154 131 ▶ if (defined $left_chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left_chunk) 129 2 131
155 129 $left_chunk =~ s/\t/ /g;
156 }
157 131 ▶ my $left_ignorable =
Condition100%3/3
defined $left_chunk && is_ignorable($options, $left_chunk)
!ll&&!rl&&r
210821
MC/DC100%2/2
defined $left_chunk && is_ignorable($options, $left_chunk)
defined $left_chunk is_ignorable($options, $left_chunk)
Truth table100%3/3
A && B
A= defined $left_chunkB= is_ignorable($options, $left_chunk)
ABresult
0X0
100
111
158 defined $left_chunk && is_ignorable($options, $left_chunk);
159 131 my $right_chunk = $match_chunks2[$i];
160 131 ▶ if ($right_chunk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right_chunk) 108 23 131
161 108 $right_chunk =~ s/\t/ /g;
162 }
163 131 ▶ my $right_ignorable =
Condition100%3/3
defined $right_chunk && is_ignorable($options, $right_chunk)
!ll&&!rl&&r
110822
MC/DC100%2/2
defined $right_chunk && is_ignorable($options, $right_chunk)
defined $right_chunk is_ignorable($options, $right_chunk)
Truth table100%3/3
A && B
A= defined $right_chunkB= is_ignorable($options, $right_chunk)
ABresult
0X0
100
111
164 defined $right_chunk && is_ignorable($options, $right_chunk);
165  
166 131 my $indicator;
167 131 ▶ if ($left_ignorable && $right_ignorable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_ignorable and $right_ignorable) { } 15 116 131
Condition100%3/3
$left_ignorable and $right_ignorable
!ll&&!rl&&r
110615
MC/DC100%2/2
$left_ignorable and $right_ignorable
$left_ignorable $right_ignorable
Truth table100%3/3
A and B
A= $left_ignorableB= $right_ignorable
ABresult
0X0
100
111
168 15 ▶ if (defined $next_indicator) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_indicator) { } 13 2 15
169 13 $indicator = $next_indicator;
170 } else {
171 2 $indicator = "-";
172 }
173 } else {
174 116 ▶ if (!$left_ignorable && !$right_ignorable) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $left_ignorable || $right_ignorable) { } 103 13 116
Condition100%3/3
$left_ignorable || $right_ignorable
l!l&&r!l&&!r
67103
MC/DC100%2/2
$left_ignorable || $right_ignorable
$left_ignorable $right_ignorable
Truth table100%3/3
A || B
A= $left_ignorableB= $right_ignorable
ABresult
1X1
011
000
175 103 $indicator = "|";
176 } else {
177 13 ▶ if ($left_ignorable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left_ignorable) { } 6 7 13
178 # insert a blank on the right and try again
179 6 splice(@match_chunks2, $i, 0, "");
180 6 $next_indicator = "<";
181 6 redo;
182 } else {
183 # insert a blank on the left and try again
184 7 splice(@match_chunks1, $i, 0, "");
185 7 $next_indicator = ">";
186 7 redo;
187 }
188 }
189 }
190  
191 118 ▶ $ret .= sprintf $match_format_str,
Branch: 2 branches
BranchTrueFalseTotal
defined $left_chunk ? : 118 0 118
defined $right_chunk ? : 118 0 118
192 (defined $left_chunk ? $left_chunk : ""),
193 $indicator,
194 (defined $right_chunk ? $right_chunk : "");
195  
196 118 $next_indicator = undef;
197 }
198  
199 35 my @end_context1 = _get_end_context($options, $max1, $match->source1);
200 35 my @end_context2 = _get_end_context($options, $max2, $match->source2);
201  
202 35 ▶ my $max_end_context_len =
Branch: 1 branch
BranchTrueFalseTotal
scalar @end_context1 > scalar @end_context2 ? : 0 35 35
203 scalar(@end_context1) > scalar(@end_context2) ?
204 scalar(@end_context1) : scalar(@end_context2);
205  
206 35 ▶ for ($i = 0; $i < $max_end_context_len; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max_end_context_len) 70 35 105
207 70 ▶ if (defined $end_context1[$i] || defined $end_context2[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end_context1[$i] or defined $end_context2[$i]) 43 27 70
Condition100%3/3
defined $end_context1[$i] or defined $end_context2[$i]
l!l&&r!l&&!r
321127
MC/DC100%2/2
defined $end_context1[$i] or defined $end_context2[$i]
defined $end_context1[$i] defined $end_context2[$i]
Truth table100%3/3
A or B
A= defined $end_context1[$i]B= defined $end_context2[$i]
ABresult
1X1
011
000
208 43 ▶ $ret .= sprintf $context_format_str,
Branch: 2 branches
BranchTrueFalseTotal
defined $end_context1[$i] ? : 32 11 43
defined $end_context2[$i] ? : 37 6 43
209 (defined $end_context1[$i] ? $end_context1[$i] : ""),
210 (defined $end_context2[$i] ? $end_context2[$i] : "");
211 }
212 }
213  
214  
215 35 return $ret;
216 }
217  
218 sub _get_match_chunks
219 100 ▶{
Subroutine _get_match_chunks: called CC=2 SCAR=6.9
220 100 my ($options, $min, $max, $source) = @_;
221  
222 100 my @ret = ();
223  
224 100 ▶ for (my $i = $min; $i <= $max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 253 100 353
225 253 my $text = ($source->get_all_chunks)[$i];
226 253 $text =~ s/\t/ /g;
227 253 push @ret, $text;
228 }
229  
230 100 return @ret;
231 }
232  
233 sub _get_start_context
234 140 ▶{
Subroutine _get_start_context: called CC=4 SCAR=13.9
235 140 my ($options, $min, $source) = @_;
236  
237 140 ▶ my $context_chunks = $options->{context} || 3;
Condition50%1/2
$options->{'context'} || 3
l!l
0140
MC/DC0%0/1
$options->{'context'} || 3
$options->{'context'}
Truth table50%1/2
A
A= $options->{'context'}
Aresult
11
00
238 140 my $context_min = $min - $context_chunks;
239  
240 140 my @ret = ();
241  
242 140 ▶ for (my $i = $context_min; $i < $min; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $min) 420 140 560
243 420 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) { } 210 210 420
244 210 push @ret, undef;
245 } else {
246 210 my $text = ($source->get_all_chunks)[$i];
247 210 $text =~ s/\t/ /g;
248 210 push @ret, $text;
249 }
250 }
251  
252 140 return @ret;
253 }
254  
255 sub _get_end_context
256 140 ▶{
Subroutine _get_end_context: called CC=4 SCAR=13.9
257 140 my ($options, $max, $source) = @_;
258  
259 140 ▶ my $context_chunks = $options->{context} || 3;
Condition50%1/2
$options->{'context'} || 3
l!l
0140
MC/DC0%0/1
$options->{'context'} || 3
$options->{'context'}
Truth table50%1/2
A
A= $options->{'context'}
Aresult
11
00
260 140 my $context_max = $max + $context_chunks;
261  
262 140 my @ret = ();
263  
264 140 ▶ for (my $i = $max + 1; $i < $context_max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $context_max) 280 140 420
265 280 ▶ if ($i > $source->get_all_chunks_count - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $source->get_all_chunks_count - 1) { } 142 138 280
266 142 push @ret, undef;
267 } else {
268 138 my $text = ($source->get_all_chunks)[$i];
269 138 $text =~ s/\t/ /g;
270 138 push @ret, $text;
271 }
272 }
273  
274 140 return @ret;
275 }
276  
277 sub _draw_match_vertically
278 35 ▶{
Subroutine _draw_match_vertically: called CC=1 SCAR=0.0
279 35 my ($options, $match) = @_;
280  
281 35 my $ret = "match " . $match->as_string . "\n";
282  
283 35 $ret .= _draw_range_and_context($options, $match->min1, $match->max1,
284 $match->source1);
285 35 $ret .= "=== matches\n";
286  
287 35 $ret .= _draw_range_and_context($options, $match->min2, $match->max2,
288 $match->source2);
289  
290 }
291  
292 sub _draw_range_and_context
293 70 ▶{
Subroutine _draw_range_and_context: called CC=6 SCAR=17.9
294 70 my ($options, $min, $max, $source) = @_;
295  
296 70 my $ret = "";
297  
298 70 for my $start_chunk_text (_get_start_context($options, $min, $source)) {
299 210 ▶ if (defined $start_chunk_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start_chunk_text) 105 105 210
300 105 $ret .= " $start_chunk_text\n";
301 }
302 }
303  
304 70 ▶ for (my $i = $min; $i <= $max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 223 70 293
305 223 my $match_chunk_text = ($source->get_all_chunks)[$i];
306 223 $ret .= "= $match_chunk_text\n";
307 }
308  
309 70 for my $end_chunk_text (_get_end_context($options, $max, $source)) {
310 140 ▶ if (defined $end_chunk_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end_chunk_text) 69 71 140
311 69 $ret .= " $end_chunk_text\n";
312 }
313 }
314  
315 70 return $ret;
316 }
317  
318 sub _get_term_width
319 35 ▶{
Subroutine _get_term_width: called CC=2 SCAR=7.6
320 35 my $options = shift;
321 35 ▶ if (defined $options->{term_width}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{'term_width'}) { } 0 35 35
322 0 return $options->{term_width};
323 } else {
324 35 return 80;
325 }
326 }
327  
328 =head1 AUTHOR
329  
330 Kim Rutherford <kmr+same@xenu.org.uk>
331  
332 =head1 COPYRIGHT & LICENSE
333  
334 Copyright 2005,2006 Kim Rutherford. All rights reserved.
335  
336 This program is free software; you can redistribute it and/or modify it
337 under the same terms as Perl itself.
338  
339 =head1 DISCLAIMER
340  
341 This module is provided "as is" without warranty of any kind. It
342 may redistributed under the same conditions as Perl itself.
343  
344 =cut
345  
346  
347 2 1;
↑ Summary Same.pm »