~ubuntu-branches/ubuntu/natty/libchamplain/natty

« back to all changes in this revision

Viewing changes to bindings/perl/Champlain/t/ChamplainPolygon.t

  • Committer: Bazaar Package Importer
  • Author(s): Sjoerd Simons, Laurent Bigonville, Sjoerd Simons
  • Date: 2009-09-15 00:01:41 UTC
  • mfrom: (1.1.3 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090915000141-i8fg5n1t02zxo79m
Tags: 0.4.0-1
[ Laurent Bigonville ]
* debian/control: Add libchamplain-0.3-dev dependency to
  libchamplain-gtk-0.3-dev

[ Sjoerd Simons ]
* New upstream release (0.4.0)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
 
 
6
use Clutter::TestHelper tests => 50;
 
7
 
 
8
use Champlain;
 
9
 
 
10
my $DEFAULT_FILL_COLOR = Champlain::Polygon->new()->get_fill_color;
 
11
my $DEFAULT_STROKE_NAME = Champlain::Polygon->new()->get_stroke_color;
 
12
 
 
13
exit tests();
 
14
 
 
15
sub tests {
 
16
        test_empty();
 
17
        test_setters();
 
18
        test_points();
 
19
        return 0;
 
20
}
 
21
 
 
22
 
 
23
sub test_empty {
 
24
        my $polygon = Champlain::Polygon->new();
 
25
        isa_ok($polygon, 'Champlain::Polygon');
 
26
        
 
27
        my @points = $polygon->get_points();
 
28
        is_deeply(\@points, [], "No points on a new polygon");
 
29
        
 
30
        $polygon->clear_points();
 
31
        is_deeply(\@points, [], "No points on a cleared polygon");
 
32
        
 
33
        $polygon->clear_points();
 
34
        is_deeply(\@points, [], "No points on a cleared polygon (2 times)");
 
35
 
 
36
        is_color($polygon->get_fill_color, $DEFAULT_FILL_COLOR, "fill_color is set on a new polygon");
 
37
        is_color($polygon->get_stroke_color, $DEFAULT_STROKE_NAME, "stroke_color is set on a new polygon");
 
38
        
 
39
        ok(!$polygon->get_fill, "fill is unset on a new polygon");
 
40
        ok($polygon->get_stroke, "stroke is set on a new polygon");
 
41
        is($polygon->get_stroke_width, 2, "stroke_width is set on a new polygon");
 
42
        
 
43
        # These fields have no accessor yet
 
44
        ok(!$polygon->get('closed-path'), "closed-path is unset on a new polygon");
 
45
        ok($polygon->get('visible'), "closed-path is set on a new polygon");
 
46
}
 
47
 
 
48
 
 
49
sub test_setters {
 
50
        my $polygon = Champlain::Polygon->new();
 
51
        isa_ok($polygon, 'Champlain::Polygon');
 
52
 
 
53
 
 
54
        my $color;
 
55
        
 
56
        $color = Clutter::Color->new(0xaa, 0xdd, 0x37, 0xbb);
 
57
        $polygon->set_fill_color($color);
 
58
        is_color($polygon->get_fill_color, $color, "set_fill_color");
 
59
        
 
60
        $color = Clutter::Color->new(0x44, 0x33, 0x23, 0x2b);
 
61
        $polygon->set_stroke_color($color);
 
62
        is_color($polygon->get_stroke_color, $color, "set_stroke_color");
 
63
        
 
64
        {
 
65
                my $old_fill = $polygon->get_fill;
 
66
                $polygon->set_fill(!$old_fill);
 
67
                is($polygon->get_fill, !$old_fill, "set_fill()");
 
68
        }
 
69
        
 
70
        {
 
71
                my $old_stroke = $polygon->get_stroke;
 
72
                $polygon->set_stroke(!$old_stroke);
 
73
                is($polygon->get_stroke, !$old_stroke, "set_stroke()");
 
74
        }
 
75
 
 
76
        {       
 
77
                my $old_stroke_width = $polygon->get_stroke_width;
 
78
                $polygon->set_stroke_width($old_stroke_width + 2);
 
79
                is($polygon->get_stroke_width, $old_stroke_width + 2, "set_stroke_width()");
 
80
        }
 
81
 
 
82
 
 
83
        $polygon->hide();
 
84
        ok(!$polygon->get('visible'), "hide()");
 
85
        $polygon->show();
 
86
        ok($polygon->get('visible'), "show()");
 
87
}
 
88
 
 
89
 
 
90
sub test_points {
 
91
        my $polygon = Champlain::Polygon->new();
 
92
        isa_ok($polygon, 'Champlain::Polygon');
 
93
 
 
94
        my @remove = ();
 
95
 
 
96
        $polygon->append_point(8, 4);
 
97
        is_polygon(
 
98
                $polygon,
 
99
                [
 
100
                        8, 4,
 
101
                ],
 
102
                "append_point()"
 
103
        );
 
104
 
 
105
        push @remove, $polygon->append_point(4, 9);
 
106
        is_polygon(
 
107
                $polygon,
 
108
                [
 
109
                        8, 4,
 
110
                        4, 9,
 
111
                ],
 
112
                "append_point()"
 
113
        );
 
114
 
 
115
        $polygon->insert_point(7, 10, 1);
 
116
        is_polygon(
 
117
                $polygon,
 
118
                [
 
119
                        8, 4,
 
120
                        7, 10,
 
121
                        4, 9,
 
122
                ],
 
123
                "insert_point() in the middle"
 
124
        );
 
125
 
 
126
        $polygon->append_point(5, 3);
 
127
        is_polygon(
 
128
                $polygon,
 
129
                [
 
130
                        8, 4,
 
131
                        7, 10,
 
132
                        4, 9,
 
133
                        5, 3,
 
134
                ],
 
135
                "polygon: 4 points"
 
136
        );
 
137
 
 
138
        push @remove, $polygon->insert_point(1, 2, 0);
 
139
        is_polygon(
 
140
                $polygon,
 
141
                [
 
142
                        1, 2,
 
143
                        8, 4,
 
144
                        7, 10,
 
145
                        4, 9,
 
146
                        5, 3,
 
147
                ],
 
148
                "insert_point() at the beginning"
 
149
        );
 
150
 
 
151
        $polygon->insert_point(10, 20, 5);
 
152
        is_polygon(
 
153
                $polygon,
 
154
                [
 
155
                        1, 2,
 
156
                        8, 4,
 
157
                        7, 10,
 
158
                        4, 9,
 
159
                        5, 3,
 
160
                        10, 20,
 
161
                ],
 
162
                "insert_point() at the end"
 
163
        );
 
164
 
 
165
        push @remove, $polygon->insert_point(30, 240, 17);
 
166
        is_polygon(
 
167
                $polygon,
 
168
                [
 
169
                        1, 2,
 
170
                        8, 4,
 
171
                        7, 10,
 
172
                        4, 9,
 
173
                        5, 3,
 
174
                        10, 20,
 
175
                        30, 240,
 
176
                ],
 
177
                "insert_point() past the end"
 
178
        );
 
179
 
 
180
        foreach my $point (@remove) {
 
181
                $polygon->remove_point($point);
 
182
        }
 
183
        is_polygon(
 
184
                $polygon,
 
185
                [
 
186
                        8, 4,
 
187
                        7, 10,
 
188
                        5, 3,
 
189
                        10, 20,
 
190
                ],
 
191
                "remove_point()"
 
192
        );
 
193
 
 
194
 
 
195
        # Clear the polygon (it should be empty after)
 
196
        $polygon->clear_points();
 
197
        is_polygon($polygon, [], "clear_points()");
 
198
 
 
199
        $polygon->append_point(100, 200);
 
200
        is_polygon($polygon, [100, 200], "add_point on a cleared polygon");
 
201
}
 
202
 
 
203
 
 
204
#
 
205
# Assert that two colors are identical.
 
206
#
 
207
sub is_polygon {
 
208
        my ($polygon, $expected, $message) = @_;
 
209
 
 
210
        my @points = map { ($_->lat, $_->lon) } $polygon->get_points;
 
211
        is_deeply(\@points, $expected, $message);
 
212
}
 
213
 
 
214
 
 
215
#
 
216
# Assert that two colors are identical.
 
217
#
 
218
sub is_color {
 
219
        my ($got, $expected, $message) = @_;
 
220
        my $tester = Test::Builder->new();
 
221
        my $are_colors = 1;
 
222
        $are_colors &= $tester->is_eq(ref($got), 'Clutter::Color', "$message, got is a Clutter::Color");
 
223
        $are_colors &= $tester->is_eq(ref($expected), 'Clutter::Color', "$message, expected a Clutter::Color");
 
224
 
 
225
        if (! $are_colors) {
 
226
                $tester->ok(0, "$message, can't compare color components") for 1 .. 4;
 
227
                return;
 
228
        }
 
229
 
 
230
        $tester->is_num($got->red, $expected->red, "$message, red matches");
 
231
        $tester->is_num($got->green, $expected->green, "$message, green matches");
 
232
        $tester->is_num($got->blue, $expected->blue, "$message, blue matches");
 
233
        $tester->is_num($got->alpha, $expected->alpha, "$message, alpha matches");
 
234
}