blib/lib/Text/Editor/Vip/Buffer/Test.pm

filter: Statementstmt 63.2% 74 / 117 Branchbran 37.5% 12 / 32 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 86.6% 13 / 15 Podpod 100% 6 / 6 totaltotal 58.5% 106 / 181 CC 21 SCAR 27.6
CC
21
Cov
56%
TestDoUndo
36.9
CompareBuffers
7.0
DiagBuffers
6.0
CRAP 15.9
« blib/lib/Text/Editor/Vip/Actions.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/FindReplace.pm »
1  
2 package Text::Editor::Vip::Buffer::Test;
3  
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Data::TreeDumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Data::Hexdumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Text::Diff ;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Test::More ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 BEGIN
14 13 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 $VERSION = 0.01;
18 13 @ISA = qw (Exporter);
19 13 @EXPORT = qw (DiagBuffers TestSerialisation TestDoUndo CompareBuffers);
20 13 @EXPORT_OK = qw ();
21 13 %EXPORT_TAGS = ();
22 }
23  
24 =head1 NAME
25  
26 Text::Editor::Vip::Buffer::Test - Support functions for testing
27  
28 =head1 SYNOPSIS
29  
30 use Test::More qw(no_plan);
31
32 use Text::Editor::Vip::Buffer ;
33 use Text::Editor::Vip::Buffer::Test ;
34
35 my $buffer = new Text::Editor::Vip::Buffer() ;
36
37 is(TestDoUndo('$buffer->DeleteLine(3) ;', '$buffer->Insert("Line 1\nLine 2\nLine 3\nLine 4") ;'), 1, 'test undo after DeleteLine') ;
38  
39 =head1 DESCRIPTION
40  
41 Support functions for testing
42  
43 =head1 FUNCTIONS
44  
45 =cut
46  
47 #-------------------------------------------------------------------------------
48  
49 sub DiagBuffers
50 0 ▶{
Subroutine DiagBuffers: not called CC=2 SCAR=17.9
Pod documented
51  
52 =head2 DiagBuffers
53  
54 Displays the content of the buffer via the diag function.
55  
56 =cut
57  
58 0 my ($package, $file_name, $line) = caller() ;
59  
60 0 my $buffer = shift ;
61 0 ▶my $comment = shift || '' ;
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
62  
63 0 diag("\n$comment\n>>>>>\n" . $buffer->GetText() . "<<<<<\n") ;
64 }
65  
66 #-------------------------------------------------------------------------------------------------
67  
68 sub TestSerialisation
69 7 ▶{
Subroutine TestSerialisation: called CC=3 SCAR=12.6
Pod documented
70  
71 =head2 TestSerialisation
72  
73 Extract the do buffer from the buffer, applies it to another buffer and compares the buffers for equality
74  
75 =cut
76  
77  
78 7 my $buffer = shift ;
79 7 my $new_buffer = $buffer->new() ;
80  
81 7 my $do_buffer = $buffer->GetDoScript() ;
82 7 ▶if($new_buffer->Do($do_buffer))
Branch: 1 branch
BranchTrueFalseTotal
if ($new_buffer->Do($do_buffer)) { } 7 0 7
83 {
84 7 ▶ unless(CompareBuffers("Doing (original, new)", $buffer, $new_buffer))
Branch: 1 branch
BranchTrueFalseTotal
unless (CompareBuffers("Doing (original, new)", $buffer, $new_buffer)) 0 7 7
85 {
86 #~ print "Do buffer:\n" . join("\n", grep{$_ !~ /\s+#/} split("\n",$do_buffer)) . "\n" ;
87 #~ print "Do buffer:\n" . $do_buffer ;
88 0 return(0) ;
89 }
90
91 }
92 else
93 {
94 0 diag("\nFailed Do!\n") ;
95 0 return(0) ;
96 }
97
98 7 return(1) ;
99 }
100  
101 #-------------------------------------------------------------------------------------------------
102  
103 sub TestDoUndo
104 13 ▶{
Subroutine TestDoUndo: called CC=13 SCAR=36.1
Pod documented
105  
106 =head2 TestDoUndo
107  
108 Given a script, obtained from a do buffer for example, this function checks the do and undo buffer creation.
109  
110 An optional setup script can be passed. It is executed first.
111  
112 =cut
113  
114 13 my $script = shift ;
115  
116 13 my $buffer_original = new Text::Editor::Vip::Buffer();
117 13 my $buffer_done = new Text::Editor::Vip::Buffer();
118 13 my $buffer_undone = new Text::Editor::Vip::Buffer();
119  
120 13 my $setup_script = shift ;
121 13 my ($result, $message) ;
122  
123 13 for($buffer_original, $buffer_done, $buffer_undone)
124 {
125 39 ($result, $message) = $_->Do($setup_script) ;
126 39 ▶ die "TestDoUndo: Invalid setup script!\n$message" unless $result ;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 39 39
127 }
128  
129 13 my $pos = $buffer_done->GetDoPosition() ;
130 13 ($result, $message) = $buffer_done->Do($script) ;
131  
132 13 my $error = 0 ;
133  
134 13 ▶if($result)
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 13 0 13
135 {
136 13 my $do_buffer = $buffer_done->GetDoScript($pos) ;
137 13 my $undo_buffer = $buffer_done->GetUndoScript($pos) ;
138
139 # test "do" perl script
140 13 ($result, $message) = $buffer_undone->Do($do_buffer) ;
141
142 13 ▶ if($result)
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 13 0 13
143 {
144 13 ok(1, 'Valid perl do script') ;
145 13 ▶ if(CompareBuffers("Doing (done, undone)", $buffer_done, $buffer_undone))
Branch: 1 branch
BranchTrueFalseTotal
if (CompareBuffers('Doing (done, undone)', $buffer_done, $buffer_undone)) { } 13 0 13
146 {
147 13 ok(1, 'perl do script OK')
148 }
149 else
150 {
151 0 $error++ ;
152 0 diag("\nWould you like a do buffer dump?") ;
153 0 my $answer = <STDIN> ;
154 0 ▶ $answer = 'y' unless defined $answer ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 0 0
155
156 0 ▶ if($answer ne "\n")
Branch: 1 branch
BranchTrueFalseTotal
if ($answer ne "\n") 0 0 0
157 {
158 0 diag("do buffer:\n$do_buffer\n<<<<<\n") ;
159 }
160
161 }
162 }
163 else
164 {
165 0 $error++ ;
166 0 diag($message) ;
167 }
168
169 # test "undo" perl script
170 13 ($result, $message) = $buffer_undone->Do($undo_buffer) ;
171
172 13 ▶ if($result)
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 13 0 13
173 {
174 13 ok(1, 'Valid perl undo script') ;
175
176 13 ▶ if(CompareBuffers("Undoing (original, undone)", $buffer_original, $buffer_undone))
Branch: 1 branch
BranchTrueFalseTotal
if (CompareBuffers('Undoing (original, undone)', $buffer_original, $buffer_undone)) { } 13 0 13
177 {
178 13 ok(1, 'perl undo script OK')
179 }
180 else
181 {
182 0 $error++ ;
183 0 diag("\nWould you like a do and an undo buffer dump?") ;
184 0 my $answer = <STDIN> ;
185 0 ▶ $answer = 'y' unless defined $answer ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $answer 0 0 0
186
187 0 ▶ if($answer ne "\n")
Branch: 1 branch
BranchTrueFalseTotal
if ($answer ne "\n") 0 0 0
188 {
189 0 diag("do buffer:\n$do_buffer\n<<<<<\n") ;
190 0 diag("Undo buffer:\n$undo_buffer\n<<<<<\n") ;
191 }
192
193 }
194 }
195 else
196 {
197 0 $error++ ;
198 0 diag($message) ;
199 }
200
201 13 ▶ if($error)
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 13 13
202 {
203 0 diag("\nBuffer original:\n" . $buffer_original->GetText() . "\n<<<<<\n") ;
204 0 diag("\nBuffer done:\n" . $buffer_done->GetText() . "\n<<<<<\n") ;
205 0 diag("\nBuffer undone:\n" . $buffer_undone->GetText() . "\n<<<<<\n") ;
206 }
207 }
208 else
209 {
210 0 diag("\n Couldn't run TestDoUndo test:\n" . $message) ;
211 0 $error++ ;
212 }
213  
214 13 return(!$error) ;
215 }
216  
217 #-------------------------------------------------------------------------------------------------
218  
219 sub CompareBuffers
220 33 ▶{
Subroutine CompareBuffers: called CC=4 SCAR=19.4
Pod documented
221  
222 =head2 CompareBuffers
223  
224 Compares two buffers for equality. debugging information is displayed if the buffers aren't equal.
225  
226 =cut
227  
228 # help functin to compare two buffers
229  
230 33 my ($message, $lhb, $rhb) = @_ ;
231  
232 33 my ($lhb_line, $lhb_character, $lhb_text) = ($lhb->GetModificationPosition(), $lhb->GetText()) ;
233 33 my ($rhb_line, $rhb_character, $rhb_text) = ($rhb->GetModificationPosition(), $rhb->GetText()) ;
234  
235 # position
236 33 ▶if ($lhb_line != $rhb_line || $lhb_character != $rhb_character)
Branch: 1 branch
BranchTrueFalseTotal
if ($lhb_line != $rhb_line or $lhb_character != $rhb_character) 0 33 33
Condition33%1/3
$lhb_line != $rhb_line or $lhb_character != $rhb_character
l!l&&r!l&&!r
0033
MC/DC0%0/2
$lhb_line != $rhb_line or $lhb_character != $rhb_character
$lhb_line != $rhb_line $lhb_character != $rhb_character
Truth table33%1/3
A or B
A= $lhb_line != $rhb_lineB= $lhb_character != $rhb_character
ABresult
1X1
011
000
237 {
238 0 diag("\n$message: Positon is different! ($lhb_line, $lhb_character) != ($rhb_line, $rhb_character).\n") ;
239
240 0 diag("\nBuffer 1:\n" . hexdump(data => $lhb_text, start_position => 0, end_position => 100)) ;
241 0 diag("\nBuffer 2:\n" . hexdump(data => $rhb_text, start_position => 0, end_position => 100)) ;
242
243 0 return(0) ;
244 }
245  
246 # content
247 33 ▶if($lhb_text ne $rhb_text)
Branch: 1 branch
BranchTrueFalseTotal
if ($lhb_text ne $rhb_text) 0 33 33
248 {
249 0 diag("$message\n" . diff(\$lhb_text, \$rhb_text, {STYLE => 'Table'})) ;
250
251 0 diag("\nBuffer 1:\n" . hexdump(data => $lhb_text, start_position => 0, end_position => 100)) ;
252 0 diag("\nBuffer 2:\n" . hexdump(data => $rhb_text, start_position => 0, end_position => 100)) ;
253
254 0 return(0) ;
255 }
256  
257 33 return(1) ;
258 }
259  
260 #-------------------------------------------------------------------------------
261  
262 sub CompareText
263 73 ▶{
Subroutine CompareText: called CC=2 SCAR=7.0
Pod documented
264  
265 =head2 CompareText
266  
267 Compares the passed text with the content of the buffer. Returns '' if the text is equal to the buffer
268 content. Returns a b<diff > if the texts won't match.
269  
270 =cut
271  
272 73 my $buffer = shift ;
273 73 my $text = shift ;
274  
275 73 my $diff = diff
276 (
277 \($buffer->GetText())
278 , \$text
279 , {STYLE => 'Table', FILENAME_A => "Buffer", FILENAME_B => "Expected", INDEX_LABEL => "Ln"}
280 ) ;
281  
282 73 ▶$diff = "\n" . $diff unless $diff eq '' ;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff eq "" 0 73 73
283  
284 73 return($diff) ;
285 }
286  
287 #-------------------------------------------------------------------------------
288  
289 sub PrintPositionData
290 0 ▶{
Subroutine PrintPositionData: not called CC=2 SCAR=17.9
Pod documented
291  
292 =head2 PrintPositionData
293  
294 Displays the modification and selection information.
295  
296 =cut
297  
298 0 my $buffer = shift ;
299 0 ▶my $message = shift || '' ;
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
300  
301 0 my ($package, $file_name, $line) = caller() ;
302 0 $message .= " @ '$file_name:$line'" ;
303  
304 0 print "\n$message\n\tPosition: " .join(", ", $buffer->GetModificationPosition()) ;
305 0 print "\n\tSelection: " . join(", ", $buffer->GetSelectionBoundaries()) . "\n" ;
306 }
307  
308 #-------------------------------------------------------------------------------
309  
310 13 1;
311  
312 =head1 AUTHOR
313  
314 Khemir Nadim ibn Hamouda
315 CPAN ID: NKH
316 mailto:nadim@khemir.net
317 http:// no web site
318  
319 =head1 COPYRIGHT
320  
321 This program is free software; you can redistribute
322 it and/or modify it under the same terms as Perl itself.
323  
324 The full text of the license can be found in the
325 LICENSE file included with this module.
326  
327 =cut
« blib/lib/Text/Editor/Vip/Actions.pm ↑ Summary blib/lib/Text/Editor/Vip/Buffer/Plugins/FindReplace.pm »