blib/lib/Test/Unit/TkTestRunner.pm untested

filter: Statementstmt 0.0% 0 / 389 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 36 totaltotal 0.0% 0 / 568 CC 36 SCAR 35.3
CC
36
Cov
0%
_arrange
182.0
run_suite
30.0
choose_file
20.0
CRAP 34.1
1 #!/usr/bin/perl -w
2  
3 package Test::Unit::TkTestRunner;
4 BEGIN {
5 $Test::Unit::TkTestRunner::VERSION = '0.25_1325'; # added by dist-tools/SetVersion.pl
6 }
7  
8 use strict;
9  
10 use base qw(Test::Unit::Runner);
11  
12 use Tk;
13 use Tk::BrowseEntry;
14 use Benchmark;
15  
16 use Test::Unit; # for copyright & version number
17 use Test::Unit::Result;
18 use Test::Unit::Loader;
19  
20 sub new {
21 my $self = bless {}, shift;
22 return $self;
23 }
24  
25 sub about {
26 my $self = shift;
27 my $dialog = $self->{frame}->DialogBox(
28 -title => 'About PerlUnit',
29 -buttons => [ 'OK' ]
30 );
31 my $text = $dialog->add("ROText"); #, -width => 80, -height => 20);
32 $text->insert("end", Test::Unit::COPYRIGHT_NOTICE);
33 $text->pack();
34 $dialog->Show();
35 }
36  
37 sub add_error {
38 my $self = shift;
39 $self->{number_of_errors} = $self->{result}->error_count();
40 $self->append_failure("Error", @_);
41 $self->update();
42 }
43  
44 sub add_failure {
45 my $self = shift;
46 $self->{number_of_failures} = $self->{result}->failure_count();
47 $self->append_failure("Failure", @_);
48 $self->update();
49 }
50  
51 sub append_failure {
52 my ($self, $kind, $test, $exception)=@_;
53 my $message = $test->name(); #bad juju!!
54 if ($message) {
55 $kind .= ":".substr($message, 0, 100);
56 }
57 $self->{failure_list}->insert("end", $message);
58 push @{$self->{failed_tests}}, $test;
59 push @{$self->{exceptions}}, $exception;
60 }
61  
62 sub plan{
63 my $self = shift;
64 $self->{planned} = shift;
65 }
66  
67  
68 sub choose_file {
69 my $self = shift;
70 my $name = $self->{suite_name};
71 my @types = ([ 'All Files', '*' ]);
72 my $dir = undef;
73 if (defined $name) {
74 require File::Basename;
75 my $sfx;
76 ($name, $dir, $sfx) = File::Basename::fileparse($name, '\..*');
77 if (defined($sfx) && length($sfx)) {
78 unshift(@types, [ 'Similar Files', [$sfx]]);
79 $name .= $sfx;
80 }
81 }
82 my $file = $self->{frame}->getOpenFile(
83 -title => "Select test case",
84 -initialdir => $dir,
85 -initialfile => $name,
86 -filetypes => \@types
87 );
88 if (defined $file) {
89 $file=~s/\/+/\//g;
90 }
91 $self->{suite_name} = $file;
92 }
93  
94 sub create_punit_menu {
95 my $self = shift;
96 my $main_menu = $self->{frame}->Menu(
97 -type => 'menubar',
98 -menuitems => [
99 [
100 cascade => 'F~ile',
101 -menuitems => [
102 [
103 command => 'O~pen',
104 -command => sub { $self->choose_file() }
105 ],
106 [
107 command => 'Ex~it',
108 -command => sub { $self->{frame}->destroy() }
109 ],
110 ],
111 ],
112 [
113 cascade => 'H~elp',
114 -menuitems => [
115 [
116 command => 'A~bout PerlUnit',
117 -command => sub { $self->about() }
118 ],
119 ],
120 ],
121 ],
122 );
123 return $main_menu;
124 }
125  
126 sub create_menus {
127 my $self = shift;
128 $self->{frame}->configure(-menu => $self->create_punit_menu());
129 }
130  
131 sub create_ui {
132 my $self = shift;
133 # Lay the window out....
134 my $mw = $self->{frame} = MainWindow->new(
135 -title => 'Run Test Suite',
136 -width => 200
137 );
138 # I need stretchy labels, Tk doesnt have them
139 my $mklabel = sub {
140 my (@args)=@_;
141 $self->{$args[0]} = $args[2];
142 $mw->Entry(
143 -textvariable => \$self->{$args[0]},
144 -justify => $args[1],
145 -relief => 'flat',
146 -state => 'disabled'
147 );
148 };
149 $self->create_menus();
150 $self->{suite_label} = $mw->Label(
151 -text => 'Enter the name of the TestCase:'
152 );
153 $self->{suite_name} = "x";
154 $self->{suite_field} = $mw->BrowseEntry(
155 -textvariable => \$self->{suite_name},
156 -choices => [],
157 );
158 $self->{add_text_listener} = sub { $self->run_suite() };
159 $self->{run} = $mw->Button(
160 -text => 'Run',
161 -state => 'normal',
162 -command => sub { $self->run_suite() }
163 );
164
165 my $lab1 = $mw->Label(-text => "Runs:");
166 my $lab2 = &{$mklabel}('number_of_runs', 'right', 0);
167 my $lab3 = $mw->Label(-text => "Errors:");
168 my $lab4 = &{$mklabel}('number_of_errors', 'right', 0);
169 my $lab5 = $mw->Label(-text => "Failures:");
170 my $lab6 = &{$mklabel}('number_of_failures', 'right', 0);
171  
172 $self->{progress_bar} = $mw->ArrayBar(
173 -width => 20,
174 -length => 400,
175 -colors => [ 'green', 'red', 'grey' ]
176 );
177 $self->{failure_label} = $mw->Label(
178 -text => 'Errors and Failures:',
179 -justify => 'left'
180 );
181 $self->{failure_list} = $mw->Scrolled('Listbox', -scrollbars => 'e');
182 $self->{failure_list}->insert("end", "", "", "", "", "", "");
183
184 $self->{quit_button} = $mw->Button(
185 -text => 'Quit',
186 -command => sub { $mw->destroy() }
187 );
188  
189 $self->{rerun_button} = $mw->Button(
190 -text => 'ReRun',
191 -state => 'normal',
192 -command => sub { $self->rerun() }
193 );
194 $self->{show_error_button} = $mw->Button(
195 -text => 'Show...',
196 -state => 'normal',
197 -command => sub { $self->show_error_trace() }
198 );
199  
200  
201 $self->{status_line_box}= &{$mklabel}('status_line', 'left', 'Status line');
202 $self->{status_line_box}->configure(-relief => 'sunken', -bg => 'grey');
203
204 # Bindings go here, so objects are already defined.
205 $self->{failure_list}->bind('<Double-1>' => sub { $self->show_error_trace() });
206  
207 # all geometry management BELOW this point. Otherwise bindings
208 # wont work.
209 $self->{suite_label}->form(
210 -left => [ '%0' ],
211 -top => [ '%0' ],
212 -fill => 'x'
213 );
214 $self->{run}->form(
215 -right => [ '%100' ],
216 -top => [ $self->{suite_label} ],
217 );
218 $self->{suite_field}->form(
219 -left => [ '%0' ],
220 -right => [$self->{run}],
221 -top => [$self->{suite_label}], -fill => 'x'
222 );
223
224 $lab1->form(-left => ['%0'], -top => [$self->{suite_field}, 10]);
225 $lab2->form(-left => [$lab1], -top => [$self->{suite_field}, 10], -fill => 'x');
226 $lab3->form(-left => [$lab2], -top => [$self->{suite_field}, 10]);
227 $lab4->form(-left => [$lab3], -top => [$self->{suite_field}, 10], -fill => 'x');
228 $lab5->form(-left => [$lab4], -top => [$self->{suite_field}, 10]);
229 $lab6->form(-left => [$lab5], -top => [$self->{suite_field}, 10], -fill => 'x');
230  
231  
232 $self->{progress_bar}->form(-left => [ '%0' ], -top => [$lab6, 10]);
233 $self->{failure_label}->form(
234 -left => [ '%0' ],
235 -top => [$self->{progress_bar}, 10],
236 -right => [ '%100' ]
237 );
238 $self->{failure_list}->form(
239 -left => [ '%0' ],
240 -top => [$self->{failure_label}],
241 -right => [ '%100' ],
242 -fill => 'both'
243 );
244 # this is in a wierd order 'cos Quit keeps trying to resize.
245 $self->{quit_button}->form(
246 -right => [ '%100' ],
247 -bottom => [ '%100' ],
248 -fill => 'none'
249 );
250 $self->{show_error_button}->form(
251 -right => [ '%100' ],
252 -bottom => [$self->{quit_button}],
253 -top => [$self->{failure_list}]
254 );
255 # Rerun doesn't work yet.
256 # $self->{rerun_button}->form(
257 # -right => [$self->{show_error_button}],
258 # -top => [$self->{failure_list}]
259 # );
260
261 $self->{status_line_box}->form(
262 -left => [ '%0' ],
263 -right => [$self->{quit_button}],
264 -bottom => [ '%100' ],
265 -top => [$self->{show_error_button}],
266 -fill => 'x'
267 );
268  
269 $self->reset();
270 return $mw;
271 }
272  
273 sub end_test {
274 my $self = shift;
275 $self->{runs} = $self->{result}->run_count();
276 $self->update();
277 }
278  
279 sub get_test {
280 my $self = shift;
281 my $suite = Test::Unit::Loader->obj_load(shift);
282 $self->{status_line}="";
283 return $suite;
284 }
285  
286 sub is_error_selected {
287 my $self = shift;
288 ($self->{listbox}->curselection>=0)?1:0;
289 }
290  
291 sub load_frame_icon {
292 # not implemented
293 }
294  
295 sub main {
296 my $main = new Test::Unit::TkTestRunner()->start(@_);
297 }
298  
299 sub rerun {
300 # not implemented and not going to!
301 my $self = shift;
302 my $index = $self->{failure_list}->curselection;
303 return if $index < 0;
304 my $test = $self->{failed_tests}->[$index];
305 #if (! $test->isa("Test::Unit::TestCase")) {
306 $self->show_status("Could not reload test.");
307 #}
308 # Not sure how to do this...
309 }
310  
311 sub reset {
312 my $self = shift;
313 $self->{number_of_errors} = 0;
314 $self->{number_of_failures} = 0;
315 $self->{number_of_runs} = 0;
316 $self->{planned} = 0;
317 $self->{failure_list}->delete(0, "end");
318 $self->{exceptions} = [];
319 $self->{failed_tests} = [];
320 $self->{progress_bar}->value(0, 0, 1);
321 }
322  
323 sub run {
324 my $self = shift;
325 $self->run_suite();
326 }
327  
328 sub run_failed {
329 my $self = shift;
330 # not implemented
331 }
332  
333 sub run_suite {
334 my $self = shift;
335 my $suite;
336 if (defined($self->{runner})) {
337 $self->{result}->stop();
338 }
339 else {
340 $self->add_to_history();
341 $self->{run}->configure(-text => "Stop");
342 $self->show_info("Initializing...");
343 $self->reset();
344 $self->show_info("Load Test Case...");
345 eval {
346 $suite = $self->get_test($self->{suite_name});
347 };
348 if ($@ or !$suite) {
349 $suite = undef;
350 $self->show_status("Could not load test!");
351 }
352 if ($suite) {
353 $self->{runner} = 1;
354 $self->{planned} = $suite->count_test_cases();
355 $self->{result} = $self->create_test_result();
356 $self->{result}->add_listener($self);
357 $self->show_info("Running...");
358 $self->{start_time} = new Benchmark();
359 $suite->run($self->{result});
360 if ($self->{result}->should_stop()) {
361 $self->show_status("Stopped");
362 }
363 else {
364 $self->{finish_time} = new Benchmark();
365 $self->{run_time} = timediff($self->{finish_time},
366 $self->{start_time});
367 $self->show_info("Finished: ".timestr($self->{run_time}, 'nop'));
368 }
369 }
370 $self->{runner} = undef;
371 $self->{result} = undef;
372 $self->{run}->configure(-text => "Run");
373 }
374 }
375  
376 sub show_error_trace {
377 # pop up a text dialog containing the details.
378 my $self = shift;
379 my $dialog = $self->{frame}->DialogBox(
380 -title => 'Details',
381 -buttons => [ 'OK' ]
382 );
383 my $selected = $self->{failure_list}->curselection;
384 if (ref($selected) eq 'ARRAY') {
385 # new Tk returns array of int...?
386 ($selected) = @$selected; # take the first, ignore the rest
387 }
388 return unless defined($selected) && $self->{exceptions}[$selected];
389 my $text = $dialog->add("Scrolled", "ROText", -width => 80, -height => 20)
390 ->pack(-expand => 1, -fill => 'both');
391 $text->insert("end", $self->{exceptions}[$selected]->to_string());
392  
393 my $e = $self->{exceptions}[$selected];
394 if ($e->object->annotations()) {
395 foreach my $data ("\n\nAnnotations:\n", $e->object->annotations()) {
396 $text->insert("end", $data); # third arg would be a tag
397 }
398 }
399  
400 $dialog->Show();
401 }
402  
403 sub show_info {
404 my $self = shift;
405 $self->{status_line} = shift;
406 $self->{status_line_box}->configure(-bg => 'grey');
407 }
408  
409 sub show_status {
410 my $self = shift;
411 $self->{status_line} = shift;
412 $self->{status_line_box}->configure(-bg => 'red');
413 }
414  
415 sub start {
416 my $self = shift;
417 my (@args)=@_;
418 my $mw = $self->create_ui();
419 if (@args) {
420 $self->{suite_name} = shift @args;
421 }
422 MainLoop;
423 }
424  
425 sub start_test {
426 my $self = shift;
427 my $test = shift;
428 $self->{number_of_runs} = $self->{result}->run_count();
429 $self->show_info("Running: " . $test->name());
430 }
431  
432 sub add_pass {
433 my $self = shift;
434 my ($test, $exception)=@_;
435 $self->update();
436 }
437  
438 sub update {
439 my $self = shift;
440 my $result = $self->{result};
441 my $total = $result->run_count();
442 my $failures = $result->failure_count();
443 my $errors = $result->error_count();
444 my $passes = $total-$failures-$errors;
445 my $bad = $failures+$errors;
446 #$passes = $result->run_count();
447 my $todo = ($total>$self->{planned})?0:$self->{planned}-$total;
448 $self->{progress_bar}->value($passes, $bad, $todo);
449 # force entry into the event loop.
450 # this makes it nearly like its threaded...
451 #sleep 1;
452 $self->{frame}->update();
453 }
454  
455 sub add_to_history {
456 my $self = shift;
457 my $new_item = $self->{suite_name};
458 my $h = $self->{suite_field};
459 my $choices = $h->cget('-choices');
460 my @choices = ();
461 if (ref($choices)) {
462 @choices=@{$h->cget('-choices')};
463 }
464 elsif ($choices) {
465 # extraordinarily bad - choices is a scalar if theres
466 # only one, and undefined if there are none!
467 @choices = ($h->cget('-choices'));
468 }
469 @choices = ($new_item, grep {$_ ne $new_item} @choices);
470 if (@choices>10) {
471 @choices=@choices[0..9];
472 }
473 $h->configure(-choices => \@choices);
474 }
475  
476 package Tk::ArrayBar;
477 # progressbar doesnt cut it.
478 # This expects a variable which is an array ref, and
479 # a matching list of colours. Sortof like stacked progress bars.
480 # Heavily - ie almost totally - based on the code in ProgressBar.
481 use Tk;
482 use Tk::Canvas;
483 use Tk::ROText;
484 use Tk::DialogBox;
485 use Carp;
486 use strict;
487  
488 use base qw(Tk::Derived Tk::Canvas);
489  
490 Construct Tk::Widget 'ArrayBar';
491  
492 sub ClassInit {
493 my ($class, $mw) = @_;
494
495 $class->SUPER::ClassInit($mw);
496
497 $mw->bind($class, '<Configure>', [ '_layoutRequest', 1 ]);
498 }
499  
500 sub Populate {
501 my($c, $args) = @_;
502
503 $c->ConfigSpecs(
504 -width => [ PASSIVE => undef, undef, 0 ],
505 '-length' => [ PASSIVE => undef, undef, 0 ],
506 -padx => [ PASSIVE => 'padX', 'Pad', 0 ],
507 -pady => [ PASSIVE => 'padY', 'Pad', 0 ],
508 -colors => [ PASSIVE => undef, undef, undef ],
509 -relief => [ SELF => 'relief', 'Relief', 'sunken' ],
510 -value => [ METHOD => undef, undef, undef ],
511 -variable => [ PASSIVE => undef, undef, [ 0 ] ],
512 -anchor => [ METHOD => 'anchor', 'Anchor', 'w' ],
513 -resolution => [ PASSIVE => undef, undef, 1.0 ],
514 -highlightthickness => [
515 SELF => 'highlightThickness', 'HighlightThickness', 0
516 ],
517 -troughcolor => [
518 PASSIVE => 'troughColor', 'Background', 'grey55'
519 ],
520 );
521
522 _layoutRequest($c, 1);
523 $c->OnDestroy([ Destroyed => $c ]);
524 }
525  
526 sub anchor {
527 my $c = shift;
528 my $var = \$c->{Configure}{'-anchor'};
529 my $old = $$var;
530
531 if (@_) {
532 my $new = shift;
533 croak "bad anchor position \"$new\": must be n, s, w or e"
534 unless $new =~ /^[news]$/;
535 $$var = $new;
536 }
537
538 $old;
539 }
540  
541 sub _layoutRequest {
542 my $c = shift;
543 my $why = shift;
544 $c->afterIdle([ '_arrange', $c ]) unless $c->{layout_pending};
545 $c->{layout_pending} |= $why;
546 }
547  
548 sub _arrange {
549 my $c = shift;
550 my $why = $c->{layout_pending};
551
552 $c->{layout_pending} = 0;
553
554 my $w = $c->Width;
555 my $h = $c->Height;
556 my $bw = $c->cget('-borderwidth') + $c->cget('-highlightthickness');
557 my $x = abs(int($c->{Configure}{'-padx'})) + $bw;
558 my $y = abs(int($c->{Configure}{'-pady'})) + $bw;
559 my $value = $c->cget('-variable');
560 my $horz = $c->{Configure}{'-anchor'} =~ /[ew]/i ? 1 : 0;
561 my $dir = $c->{Configure}{'-anchor'} =~ /[ne]/i ? -1 : 1;
562
563 if ($w == 1 && $h == 1) {
564 my $bw = $c->cget('-borderwidth');
565 $h = $c->pixels($c->cget('-length')) || 40;
566 $w = $c->pixels($c->cget('-width')) || 20;
567
568 ($w, $h) = ($h, $w) if $horz;
569 $c->GeometryRequest($w, $h);
570 $c->parent->update;
571 $c->update;
572
573 $w = $c->Width;
574 $h = $c->Height;
575 }
576
577 $w -= $x*2;
578 $h -= $y*2;
579
580 my $length = $horz ? $w : $h;
581 my $width = $horz ? $h : $w;
582 # at this point we have the length and width of the
583 # bar independent of orientation and padding.
584 # blocks and gaps are not used.
585
586 # unlike progressbar I need to redraw these each time.
587 # actually resizing them might be better...
588 my $colors = $c->{Configure}{'-colors'} || [ 'green', 'red', 'grey55' ];
589 $c->delete($c->find('all'));
590 $c->createRectangle(
591 0, 0, $w+$x*2, $h+$y*2,
592 -fill => $c->{Configure}{'-troughcolor'},
593 -width => 0,
594 -outline => undef
595 );
596 my $total;
597 my $count_value = scalar(@$value)-1;
598 foreach my $val (@$value) {
599 $total += $val > 0 ? $val : 0;
600 }
601 # prevent div by zero and give a nice initial appearance.
602 $total = $total ? $total : 1;
603 my $curx = $x;
604 my $cury = $y;
605 foreach my $index (0..$count_value) {
606 my $size = ($length*$value->[$index])/$total;
607 my $ud = $horz?$width:$size;
608 my $lr = $horz?$size:$width;
609 $c->{cover}->[$index] = $c->createRectangle(
610 $curx, $cury, $curx+$lr-1, $cury+$ud-1,
611 -fill => $colors->[$index],
612 -width => 1,
613 -outline => 'black'
614 );
615 $curx+=$horz?$lr:0;
616 $cury+=$horz?0:$ud;
617 }
618 }
619  
620 sub value {
621 my $c = shift;
622 my $val = $c->cget('-variable');
623
624 if (@_) {
625 $c->configure(-variable => [@_]);
626 _layoutRequest($c, 2);
627 }
628 }
629  
630 sub Destroyed {
631 my $c = shift;
632 my $var = delete $c->{'-variable'};
633 untie $$var if defined($var) && ref($var);
634 }
635  
636 1;
637 __END__