blib/lib/Test/Tk.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Test::Tk;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 our $VERSION = '3.04';
7  
8 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Test::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use Tk;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 use Exporter;
14 our @ISA = qw(Exporter);
15 our @EXPORT = qw(
16 $app
17 $delay
18 $quitdelay
19 $mwclass
20 @tests
21 $show
22 createapp
23 hashcompare
24 listcompare
25 pause
26 starttesting
27 testaccessors
28 );
29  
30 our $app;
31 our $mwclass = 'Tk::MainWindow';
32 our @tests = ();
33 our $show = 0;
34 our $delay = 100;
35 our $quitdelay = 200;
36  
37 my $arg = shift @ARGV;
38 $show = 1 if (defined($arg) and ($arg eq 'show'));
39  
40 sub createapp {
41 if (($Config{'osname'} eq 'MSWin32') or (exists $ENV{'DISPLAY'})) {
42 eval "use $mwclass";
43 $app = new $mwclass(
44 -width => 200,
45 -height => 125,
46 -title => 'TestSuite',
47 @_
48 );
49 ok(defined $app, "app created");
50 }
51 }
52  
53 sub dotests {
54 if (defined $app) {
55 ok(1, "main loop runs");
56 for (@tests) {
57 my ($call, $expected, $comment) = @$_;
58 my $result = &$call;
59 cmp_deeply($result, $expected, $comment);
60 }
61 $app->after($quitdelay, sub { $app->destroy }) unless $show
62 }
63 }
64  
65 sub hashcompare {
66 my ($h1, $h2) = @_;
67 warn "Depricated 'hashcompare', use Test::Deep";
68 my @l1 = sort keys %$h1;
69 my @l2 = sort keys %$h2;
70 return 0 unless listcompare(\@l1, \@l2);
71 for (@l1) {
72 my $test1 = $h1->{$_};
73 unless (defined $test1) { $test1 = 'UNDEF' }
74 my $test2 = $h2->{$_};
75 unless (defined $test2) { $test2 = 'UNDEF' }
76 if ($test1 =~ /^ARRAY/) {
77 return 0 unless listcompare($test1, $test2)
78 } elsif ($test1 =~ /^HASH/) {
79 return 0 unless hashcompare($test1, $test2)
80 } else {
81 return 0 if $test1 ne $test2
82 }
83 }
84 return 1
85 }
86  
87 sub listcompare {
88 my ($l1, $l2) = @_;;
89 warn "Depricated 'listcompare', use Test::Deep";
90 my $size1 = @$l1;
91 my $size2 = @$l2;
92 if ($size1 ne $size2) { return 0 }
93 foreach my $item (0 .. $size1 - 1) {
94 my $test1 = $l1->[$item];
95 unless (defined $test1) { $test1 = 'UNDEF' }
96 my $test2 = $l2->[$item];
97 unless (defined $test2) { $test2 = 'UNDEF' }
98 if ($test1 =~ /^ARRAY/) {
99 return 0 unless listcompare($test1, $test2)
100 } elsif ($test1 =~ /^HASH/) {
101 return 0 unless hashcompare($test1, $test2)
102 } else {
103 return 0 if $test1 ne $test2
104 }
105 }
106 return 1
107 }
108  
109 sub pause {
110 return unless defined $app;
111 my $milisecs = shift;
112 my $var = 1;
113 $app->after($milisecs, sub { $var = 0 });
114 $app->waitVariable(\$var);
115 }
116  
117 sub starttesting {
118 if (defined $app) {
119 $app->after($delay, \&dotests);
120 $app->MainLoop;
121 } else {
122 my $size = @tests + 2;
123 SKIP: {
124 skip 'No XServer running for this user', $size;
125 }
126 }
127 }
128  
129 sub testaccessors {
130 my $obj = shift;
131 for (@_) {
132 my $method = $_;
133 push @tests, [sub {
134 my $default = $obj->$method;
135 $obj->$method('blieb');
136 my $res1 = $obj->$method;
137 $obj->$method('quep');
138 my $res2 = $obj->$method;
139 $obj->$method($default);
140 return (($res1 eq 'blieb') and ($res2 eq 'quep'));
141 }, 1, "Accessor $method"];
142 }
143 }
144  
145 1;
146 __END__
↑ Summary