File: GtkSimpleList.t

package info (click to toggle)
libgtk2-perl 1%3A1.190-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,132 kB
  • ctags: 516
  • sloc: perl: 16,575; sh: 149; ansic: 148; makefile: 76
file content (337 lines) | stat: -rw-r--r-- 9,743 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# vim: set syntax=perl :
#
# $Header: /cvsroot/gtk2-perl/gtk2-perl-xs/Gtk2/t/GtkSimpleList.t,v 1.8 2005/11/12 21:08:00 kaffeetisch Exp $
#

#########################
# GtkSimpleList Tests
# 	- rm
#########################

use Gtk2::TestHelper tests => 46;

require_ok( 'Gtk2::SimpleList' );

Gtk2::SimpleList->add_column_type(
	'ralacs', 	# think about it for a second...
		type     => 'Glib::Scalar',
		renderer => 'Gtk2::CellRendererText',
		attr     => sub {
			my ($tree_column, $cell, $model, $iter, $i) = @_;
			my ($info) = $model->get ($iter, $i);
			$info = join('',reverse(split('', $info || '' )));
			$cell->set (text => $info );
		}
	);

# add a new type of column that sums up an array reference
Gtk2::SimpleList->add_column_type(
	'sum_of_array',
		type     => 'Glib::Scalar',
		renderer => 'Gtk2::CellRendererText',
		attr     => sub {
			my ($tree_column, $cell, $model, $iter, $i) = @_;
			my $sum = 0;
			my $info = $model->get ($iter, $i);
			foreach (@$info)
			{
				$sum += $_;
			}
			$cell->set (text => $sum);
		}
	);

my $win = Gtk2::Window->new;
$win->set_title('19.GtkSimpleList.t test');
$win->set_default_size(450, 350);

my $vb = Gtk2::VBox->new(0, 6);
$win->add($vb);

my $sw = Gtk2::ScrolledWindow->new;
$sw->set_policy (qw/automatic automatic/);
$vb->pack_start($sw, 1, 1, 0);

ok( my $list = Gtk2::SimpleList->new(
			'Text Field'    => 'text',
			'Int Field'     => 'int',
			'Double Field'  => 'double',
			'Bool Field'    => 'bool',
			'Scalar Field'  => 'scalar',
			'Pixbuf Field'  => 'pixbuf',
			'Ralacs Field'  => 'ralacs',
			'Sum of Array'  => 'sum_of_array',
			'Markup Field'  => 'markup',
		) );
# $sw->add($list);

my $quitbtn = Gtk2::Button->new_from_stock('gtk-quit');
$quitbtn->signal_connect( clicked => sub { Gtk2->main_quit; 1 } );
$vb->pack_start($quitbtn, 0, 0, 0);

# begin exercise of SimpleList

# this could easily fail, so we'll catch and work around it
my $pixbuf;
eval { $pixbuf = $win->render_icon ('gtk-ok', 'menu') };
if( $@ )
{
	$pixbuf = undef;
}
my $undef;
my $scalar = 'scalar';

@{$list->{data}} = (
	[ 'one', 1, 1.1, 1, undef, $pixbuf, undef, [0, 1, 2], '<big>one</big>' ],
	[ 'two', 2, 2.2, 0, undef, undef, $scalar, [1, 2, 3], '<big>two</big>' ],
	[ 'three', 3, 3.3, 1, $scalar, $pixbuf, undef, [2, 3, 4], '<big>three</big>' ],
	[ 'four', 4, 4.4, 0, $scalar, $undef, $scalar, [3, 4, 5], '<big>four</big>' ],
);
ok( scalar(@{$list->{data}}) == 4 );

ok( $list->signal_connect( row_activated => sub
	{
		print STDERR "row_activated: @_";
		1;
	} ) );

