Debian Codegraph @

t/canvas2.t

raw
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 #
5 # $Id: $
6 #
7
8 use strict;
9
10 use Data::Dumper;
11 use Getopt::Long;
12 use Tk;
13 use Tk::Balloon;
14 use Tk::Canvas;
15
16 BEGIN {
17 if (!eval q{
18 use Test::More;
19 1;
20 }) {
21 print "1..0 # skip: no Test::More module\n";
22 exit;
23 }
24 }
25
26 my @dashes1 = ([4,4],
27 [2,2],
28 [1,1],
29 [8,8],
30 [16,16],
31 [1,2,3,4,5,6],
32 '.',
33 '. ',
34 ',',
35 ', ',
36 '-',
37 '- ',
38 '_',
39 '_ ',
40 '.,-_',
41 #'.,-_.,-_.,-_.,-_', # dumps core!
42 );
43
44 my @dashes2 = (
45 # now equivalent pairs
46 '.', [2,4],
47 '-', [6,4],
48 '-.', [6,4,2,4],
49 '-..', [6,4,2,4,2,4],
50 '. ', [2,8],
51 ',', [4,4],
52 );
53
54 my @dashes;
55 push @dashes, map { +{dash => $_, width => 1} } @dashes1;
56 push @dashes, map { +{dash => $_, width => 2} } @dashes2;
57
58 plan tests => 6 + 3 * @dashes;
59
60 my $show;
61 GetOptions("show!" => \$show)
62 or die "usage: $0 [-show]";
63
64 my $mw = MainWindow->new;
65 $mw->geometry("+10+10");
66 my $b = $mw->Balloon(-initwait => 50, -balloonposition => 'mouse');
67 $b->Subwidget("message")->configure(-font => "monospace");
68
69 {
70 local $TODO;
71 $TODO = "Dash errors not yet corrected" if $Tk::VERSION < 804.027502;
72
73 my $c = $mw->Canvas;
74 eval { $c->createLine(0,0,1,1,-dash => [4]) };
75 like($@, qr{\Qbad dash list "4": must be a list of integers or a format like "-.."}, "error with one number in list");
76 eval { $c->createLine(0,0,1,1,-dash => 4) };
77 like($@, qr{\Qbad dash list "4": must be a list of integers or a format like "-.."}, "error with one number in list");
78 eval { $c->createLine(0,0,1,1,-dash => ',;') };
79 like($@, qr{\Qbad dash list ",;": must be a list of integers or a format like "-.."}, "error with bad dash list");
80 eval { $c->createLine(0,0,1,1,-dash => ['x',4]) };
81 like($@, qr{\Qexpected integer in the range 1..255 but got "x"}, "non-integer in dash list");
82 }
83
84 {
85 use constant DIR_HORIZ => 0;
86 use constant DIR_DIAG => 1;
87 use constant DIR_VERT => 2;
88
89 my @c;
90 my @itemlabel;
91 for (0 .. 2) {
92 $c[$_] = $mw->Canvas(-height => 20 + 6 * @dashes)->pack;
93 $itemlabel[$_] = {};
94 $b->attach($c[$_], -msg => $itemlabel[$_]);
95 }
96
97 $mw->update; # to refresh Width/Height
98 my $c_x2 = $c[0]->Width - 10;
99 my $c_y2 = $c[0]->Height - 10;
100
101 for my $dir (DIR_HORIZ, DIR_DIAG, DIR_VERT) {
102 my $x = 10;
103 my $y = 10;
104 my $x2 = $dir == DIR_VERT ? $x : $c_x2;
105 my $y2 = $dir == DIR_HORIZ ? $y : $dir == DIR_DIAG ? $y + 20 : $c_y2;
106 my $xd = $dir == DIR_VERT ? 6 : 0;
107 my $yd = $dir == DIR_VERT ? 0 : 6;
108 for my $def (@dashes) {
109 my($dash, $width) = @{$def}{qw(dash width)};
110 my $dash_printable = Data::Dumper->new([$dash],['dash'])->Indent(0)->Dump;
111 my $item = $c[$dir]->createLine($x, $y, $x2, $y2, -width => $width, -dash => $dash);
112 pass("Created dash $dash_printable (direction=$dir)");
113 $itemlabel[$dir]->{$item} = $dash_printable;
114
115 $x +=$xd;
116 $x2+=$xd;
117 $y +=$yd;
118 $y2+=$yd;
119 }
120 }
121 }
122
123 {
124 eval { $mw->Canvas(-scrollregion => "1 2") };
125 like $@, qr{bad scrollRegion "1 2"}, "Expected error message for bad scrollregion";
126 }
127
128 {
129 my $c = $mw->Canvas;
130 my $item = $c->createText(0,0,-text=>"foo");
131 eval { $c->select('clear',$item,"bla","foo") };
132 # This used to segfault for Tk <= 804.029_501
133 like $@, qr{\Qwrong # args: should be ".canvas\E\d+\Q select clear tagOrId index"}, 'select clear error message';
134 $c->destroy;
135 }
136
137 if (!$show) {
138 $mw->after(1000, sub { $mw->destroy });
139 }
140 MainLoop;
141
142 __END__