/
draw_test.py
6273 lines (5211 loc) 路 222 KB
/
draw_test.py
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import unittest
import sys
import pygame
from pygame import draw
from pygame import draw_py
from pygame.locals import SRCALPHA
from pygame.tests import test_utils
from pygame.math import Vector2
PY3 = sys.version_info >= (3, 0, 0)
RED = BG_RED = pygame.Color("red")
GREEN = FG_GREEN = pygame.Color("green")
# Clockwise from the top left corner and ending with the center point.
RECT_POSITION_ATTRIBUTES = (
"topleft",
"midtop",
"topright",
"midright",
"bottomright",
"midbottom",
"bottomleft",
"midleft",
"center",
)
def get_border_values(surface, width, height):
"""Returns a list containing lists with the values of the surface's
borders.
"""
border_top = [surface.get_at((x, 0)) for x in range(width)]
border_left = [surface.get_at((0, y)) for y in range(height)]
border_right = [surface.get_at((width - 1, y)) for y in range(height)]
border_bottom = [surface.get_at((x, height - 1)) for x in range(width)]
return [border_top, border_left, border_right, border_bottom]
def corners(surface):
"""Returns a tuple with the corner positions of the given surface.
Clockwise from the top left corner.
"""
width, height = surface.get_size()
return ((0, 0), (width - 1, 0), (width - 1, height - 1), (0, height - 1))
def rect_corners_mids_and_center(rect):
"""Returns a tuple with each corner, mid, and the center for a given rect.
Clockwise from the top left corner and ending with the center point.
"""
return (
rect.topleft,
rect.midtop,
rect.topright,
rect.midright,
rect.bottomright,
rect.midbottom,
rect.bottomleft,
rect.midleft,
rect.center,
)
def border_pos_and_color(surface):
"""Yields each border position and its color for a given surface.
Clockwise from the top left corner.
"""
width, height = surface.get_size()
right, bottom = width - 1, height - 1
# Top edge.
for x in range(width):
pos = (x, 0)
yield pos, surface.get_at(pos)
# Right edge.
# Top right done in top edge loop.
for y in range(1, height):
pos = (right, y)
yield pos, surface.get_at(pos)
# Bottom edge.
# Bottom right done in right edge loop.
for x in range(right - 1, -1, -1):
pos = (x, bottom)
yield pos, surface.get_at(pos)
# Left edge.
# Bottom left done in bottom edge loop. Top left done in top edge loop.
for y in range(bottom - 1, 0, -1):
pos = (0, y)
yield pos, surface.get_at(pos)
def get_color_points(surface, color, bounds_rect=None, match_color=True):
"""Get all the points of a given color on the surface within the given
bounds.
If bounds_rect is None the full surface is checked.
If match_color is True, all points matching the color are returned,
otherwise all points not matching the color are returned.
"""
get_at = surface.get_at # For possible speed up.
if bounds_rect is None:
x_range = range(surface.get_width())
y_range = range(surface.get_height())
else:
x_range = range(bounds_rect.left, bounds_rect.right)
y_range = range(bounds_rect.top, bounds_rect.bottom)
surface.lock() # For possible speed up.
if match_color:
pts = [(x, y) for x in x_range for y in y_range if get_at((x, y)) == color]
else:
pts = [(x, y) for x in x_range for y in y_range if get_at((x, y)) != color]
surface.unlock()
return pts
def create_bounding_rect(surface, surf_color, default_pos):
"""Create a rect to bound all the pixels that don't match surf_color.
The default_pos parameter is used to position the bounding rect for the
case where all pixels match the surf_color.
"""
width, height = surface.get_clip().size
xmin, ymin = width, height
xmax, ymax = -1, -1
get_at = surface.get_at # For possible speed up.
surface.lock() # For possible speed up.
for y in range(height):
for x in range(width):
if get_at((x, y)) != surf_color:
xmin = min(x, xmin)
xmax = max(x, xmax)
ymin = min(y, ymin)
ymax = max(y, ymax)
surface.unlock()
if -1 == xmax:
# No points means a 0 sized rect positioned at default_pos.
return pygame.Rect(default_pos, (0, 0))
return pygame.Rect((xmin, ymin), (xmax - xmin + 1, ymax - ymin + 1))
class InvalidBool(object):
"""To help test invalid bool values."""
__nonzero__ = None
__bool__ = None
class DrawTestCase(unittest.TestCase):
"""Base class to test draw module functions."""
draw_rect = staticmethod(draw.rect)
draw_polygon = staticmethod(draw.polygon)
draw_circle = staticmethod(draw.circle)
draw_ellipse = staticmethod(draw.ellipse)
draw_arc = staticmethod(draw.arc)
draw_line = staticmethod(draw.line)
draw_lines = staticmethod(draw.lines)
draw_aaline = staticmethod(draw.aaline)
draw_aalines = staticmethod(draw.aalines)
class PythonDrawTestCase(unittest.TestCase):
"""Base class to test draw_py module functions."""
# draw_py is currently missing some functions.
# draw_rect = staticmethod(draw_py.draw_rect)
draw_polygon = staticmethod(draw_py.draw_polygon)
# draw_circle = staticmethod(draw_py.draw_circle)
# draw_ellipse = staticmethod(draw_py.draw_ellipse)
# draw_arc = staticmethod(draw_py.draw_arc)
draw_line = staticmethod(draw_py.draw_line)
draw_lines = staticmethod(draw_py.draw_lines)
draw_aaline = staticmethod(draw_py.draw_aaline)
draw_aalines = staticmethod(draw_py.draw_aalines)
### Ellipse Testing ###########################################################
class DrawEllipseMixin(object):
"""Mixin tests for drawing ellipses.
This class contains all the general ellipse drawing tests.
"""
def test_ellipse__args(self):
"""Ensures draw ellipse accepts the correct args."""
bounds_rect = self.draw_ellipse(
pygame.Surface((3, 3)), (0, 10, 0, 50), pygame.Rect((0, 0), (3, 2)), 1
)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__args_without_width(self):
"""Ensures draw ellipse accepts the args without a width."""
bounds_rect = self.draw_ellipse(
pygame.Surface((2, 2)), (1, 1, 1, 99), pygame.Rect((1, 1), (1, 1))
)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__args_with_negative_width(self):
"""Ensures draw ellipse accepts the args with negative width."""
bounds_rect = self.draw_ellipse(
pygame.Surface((3, 3)), (0, 10, 0, 50), pygame.Rect((2, 3), (3, 2)), -1
)
self.assertIsInstance(bounds_rect, pygame.Rect)
self.assertEqual(bounds_rect, pygame.Rect(2, 3, 0, 0))
def test_ellipse__args_with_width_gt_radius(self):
"""Ensures draw ellipse accepts the args with
width > rect.w // 2 and width > rect.h // 2.
"""
rect = pygame.Rect((0, 0), (4, 4))
bounds_rect = self.draw_ellipse(
pygame.Surface((3, 3)), (0, 10, 0, 50), rect, rect.w // 2 + 1
)
self.assertIsInstance(bounds_rect, pygame.Rect)
bounds_rect = self.draw_ellipse(
pygame.Surface((3, 3)), (0, 10, 0, 50), rect, rect.h // 2 + 1
)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__kwargs(self):
"""Ensures draw ellipse accepts the correct kwargs
with and without a width arg.
"""
kwargs_list = [
{
"surface": pygame.Surface((4, 4)),
"color": pygame.Color("yellow"),
"rect": pygame.Rect((0, 0), (3, 2)),
"width": 1,
},
{
"surface": pygame.Surface((2, 1)),
"color": (0, 10, 20),
"rect": (0, 0, 1, 1),
},
]
for kwargs in kwargs_list:
bounds_rect = self.draw_ellipse(**kwargs)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__kwargs_order_independent(self):
"""Ensures draw ellipse's kwargs are not order dependent."""
bounds_rect = self.draw_ellipse(
color=(1, 2, 3),
surface=pygame.Surface((3, 2)),
width=0,
rect=pygame.Rect((1, 0), (1, 1)),
)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__args_missing(self):
"""Ensures draw ellipse detects any missing required args."""
surface = pygame.Surface((1, 1))
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse(surface, pygame.Color("red"))
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse(surface)
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse()
def test_ellipse__kwargs_missing(self):
"""Ensures draw ellipse detects any missing required kwargs."""
kwargs = {
"surface": pygame.Surface((1, 2)),
"color": pygame.Color("red"),
"rect": pygame.Rect((1, 0), (2, 2)),
"width": 2,
}
for name in ("rect", "color", "surface"):
invalid_kwargs = dict(kwargs)
invalid_kwargs.pop(name) # Pop from a copy.
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse(**invalid_kwargs)
def test_ellipse__arg_invalid_types(self):
"""Ensures draw ellipse detects invalid arg types."""
surface = pygame.Surface((2, 2))
color = pygame.Color("blue")
rect = pygame.Rect((1, 1), (1, 1))
with self.assertRaises(TypeError):
# Invalid width.
bounds_rect = self.draw_ellipse(surface, color, rect, "1")
with self.assertRaises(TypeError):
# Invalid rect.
bounds_rect = self.draw_ellipse(surface, color, (1, 2, 3, 4, 5), 1)
with self.assertRaises(TypeError):
# Invalid color.
bounds_rect = self.draw_ellipse(surface, "blue", rect, 0)
with self.assertRaises(TypeError):
# Invalid surface.
bounds_rect = self.draw_ellipse(rect, color, rect, 2)
def test_ellipse__kwarg_invalid_types(self):
"""Ensures draw ellipse detects invalid kwarg types."""
surface = pygame.Surface((3, 3))
color = pygame.Color("green")
rect = pygame.Rect((0, 1), (1, 1))
kwargs_list = [
{
"surface": pygame.Surface, # Invalid surface.
"color": color,
"rect": rect,
"width": 1,
},
{
"surface": surface,
"color": "green", # Invalid color.
"rect": rect,
"width": 1,
},
{
"surface": surface,
"color": color,
"rect": (0, 0, 0), # Invalid rect.
"width": 1,
},
{"surface": surface, "color": color, "rect": rect, "width": 1.1},
] # Invalid width.
for kwargs in kwargs_list:
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse(**kwargs)
def test_ellipse__kwarg_invalid_name(self):
"""Ensures draw ellipse detects invalid kwarg names."""
surface = pygame.Surface((2, 3))
color = pygame.Color("cyan")
rect = pygame.Rect((0, 1), (2, 2))
kwargs_list = [
{
"surface": surface,
"color": color,
"rect": rect,
"width": 1,
"invalid": 1,
},
{"surface": surface, "color": color, "rect": rect, "invalid": 1},
]
for kwargs in kwargs_list:
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse(**kwargs)
def test_ellipse__args_and_kwargs(self):
"""Ensures draw ellipse accepts a combination of args/kwargs"""
surface = pygame.Surface((3, 1))
color = (255, 255, 0, 0)
rect = pygame.Rect((1, 0), (2, 1))
width = 0
kwargs = {"surface": surface, "color": color, "rect": rect, "width": width}
for name in ("surface", "color", "rect", "width"):
kwargs.pop(name)
if "surface" == name:
bounds_rect = self.draw_ellipse(surface, **kwargs)
elif "color" == name:
bounds_rect = self.draw_ellipse(surface, color, **kwargs)
elif "rect" == name:
bounds_rect = self.draw_ellipse(surface, color, rect, **kwargs)
else:
bounds_rect = self.draw_ellipse(surface, color, rect, width, **kwargs)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__valid_width_values(self):
"""Ensures draw ellipse accepts different width values."""
pos = (1, 1)
surface_color = pygame.Color("white")
surface = pygame.Surface((3, 4))
color = (10, 20, 30, 255)
kwargs = {
"surface": surface,
"color": color,
"rect": pygame.Rect(pos, (3, 2)),
"width": None,
}
for width in (-1000, -10, -1, 0, 1, 10, 1000):
surface.fill(surface_color) # Clear for each test.
kwargs["width"] = width
expected_color = color if width >= 0 else surface_color
bounds_rect = self.draw_ellipse(**kwargs)
self.assertEqual(surface.get_at(pos), expected_color)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__valid_rect_formats(self):
"""Ensures draw ellipse accepts different rect formats."""
pos = (1, 1)
expected_color = pygame.Color("red")
surface_color = pygame.Color("black")
surface = pygame.Surface((4, 4))
kwargs = {"surface": surface, "color": expected_color, "rect": None, "width": 0}
rects = (pygame.Rect(pos, (1, 3)), (pos, (2, 1)), (pos[0], pos[1], 1, 1))
for rect in rects:
surface.fill(surface_color) # Clear for each test.
kwargs["rect"] = rect
bounds_rect = self.draw_ellipse(**kwargs)
self.assertEqual(surface.get_at(pos), expected_color)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__valid_color_formats(self):
"""Ensures draw ellipse accepts different color formats."""
pos = (1, 1)
green_color = pygame.Color("green")
surface_color = pygame.Color("black")
surface = pygame.Surface((3, 4))
kwargs = {
"surface": surface,
"color": None,
"rect": pygame.Rect(pos, (1, 2)),
"width": 0,
}
reds = (
(0, 255, 0),
(0, 255, 0, 255),
surface.map_rgb(green_color),
green_color,
)
for color in reds:
surface.fill(surface_color) # Clear for each test.
kwargs["color"] = color
if isinstance(color, int):
expected_color = surface.unmap_rgb(color)
else:
expected_color = green_color
bounds_rect = self.draw_ellipse(**kwargs)
self.assertEqual(surface.get_at(pos), expected_color)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_ellipse__invalid_color_formats(self):
"""Ensures draw ellipse handles invalid color formats correctly."""
pos = (1, 1)
surface_color = pygame.Color("black")
surface = pygame.Surface((4, 3))
kwargs = {
"surface": surface,
"color": None,
"rect": pygame.Rect(pos, (2, 2)),
"width": 1,
}
# These color formats are currently not supported (it would be
# nice to eventually support them).
for expected_color in ("green", "#00FF00FF", "0x00FF00FF"):
kwargs["color"] = expected_color
with self.assertRaises(TypeError):
bounds_rect = self.draw_ellipse(**kwargs)
def test_ellipse(self):
"""Tests ellipses of differing sizes on surfaces of differing sizes.
Checks if the number of sides touching the border of the surface is
correct.
"""
left_top = [(0, 0), (1, 0), (0, 1), (1, 1)]
sizes = [(4, 4), (5, 4), (4, 5), (5, 5)]
color = (1, 13, 24, 255)
def same_size(width, height, border_width):
"""Test for ellipses with the same size as the surface."""
surface = pygame.Surface((width, height))
self.draw_ellipse(surface, color, (0, 0, width, height), border_width)
# For each of the four borders check if it contains the color
borders = get_border_values(surface, width, height)
for border in borders:
self.assertTrue(color in border)
def not_same_size(width, height, border_width, left, top):
"""Test for ellipses that aren't the same size as the surface."""
surface = pygame.Surface((width, height))
self.draw_ellipse(
surface, color, (left, top, width - 1, height - 1), border_width
)
borders = get_border_values(surface, width, height)
# Check if two sides of the ellipse are touching the border
sides_touching = [color in border for border in borders].count(True)
self.assertEqual(sides_touching, 2)
for width, height in sizes:
for border_width in (0, 1):
same_size(width, height, border_width)
for left, top in left_top:
not_same_size(width, height, border_width, left, top)
def _check_1_pixel_sized_ellipse(
self, surface, collide_rect, surface_color, ellipse_color
):
# Helper method to check the surface for 1 pixel wide and/or high
# ellipses.
surf_w, surf_h = surface.get_size()
surface.lock() # For possible speed up.
for pos in ((x, y) for y in range(surf_h) for x in range(surf_w)):
# Since the ellipse is just a line we can use a rect to help find
# where it is expected to be drawn.
if collide_rect.collidepoint(pos):
expected_color = ellipse_color
else:
expected_color = surface_color
self.assertEqual(
surface.get_at(pos),
expected_color,
"collide_rect={}, pos={}".format(collide_rect, pos),
)
surface.unlock()
def test_ellipse__1_pixel_width(self):
"""Ensures an ellipse with a width of 1 is drawn correctly.
An ellipse with a width of 1 pixel is a vertical line.
"""
ellipse_color = pygame.Color("red")
surface_color = pygame.Color("black")
surf_w, surf_h = 10, 20
surface = pygame.Surface((surf_w, surf_h))
rect = pygame.Rect((0, 0), (1, 0))
collide_rect = rect.copy()
# Calculate some positions.
off_left = -1
off_right = surf_w
off_bottom = surf_h
center_x = surf_w // 2
center_y = surf_h // 2
# Test some even and odd heights.
for ellipse_h in range(6, 10):
# The ellipse is drawn on the edge of the rect so collide_rect
# needs +1 height to track where it's drawn.
collide_rect.h = ellipse_h + 1
rect.h = ellipse_h
# Calculate some variable positions.
off_top = -(ellipse_h + 1)
half_off_top = -(ellipse_h // 2)
half_off_bottom = surf_h - (ellipse_h // 2)
# Draw the ellipse in different positions: fully on-surface,
# partially off-surface, and fully off-surface.
positions = (
(off_left, off_top),
(off_left, half_off_top),
(off_left, center_y),
(off_left, half_off_bottom),
(off_left, off_bottom),
(center_x, off_top),
(center_x, half_off_top),
(center_x, center_y),
(center_x, half_off_bottom),
(center_x, off_bottom),
(off_right, off_top),
(off_right, half_off_top),
(off_right, center_y),
(off_right, half_off_bottom),
(off_right, off_bottom),
)
for rect_pos in positions:
surface.fill(surface_color) # Clear before each draw.
rect.topleft = rect_pos
collide_rect.topleft = rect_pos
self.draw_ellipse(surface, ellipse_color, rect)
self._check_1_pixel_sized_ellipse(
surface, collide_rect, surface_color, ellipse_color
)
def test_ellipse__1_pixel_width_spanning_surface(self):
"""Ensures an ellipse with a width of 1 is drawn correctly
when spanning the height of the surface.
An ellipse with a width of 1 pixel is a vertical line.
"""
ellipse_color = pygame.Color("red")
surface_color = pygame.Color("black")
surf_w, surf_h = 10, 20
surface = pygame.Surface((surf_w, surf_h))
rect = pygame.Rect((0, 0), (1, surf_h + 2)) # Longer than the surface.
# Draw the ellipse in different positions: on-surface and off-surface.
positions = (
(-1, -1), # (off_left, off_top)
(0, -1), # (left_edge, off_top)
(surf_w // 2, -1), # (center_x, off_top)
(surf_w - 1, -1), # (right_edge, off_top)
(surf_w, -1),
) # (off_right, off_top)
for rect_pos in positions:
surface.fill(surface_color) # Clear before each draw.
rect.topleft = rect_pos
self.draw_ellipse(surface, ellipse_color, rect)
self._check_1_pixel_sized_ellipse(
surface, rect, surface_color, ellipse_color
)
def test_ellipse__1_pixel_height(self):
"""Ensures an ellipse with a height of 1 is drawn correctly.
An ellipse with a height of 1 pixel is a horizontal line.
"""
ellipse_color = pygame.Color("red")
surface_color = pygame.Color("black")
surf_w, surf_h = 20, 10
surface = pygame.Surface((surf_w, surf_h))
rect = pygame.Rect((0, 0), (0, 1))
collide_rect = rect.copy()
# Calculate some positions.
off_right = surf_w
off_top = -1
off_bottom = surf_h
center_x = surf_w // 2
center_y = surf_h // 2
# Test some even and odd widths.
for ellipse_w in range(6, 10):
# The ellipse is drawn on the edge of the rect so collide_rect
# needs +1 width to track where it's drawn.
collide_rect.w = ellipse_w + 1
rect.w = ellipse_w
# Calculate some variable positions.
off_left = -(ellipse_w + 1)
half_off_left = -(ellipse_w // 2)
half_off_right = surf_w - (ellipse_w // 2)
# Draw the ellipse in different positions: fully on-surface,
# partially off-surface, and fully off-surface.
positions = (
(off_left, off_top),
(half_off_left, off_top),
(center_x, off_top),
(half_off_right, off_top),
(off_right, off_top),
(off_left, center_y),
(half_off_left, center_y),
(center_x, center_y),
(half_off_right, center_y),
(off_right, center_y),
(off_left, off_bottom),
(half_off_left, off_bottom),
(center_x, off_bottom),
(half_off_right, off_bottom),
(off_right, off_bottom),
)
for rect_pos in positions:
surface.fill(surface_color) # Clear before each draw.
rect.topleft = rect_pos
collide_rect.topleft = rect_pos
self.draw_ellipse(surface, ellipse_color, rect)
self._check_1_pixel_sized_ellipse(
surface, collide_rect, surface_color, ellipse_color
)
def test_ellipse__1_pixel_height_spanning_surface(self):
"""Ensures an ellipse with a height of 1 is drawn correctly
when spanning the width of the surface.
An ellipse with a height of 1 pixel is a horizontal line.
"""
ellipse_color = pygame.Color("red")
surface_color = pygame.Color("black")
surf_w, surf_h = 20, 10
surface = pygame.Surface((surf_w, surf_h))
rect = pygame.Rect((0, 0), (surf_w + 2, 1)) # Wider than the surface.
# Draw the ellipse in different positions: on-surface and off-surface.
positions = (
(-1, -1), # (off_left, off_top)
(-1, 0), # (off_left, top_edge)
(-1, surf_h // 2), # (off_left, center_y)
(-1, surf_h - 1), # (off_left, bottom_edge)
(-1, surf_h),
) # (off_left, off_bottom)
for rect_pos in positions:
surface.fill(surface_color) # Clear before each draw.
rect.topleft = rect_pos
self.draw_ellipse(surface, ellipse_color, rect)
self._check_1_pixel_sized_ellipse(
surface, rect, surface_color, ellipse_color
)
def test_ellipse__1_pixel_width_and_height(self):
"""Ensures an ellipse with a width and height of 1 is drawn correctly.
An ellipse with a width and height of 1 pixel is a single pixel.
"""
ellipse_color = pygame.Color("red")
surface_color = pygame.Color("black")
surf_w, surf_h = 10, 10
surface = pygame.Surface((surf_w, surf_h))
rect = pygame.Rect((0, 0), (1, 1))
# Calculate some positions.
off_left = -1
off_right = surf_w
off_top = -1
off_bottom = surf_h
left_edge = 0
right_edge = surf_w - 1
top_edge = 0
bottom_edge = surf_h - 1
center_x = surf_w // 2
center_y = surf_h // 2
# Draw the ellipse in different positions: center surface,
# top/bottom/left/right edges, and off-surface.
positions = (
(off_left, off_top),
(off_left, top_edge),
(off_left, center_y),
(off_left, bottom_edge),
(off_left, off_bottom),
(left_edge, off_top),
(left_edge, top_edge),
(left_edge, center_y),
(left_edge, bottom_edge),
(left_edge, off_bottom),
(center_x, off_top),
(center_x, top_edge),
(center_x, center_y),
(center_x, bottom_edge),
(center_x, off_bottom),
(right_edge, off_top),
(right_edge, top_edge),
(right_edge, center_y),
(right_edge, bottom_edge),
(right_edge, off_bottom),
(off_right, off_top),
(off_right, top_edge),
(off_right, center_y),
(off_right, bottom_edge),
(off_right, off_bottom),
)
for rect_pos in positions:
surface.fill(surface_color) # Clear before each draw.
rect.topleft = rect_pos
self.draw_ellipse(surface, ellipse_color, rect)
self._check_1_pixel_sized_ellipse(
surface, rect, surface_color, ellipse_color
)
def test_ellipse__bounding_rect(self):
"""Ensures draw ellipse returns the correct bounding rect.
Tests ellipses on and off the surface and a range of width/thickness
values.
"""
ellipse_color = pygame.Color("red")
surf_color = pygame.Color("black")
min_width = min_height = 5
max_width = max_height = 7
surface = pygame.Surface((30, 30), 0, 32)
surf_rect = surface.get_rect()
# Make a rect that is bigger than the surface to help test drawing
# ellipses off and partially off the surface.
big_rect = surf_rect.inflate(min_width * 2 + 1, min_height * 2 + 1)
for pos in rect_corners_mids_and_center(
surf_rect
) + rect_corners_mids_and_center(big_rect):
# Each of the ellipse's rect position attributes will be set to
# the pos value.
for attr in RECT_POSITION_ATTRIBUTES:
# Test using different rect sizes and thickness values.
for width in range(min_width, max_width + 1):
for height in range(min_height, max_height + 1):
ellipse_rect = pygame.Rect((0, 0), (width, height))
setattr(ellipse_rect, attr, pos)
for thickness in range(min(width, height) + 1):
surface.fill(surf_color) # Clear for each test.
bounding_rect = self.draw_ellipse(
surface, ellipse_color, ellipse_rect, thickness
)
# Calculating the expected_rect after the ellipse
# is drawn (it uses what is actually drawn).
expected_rect = create_bounding_rect(
surface, surf_color, ellipse_rect.topleft
)
self.assertEqual(bounding_rect, expected_rect)
def test_ellipse__surface_clip(self):
"""Ensures draw ellipse respects a surface's clip area.
Tests drawing the ellipse filled and unfilled.
"""
surfw = surfh = 30
ellipse_color = pygame.Color("red")
surface_color = pygame.Color("green")
surface = pygame.Surface((surfw, surfh))
surface.fill(surface_color)
clip_rect = pygame.Rect((0, 0), (11, 11))
clip_rect.center = surface.get_rect().center
pos_rect = clip_rect.copy() # Manages the ellipse's pos.
for width in (0, 1): # Filled and unfilled.
# Test centering the ellipse along the clip rect's edge.
for center in rect_corners_mids_and_center(clip_rect):
# Get the expected points by drawing the ellipse without the
# clip area set.
pos_rect.center = center
surface.set_clip(None)
surface.fill(surface_color)
self.draw_ellipse(surface, ellipse_color, pos_rect, width)
expected_pts = get_color_points(surface, ellipse_color, clip_rect)
# Clear the surface and set the clip area. Redraw the ellipse
# and check that only the clip area is modified.
surface.fill(surface_color)
surface.set_clip(clip_rect)
self.draw_ellipse(surface, ellipse_color, pos_rect, width)
surface.lock() # For possible speed up.
# Check all the surface points to ensure only the expected_pts
# are the ellipse_color.
for pt in ((x, y) for x in range(surfw) for y in range(surfh)):
if pt in expected_pts:
expected_color = ellipse_color
else:
expected_color = surface_color
self.assertEqual(surface.get_at(pt), expected_color, pt)
surface.unlock()
class DrawEllipseTest(DrawEllipseMixin, DrawTestCase):
"""Test draw module function ellipse.
This class inherits the general tests from DrawEllipseMixin. It is also
the class to add any draw.ellipse specific tests to.
"""
# Commented out to avoid cluttering the test output. Add back in if draw_py
# ever properly supports drawing ellipses.
# @unittest.skip('draw_py.draw_ellipse not supported yet')
# class PythonDrawEllipseTest(DrawEllipseMixin, PythonDrawTestCase):
# """Test draw_py module function draw_ellipse.
#
# This class inherits the general tests from DrawEllipseMixin. It is also
# the class to add any draw_py.draw_ellipse specific tests to.
# """
### Line/Lines/AALine/AALines Testing #########################################
class BaseLineMixin(object):
"""Mixin base for drawing various lines.
This class contains general helper methods and setup for testing the
different types of lines.
"""
COLORS = (
(0, 0, 0),
(255, 0, 0),
(0, 255, 0),
(0, 0, 255),
(255, 255, 0),
(255, 0, 255),
(0, 255, 255),
(255, 255, 255),
)
@staticmethod
def _create_surfaces():
# Create some surfaces with different sizes, depths, and flags.
surfaces = []
for size in ((49, 49), (50, 50)):
for depth in (8, 16, 24, 32):
for flags in (0, SRCALPHA):
surface = pygame.display.set_mode(size, flags, depth)
surfaces.append(surface)
surfaces.append(surface.convert_alpha())
return surfaces
@staticmethod
def _rect_lines(rect):
# Yields pairs of end points and their reverse (to test symmetry).
# Uses a rect with the points radiating from its midleft.
for pt in rect_corners_mids_and_center(rect):
if pt == rect.midleft or pt == rect.center:
# Don't bother with these points.
continue
yield (rect.midleft, pt)
yield (pt, rect.midleft)
### Line Testing ##############################################################
class LineMixin(BaseLineMixin):
"""Mixin test for drawing a single line.
This class contains all the general single line drawing tests.
"""
def test_line__args(self):
"""Ensures draw line accepts the correct args."""
bounds_rect = self.draw_line(
pygame.Surface((3, 3)), (0, 10, 0, 50), (0, 0), (1, 1), 1
)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_line__args_without_width(self):
"""Ensures draw line accepts the args without a width."""
bounds_rect = self.draw_line(
pygame.Surface((2, 2)), (0, 0, 0, 50), (0, 0), (2, 2)
)
self.assertIsInstance(bounds_rect, pygame.Rect)
def test_line__kwargs(self):
"""Ensures draw line accepts the correct kwargs
with and without a width arg.