Debian Codegraph @

t/trace1.t

raw
1 BEGIN { $|=1; $^W=1; }
2 use Test;
3 use Tk;
4 use Tk::Trace;
5 use strict;
6
7 plan test => 18;
8
9 my $mw = MainWindow->new;
10 $mw->geometry("+10+10");
11 my $v = 0;
12
13 my $e = $mw->Entry(-textvariable => \$v)->pack;
14
15 my ($r, $w, $u ) = (0, 0, 0 );
16 $e->traceVariable( \$v, 'rwu', [ \&trace_v, $e, 123 ] );
17
18 foreach my $k ( 1 .. 5 ) {
19 $mw->after(100);
20 $v++;
21 $mw->update;
22 }
23
24 $e->traceVdelete( \$v );
25 ok( $v, 5, 'traced variable != 5' );
26 ok( $r, 5, 'read trace not called 5 times' );
27 ok( $w, 6, 'write trace not called 5 times' );
28 ok( $u, 1, 'undef trace not called once' );
29
30 my $read_only = $v;
31 $e->traceVariable( \$v, 'w', sub { $read_only } );
32 $v = 777;
33 $e->traceVdelete( \$v );
34 ok( $v, 5, 'read-only variable failed != 5' );
35
36 ok( $v, 5, 'final value != 5' );
37
38 if ($Tk::VERSION < 804.027501) {
39 warn "# This test segfaults in Tk804.027\n";
40 ok(1);
41 } else {
42 # Similar code is part of the CPAN module Tk::LCD
43 my $c = $mw->Canvas->pack;
44 my $foo;
45 my $vref = \$foo;
46 my $st = [sub {
47 my ($index, $new_val, $op, $lcd) = @_;
48 return unless $op eq 'w';
49 # Problem: $c is not alive (or half-alive only) here
50 # and internal data structures seem not to be valid
51 $c->move("foo", 20,30);
52 $new_val;
53 }, $c];
54 $c->traceVariable($vref, 'w' => $st);
55 $c->{watch} = $vref;
56 $c->createPolygon(10,10,20,10,20,20,10,20,-tags=>"foo");
57 $c->OnDestroy( [sub {$_[0]->traceVdelete($_[0]->{watch})}, $c] );
58 $c->update;
59 $c->destroy;
60 ok(1);
61 }
62
63 sub trace_v {
64
65 my( $index, $value, $op, $ent, $num ) = @_;
66
67 if ( $op eq 'r' ) {
68 ok( $e, $ent, 'arguments out of order' );
69 $r++;
70 } elsif ( $op eq 'w' ) {
71 ok( $num, 123, '$num != 123' );
72 $w++;
73 } elsif ( $op eq 'u' ) {
74 $u++;
75 }
76
77 $value;
78
79 }
80