my $count = 0;
run_main sub {
		my $ldata = $list->{data};

		ok( scalar(@$ldata) == 4 );

		# test the initial values we put in there
		ok(
			$ldata->[0][0] eq 'one' and
			$ldata->[1][0] eq 'two' and
			$ldata->[2][0] eq 'three' and
			$ldata->[3][0] eq 'four' and
			$ldata->[0][1] == 1 and
			$ldata->[1][1] == 2 and
			$ldata->[2][1] == 3 and
			$ldata->[3][1] == 4 and
			$ldata->[0][2] == 1.1 and
			$ldata->[1][2] == 2.2 and
			$ldata->[2][2] == 3.3 and
			$ldata->[3][2] == 4.4 and
			$ldata->[0][3] == 1 and
			$ldata->[1][3] == 0 and
			$ldata->[2][3] == 1 and
			$ldata->[3][3] == 0 and
			not defined($ldata->[0][4]) and
			not defined($ldata->[1][4]) and
			$ldata->[2][4] eq $scalar and
			$ldata->[3][4] eq $scalar and
			$ldata->[0][5] == $pixbuf and
			not defined($ldata->[1][5]) and
			$ldata->[2][5] == $pixbuf and
			not defined($ldata->[3][5]) and
			eq_array($ldata->[0][7], [0, 1, 2]) and
			eq_array($ldata->[1][7], [1, 2, 3]) and
			eq_array($ldata->[2][7], [2, 3, 4]) and
			eq_array($ldata->[3][7], [3, 4, 5]) and
			$ldata->[0][8] eq '<big>one</big>' and
			$ldata->[1][8] eq '<big>two</big>' and
			$ldata->[2][8] eq '<big>three</big>' and
			$ldata->[3][8] eq '<big>four</big>'
		);

		is (push (@$ldata, [ 'pushed', 1, 0.1, undef ]), 5);
		ok( scalar(@$ldata) == 5 );
		push @$ldata, [ 'pushed', 2, 0.2, undef ];
		ok( scalar(@$ldata) == 6 );
		push @$ldata, [ 'pushed', 3, 0.3, undef ];
		ok( scalar(@$ldata) == 7 );

		ok (eq_array (pop @$ldata, ['pushed', 3, 0.3, 0, 
					undef, undef, undef, undef, undef]));
		ok( scalar(@$ldata) == 6 );
		pop @$ldata;
		ok( scalar(@$ldata) == 5 );
		pop @$ldata;
		ok( scalar(@$ldata) == 4 );

		is (unshift (@$ldata, [ 'unshifted', 1, 0.1, undef ]), 5);
		ok( scalar(@$ldata) == 5 );
		unshift @$ldata, [ 'unshifted', 2, 0.2, undef ];
		ok( scalar(@$ldata) == 6 );
		unshift @$ldata, [ 'unshifted', 3, 0.3, undef ];
		ok( scalar(@$ldata) == 7 );

		ok (eq_array (shift @$ldata, ['unshifted', 3, 0.3, 0, 
					undef, undef, undef, undef, undef]));
		ok( scalar(@$ldata) == 6 );
		shift @$ldata;
		ok( scalar(@$ldata) == 5 );
		shift @$ldata;
		ok( scalar(@$ldata) == 4 );

		# make sure we're back to the initial values we put in there
		ok(
			$ldata->[0][0] eq 'one' and
			$ldata->[1][0] eq 'two' and
			$ldata->[2][0] eq 'three' and
			$ldata->[3][0] eq 'four' and
			$ldata->[0][1] == 1 and
			$ldata->[1][1] == 2 and
			$ldata->[2][1] == 3 and
			$ldata->[3][1] == 4 and
			$ldata->[0][2] == 1.1 and
			$ldata->[1][2] == 2.2 and
			$ldata->[2][2] == 3.3 and
			$ldata->[3][2] == 4.4 and
			$ldata->[0][3] == 1 and
			$ldata->[1][3] == 0 and
			$ldata->[2][3] == 1 and
			$ldata->[3][3] == 0 and
			not defined($ldata->[0][4]) and
			not defined($ldata->[1][4]) and
			$ldata->[2][4] eq $scalar and
			$ldata->[3][4] eq $scalar and
			$ldata->[0][5] == $pixbuf and
			not defined($ldata->[1][5]) and
			$ldata->[2][5] == $pixbuf and
			not defined($ldata->[3][5]) and
			eq_array($ldata->[0][7], [0, 1, 2]) and
			eq_array($ldata->[1][7], [1, 2, 3]) and
			eq_array($ldata->[2][7], [2, 3, 4]) and
			eq_array($ldata->[3][7], [3, 4, 5]) and
			$ldata->[0][8] eq '<big>one</big>' and
			$ldata->[1][8] eq '<big>two</big>' and
			$ldata->[2][8] eq '<big>three</big>' and
			$ldata->[3][8] eq '<big>four</big>'
		);

		$ldata->[1][0] = 'getting deleted';
		ok( $ldata->[1][0] eq 'getting deleted' );

		$ldata->[1] = [ 'right now', -1, -1.1, 1, undef ];
		ok(
			$ldata->[1][0] eq 'right now' and
			$ldata->[1][1] == -1 and
			$ldata->[1][2] == -1.1 and
			$ldata->[1][3] == 1
	       	);

		$ldata->[1] = 'bye';
		ok( $ldata->[1][0] eq 'bye' );

		delete $ldata->[1];
		ok( scalar(@$ldata) == 3 );

		ok( exists($ldata->[0]) );
		ok( exists($ldata->[0][0]) );

		@{$list->{data}} = ();
		ok( scalar(@$ldata) == 0 );
		
		push @{$list->{data}}, (
			[ 'pushed', 1, 0.1, undef ],
			[ 'pushed', 2, 0.1, undef ],
			[ 'pushed', 3, 0.1, undef ],
			[ 'pushed', 4, 0.1, undef ],
		);
		unshift @{$list->{data}}, (
			[ 'unshifted', 1, 0.1, undef ],
			[ 'unshifted', 2, 0.1, undef ],
			[ 'unshifted', 3, 0.1, undef ],
			[ 'unshifted', 4, 0.1, undef ],
		);

		is( scalar(@{$list->{data}}), 8 );

		my @ret;
		
		@ret = splice @{$list->{data}}, 2, 2,
				[ 'spliced', 1, 0.1, undef ],
				[ 'spliced', 2, 0.1, undef ];
		is_deeply (\@ret, 
			[ [ 'unshifted', 2, 0.1, 0, 
			    undef, undef, undef, undef, undef ],
			  [ 'unshifted', 1, 0.1, 0, 
			    undef, undef, undef, undef, undef ] ], 'splice @, 2, 2 @');
		
		@ret = splice @{$list->{data}}, -2, 1,
			[ 'negspliced', 1, 0.1, undef ],
			[ 'negspliced', 2, 0.1, undef ],
			[ 'negspliced', 3, 0.1, undef ];
		is_deeply (\@ret, 
			[ [ 'pushed', 3, 0.1, 0, 
			    undef, undef, undef, undef, undef ] ], 'splice @, -2, 1 @');

		@ret = splice @{$list->{data}}, 8;
		is_deeply (\@ret, 
			[ [ 'negspliced', 3, 0.1, 0, 
			    undef, undef, undef, undef, undef ],
			  [ 'pushed', 4, 0.1, 0, 
			    undef, undef, undef, undef, undef ] ], 'splice @, 8');

		@ret = splice @{$list->{data}}, -2;
		is_deeply (\@ret, 
			[ [ 'negspliced', 1, 0.1, 0, 
			    undef, undef, undef, undef, undef ],
			  [ 'negspliced', 2, 0.1, 0, 
			    undef, undef, undef, undef, undef ] ], 'splice @, -2');
		
		@ret = splice @{$list->{data}}, -2, 0, 
			[ 'norem', 1, 0.1, undef ],
			[ 'norem', 2, 0.1, undef ];
		is_deeply (\@ret, [], 'splice @, -2, 0, @');

		@ret = splice @{$list->{data}};
		is_deeply (\@ret, 
			[ [ 'unshifted', 4, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'unshifted', 3, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'spliced', 1, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'spliced', 2, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'norem', 1, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'norem', 2, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'pushed', 1, 0.1, 0,
			    undef, undef, undef, undef, undef ],
			  [ 'pushed', 2, 0.1, 0,
			    undef, undef, undef, undef, undef ] ], 'splice @');
};

# end exercise of SimpleList

ok(1);

# each of these should result in exceptions.
eval { Gtk2::SimpleList->new; };
ok( $@ =~ m/no columns/i, 'no args' );

eval { Gtk2::SimpleList->new ('foo'); };
ok( $@ =~ m/no columns/i, 'odd number of params' );

eval { Gtk2::SimpleList->new ('foo' => 'bar'); };
ok( $@ =~ m/unknown column type/i, 'bad column type' );

eval { Gtk2::SimpleList->new_from_treeview; };
ok( $@ =~ m/not a Gtk2::TreeView/i, 'no args triggers invalid treeview first' );

eval { Gtk2::SimpleList->new_from_treeview ('foo'); };
ok( $@ =~ m/not a Gtk2::TreeView/i, 'invalid treeview reference' );

my $tv = Gtk2::TreeView->new;
eval { Gtk2::SimpleList->new_from_treeview ($tv, 'bar'); };
ok( $@ =~ m/no columns/i, 'odd number of params' );

eval { Gtk2::SimpleList->new_from_treeview ($tv, 'bar', 'baz'); };
ok( $@ =~ m/unknown column type/i, 'unknown column type' );

eval { Gtk2::SimpleList->new_from_treeview ($tv, 'bar', 'text', 'baz'); };
ok( $@ =~ m/expecting pairs/i, 'odd number of params beyond the required first' );

$tv = undef;

__END__

Copyright (C) 2003-2005 by the gtk2-perl team (see the file AUTHORS for the
full list).  See LICENSE for more information.