forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry_test.py
2220 lines (1702 loc) · 66.5 KB
/
geometry_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
from math import sqrt
from pygame import Vector2, Vector3, Rect, FRect
from pygame.geometry import Circle, Line
def float_range(a, b, step):
result = []
current_value = a
while current_value < b:
result.append(current_value)
current_value += step
return result
class CircleTypeTest(unittest.TestCase):
def testConstruction_invalid_type(self):
"""Checks whether passing wrong types to the constructor
raises the appropriate errors
"""
invalid_types = (None, [], "1", (1,), [1, 2, 3], Vector2(1, 1))
# Test x
for value in invalid_types:
with self.assertRaises(TypeError):
c = Circle(value, 0, 1)
# Test y
for value in invalid_types:
with self.assertRaises(TypeError):
c = Circle(0, value, 1)
# Test r
for value in invalid_types + (-1,):
with self.assertRaises(TypeError):
c = Circle(0, 0, value)
def test2ndConstruction_invalid_type(self):
"""Checks whether passing wrong types to the 2nd constructor
raises the appropriate errors
"""
invalid_types = (None, [], "1", (1,), [1, 2, 3], Vector2(1, 1))
# Test x
for value in invalid_types:
with self.assertRaises(TypeError):
c = Circle((value, 0), 1)
# Test y
for value in invalid_types:
with self.assertRaises(TypeError):
c = Circle((0, value), 1)
# Test r
for value in invalid_types + (-1,):
with self.assertRaises(TypeError):
c = Circle((0, 0), value)
def testConstruction_invalid_arguments_number(self):
"""Checks whether passing the wrong number of arguments to the constructor
raises the appropriate errors
"""
arguments = (
(1,), # one non vec3 non circle arg
(1, 1, 1, 1), # four args
)
for arg_seq in arguments:
with self.assertRaises(TypeError):
c = Circle(*arg_seq)
def testConstructionXYR_float(self):
c = Circle(1.0, 2.0, 3.0)
self.assertEqual(1.0, c.x)
self.assertEqual(2.0, c.y)
self.assertEqual(3.0, c.r)
def testConstructionTUP_XYR_float(self):
c = Circle((1.0, 2.0, 3.0))
self.assertEqual(1.0, c.x)
self.assertEqual(2.0, c.y)
self.assertEqual(3.0, c.r)
def testConstructionXYR_int(self):
c = Circle(1, 2, 3)
self.assertEqual(1.0, c.x)
self.assertEqual(2.0, c.y)
self.assertEqual(3.0, c.r)
def testConstructionTUP_XYR_int(self):
c = Circle((1, 2, 3))
self.assertEqual(1.0, c.x)
self.assertEqual(2.0, c.y)
self.assertEqual(3.0, c.r)
def testConstruction_zero_radius(self):
c = Circle(1, 2, 0)
self.assertEqual(1.0, c.x)
self.assertEqual(2.0, c.y)
self.assertEqual(0, c.r)
def test_x(self):
"""Ensures changing the x attribute moves the circle and does not change
the circle's radius.
"""
expected_x = 10.0
expected_y = 2.0
expected_radius = 5.0
c = Circle(1, expected_y, expected_radius)
c.x = expected_x
self.assertEqual(c.x, expected_x)
self.assertEqual(c.y, expected_y)
self.assertEqual(c.r, expected_radius)
def test_x__invalid_value(self):
"""Ensures the x attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.x = value
def test_x__del(self):
"""Ensures the x attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.x
def test_y(self):
"""Ensures changing the y attribute moves the circle and does not change
the circle's radius.
"""
expected_x = 10.0
expected_y = 2.0
expected_radius = 5.0
c = Circle(expected_x, 1, expected_radius)
c.y = expected_y
self.assertEqual(c.x, expected_x)
self.assertEqual(c.y, expected_y)
self.assertEqual(c.r, expected_radius)
def test_y__invalid_value(self):
"""Ensures the y attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.y = value
def test_y__del(self):
"""Ensures the y attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.y
def test_r(self):
"""Ensures changing the r attribute changes the radius without moving the circle."""
expected_x = 10.0
expected_y = 2.0
expected_radius = 5.0
c = Circle(expected_x, expected_y, 1.0)
c.r = expected_radius
self.assertEqual(c.x, expected_x)
self.assertEqual(c.y, expected_y)
self.assertEqual(c.r, expected_radius)
c.radius = expected_radius
self.assertEqual(c.x, expected_x)
self.assertEqual(c.y, expected_y)
self.assertEqual(c.radius, expected_radius)
def test_r__invalid_value(self):
"""Ensures the r attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.r = value
with self.assertRaises(TypeError):
c.radius = value
for value in (-10.3234, -1):
with self.assertRaises(ValueError):
c.r = value
with self.assertRaises(ValueError):
c.radius = value
def test_r__del(self):
"""Ensures the r attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.r
with self.assertRaises(AttributeError):
del c.radius
def test_r_sqr(self):
"""Ensures setting the r_sqr attribute matches the r_sqr passed"""
expected_r_sqr = 10.0
c = Circle(1.0, 1.0, 1.0)
c.r_sqr = expected_r_sqr
self.assertAlmostEqual(c.r_sqr, expected_r_sqr, places=14)
def test_r_to_r_sqr(self):
"""Ensures changing the r attribute correctly changes the r_sqr attribute."""
expected_r_sqr = 1.0
expected_r_sqr2 = 100.0
c = Circle(0, 0, 23)
c2 = Circle(0, 0, 4)
c.r = 1
self.assertEqual(c.r_sqr, expected_r_sqr)
c2.r = 10
self.assertEqual(c2.r_sqr, expected_r_sqr2)
def test_r_sqr_to_r(self):
"""Ensures changing the r_sqr attribute correctly changes the r attribute."""
expected_r = 2.0
c = Circle(0, 0, 23)
c.r_sqr = 4.0
self.assertEqual(c.r, expected_r)
self.assertEqual(c.radius, expected_r)
c.r_sqr = 13.33421
self.assertEqual(c.r, sqrt(13.33421))
self.assertEqual(c.radius, sqrt(13.33421))
def test_r_sqr__invalid_set(self):
"""Ensures the r_sqr attribute can't be set"""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.r_sqr = value
def test_r_sqr__del(self):
"""Ensures the r attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.r
def test_center(self):
"""Ensures changing the center moves the circle and does not change
the circle's radius.
"""
expected_x = 10.3
expected_y = 2.12
expected_radius = 5.0
c = Circle(1, 1, expected_radius)
c.center = (expected_x, expected_y)
self.assertEqual(c.x, expected_x)
self.assertEqual(c.y, expected_y)
self.assertEqual(c.r, expected_radius)
def test_center_update(self):
"""Ensures changing the x or y value of the circle correctly updates the center."""
expected_x = 10.3
expected_y = 2.12
expected_radius = 5.0
c = Circle(1, 1, expected_radius)
c.x = expected_x
self.assertEqual(c.center, (expected_x, c.y))
c.y = expected_y
self.assertEqual(c.center, (c.x, expected_y))
def test_center_invalid_value(self):
"""Ensures the center attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.center = value
def test_center_del(self):
"""Ensures the center attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.center
def test_top(self):
"""Ensures changing the top attribute moves the circle and does not change the circle's radius."""
expected_radius = 5.0
for pos in [
(1, 0),
(0, 0),
(-1, 0),
(0, -1),
(1, 1),
(-1, -1),
(-1, 1),
(1, -1),
]:
c = Circle((0, 0), expected_radius)
c.top = pos
self.assertEqual(pos[0], c.x)
self.assertEqual(pos[1], c.y - expected_radius)
self.assertEqual(expected_radius, c.r)
def test_top_update(self):
"""Ensures changing the x or y value of the circle correctly updates the top."""
expected_x = 10.3
expected_y = 2.12
expected_radius = 5.0
c = Circle(1, 1, expected_radius)
c.x = expected_x
self.assertEqual(c.top, (expected_x, c.y - expected_radius))
c.y = expected_y
self.assertEqual(c.top, (c.x, expected_y - expected_radius))
def test_top_invalid_value(self):
"""Ensures the top attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3], True, False):
with self.assertRaises(TypeError):
c.top = value
def test_top_del(self):
"""Ensures the top attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.top
def test_left(self):
"""Ensures changing the left attribute moves the circle and does not change the circle's radius."""
expected_radius = 5.0
for pos in [
(1, 0),
(0, 0),
(-1, 0),
(0, -1),
(1, 1),
(-1, -1),
(-1, 1),
(1, -1),
]:
c = Circle((0, 0), expected_radius)
c.left = pos
self.assertEqual(pos[0], c.x - expected_radius)
self.assertEqual(pos[1], c.y)
self.assertEqual(expected_radius, c.r)
def test_left_update(self):
"""Ensures changing the x or y value of the circle correctly updates the left."""
expected_x = 10.3
expected_y = 2.12
expected_radius = 5.0
c = Circle(1, 1, expected_radius)
c.x = expected_x
self.assertEqual(c.left, (expected_x - expected_radius, c.y))
c.y = expected_y
self.assertEqual(c.left, (c.x - expected_radius, expected_y))
def test_left_invalid_value(self):
"""Ensures the left attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3], True, False):
with self.assertRaises(TypeError):
c.left = value
def test_left_del(self):
"""Ensures the left attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.left
def test_right(self):
"""Ensures changing the right attribute moves the circle and does not change the circle's radius."""
expected_radius = 5.0
for pos in [
(1, 0),
(0, 0),
(-1, 0),
(0, -1),
(1, 1),
(-1, -1),
(-1, 1),
(1, -1),
]:
c = Circle((0, 0), expected_radius)
c.right = pos
self.assertEqual(pos[0], c.x + expected_radius)
self.assertEqual(pos[1], c.y)
self.assertEqual(expected_radius, c.r)
def test_right_update(self):
"""Ensures changing the x or y value of the circle correctly updates the right."""
expected_x = 10.3
expected_y = 2.12
expected_radius = 5.0
c = Circle(1, 1, expected_radius)
c.x = expected_x
self.assertEqual(c.right, (expected_x + expected_radius, c.y))
c.y = expected_y
self.assertEqual(c.right, (c.x + expected_radius, expected_y))
def test_right_invalid_value(self):
"""Ensures the right attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3], True, False):
with self.assertRaises(TypeError):
c.right = value
def test_right_del(self):
"""Ensures the right attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.right
def test_bottom(self):
"""Ensures changing the bottom attribute moves the circle and does not change the circle's radius."""
expected_radius = 5.0
for pos in [
(1, 0),
(0, 0),
(-1, 0),
(0, -1),
(1, 1),
(-1, -1),
(-1, 1),
(1, -1),
]:
c = Circle((0, 0), expected_radius)
c.bottom = pos
self.assertEqual(pos[0], c.x)
self.assertEqual(pos[1], c.y + expected_radius)
self.assertEqual(expected_radius, c.r)
def test_bottom_update(self):
"""Ensures changing the x or y value of the circle correctly updates the bottom."""
expected_x = 10.3
expected_y = 2.12
expected_radius = 5.0
c = Circle(1, 1, expected_radius)
c.x = expected_x
self.assertEqual(c.bottom, (expected_x, c.y + expected_radius))
c.y = expected_y
self.assertEqual(c.bottom, (c.x, expected_y + expected_radius))
def test_bottom_invalid_value(self):
"""Ensures the bottom attribute handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3], True, False):
with self.assertRaises(TypeError):
c.bottom = value
def test_bottom_del(self):
"""Ensures the bottom attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.bottom
def test_area(self):
"""Ensures the area is calculated correctly."""
c = Circle(0, 0, 1)
self.assertEqual(c.area, math.pi)
def test_area_update(self):
"""Ensures the area is updated correctly."""
c = Circle(0, 0, 1)
c.r = 2
self.assertEqual(c.area, math.pi * 4)
c.r_sqr = 100
self.assertEqual(c.area, math.pi * (10**2))
def test_area_invalid_value(self):
"""Ensures the area handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.area = value
for value in (-10.3234, -1):
with self.assertRaises(ValueError):
c.area = value
def test_area_del(self):
"""Ensures the area attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.area
def test_circumference(self):
"""Ensures the circumference is calculated correctly."""
c = Circle(0, 0, 1)
self.assertEqual(c.circumference, math.tau)
def test_circumference_update(self):
"""Ensures the circumference is updated correctly."""
c = Circle(0, 0, 1)
c.r = 2
self.assertEqual(c.circumference, math.tau * 2)
c.r_sqr = 100
self.assertEqual(c.circumference, math.tau * 10)
def test_circumference_invalid_value(self):
"""Ensures the circumference handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.circumference = value
for value in (-10.3234, -1):
with self.assertRaises(ValueError):
c.circumference = value
def test_circumference_del(self):
"""Ensures the circumference attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.circumference
def test_diameter(self):
"""Ensures the diameter is calculated correctly."""
c = Circle(0, 0, 1)
self.assertEqual(c.diameter, 2.0)
self.assertEqual(c.d, 2.0)
def test_diameter_update(self):
"""Ensures the diameter is updated correctly."""
c = Circle(0, 0, 1)
c.r = 2
self.assertEqual(c.diameter, 4.0)
self.assertEqual(c.d, 4.0)
c.r_sqr = 100
self.assertEqual(c.diameter, 20.0)
self.assertEqual(c.d, 20.0)
def test_diameter_invalid_value(self):
"""Ensures the diameter handles invalid values correctly."""
c = Circle(0, 0, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
c.diameter = value
for value in (-10.3234, -1):
with self.assertRaises(ValueError):
c.diameter = value
with self.assertRaises(ValueError):
c.d = value
def test_diameter_del(self):
"""Ensures the diameter attribute can't be deleted."""
c = Circle(0, 0, 1)
with self.assertRaises(AttributeError):
del c.diameter
with self.assertRaises(AttributeError):
del c.d
def test__str__(self):
"""Checks whether the __str__ method works correctly."""
c_str = "<Circle((10.3, 3.2), 4.3)>"
circle = Circle((10.3, 3.2), 4.3)
self.assertEqual(str(circle), c_str)
self.assertEqual(circle.__str__(), c_str)
def test__repr__(self):
"""Checks whether the __repr__ method works correctly."""
c_repr = "<Circle((10.3, 3.2), 4.3)>"
circle = Circle((10.3, 3.2), 4.3)
self.assertEqual(repr(circle), c_repr)
self.assertEqual(circle.__repr__(), c_repr)
def test_copy(self):
c = Circle(10, 10, 4)
# check 1 arg passed
with self.assertRaises(TypeError):
c.copy(10)
# check copied circle has the same attribute values
c_2 = c.copy()
self.assertEqual(c.x, c_2.x)
self.assertEqual(c.y, c_2.y)
self.assertEqual(c.r, c_2.r)
# check c2 is not c
self.assertIsNot(c_2, c)
def test_collidepoint_argtype(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, [], "1", (1,), Vector3(1, 1, 1), 1)
c = Circle(10, 10, 4)
for value in invalid_types:
with self.assertRaises(TypeError):
c.collidepoint(value)
def test_collidepoint_argnum(self):
c = Circle(10, 10, 4)
args = [tuple(range(x)) for x in range(3, 13)]
# no params
with self.assertRaises(TypeError):
c.collidepoint()
# too many params
for arg in args:
with self.assertRaises(TypeError):
c.collidepoint(*arg)
def test_collidepoint(self):
c = Circle(0, 0, 5)
p1 = (3, 3)
p2 = (10, 10)
p3 = Vector2(3, 3)
p4 = Vector2(10, 10)
# colliding single
self.assertTrue(c.collidepoint(p1), "Expected True, point should collide here")
self.assertTrue(c.collidepoint(p3), "Expected True, point should collide here")
# not colliding single
self.assertFalse(
c.collidepoint(p2), "Expected False, point should not collide here"
)
self.assertFalse(
c.collidepoint(p4), "Expected False, point should not collide here"
)
# colliding 2 args
self.assertTrue(
c.collidepoint(3, 3), "Expected True, point should collide here"
)
# not colliding 2 args
self.assertFalse(
c.collidepoint(10, 10), "Expected False, point should not collide here"
)
def test_collidecircle_argtype(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, [], "1", (1,), Vector2(1, 1), 1)
c = Circle(10, 10, 4)
for value in invalid_types:
with self.assertRaises(TypeError):
c.collidecircle(value)
def test_collidecircle_argnum(self):
c = Circle(10, 10, 4)
# no params
with self.assertRaises(TypeError):
c.collidecircle()
with self.assertRaises(TypeError):
c.collidecircle(Circle(10, 10, 4), Circle(10, 10, 4))
def test_collidecircle(self):
c = Circle(0, 0, 5)
c_same = c.copy()
c2 = Circle(10, 0, 5)
c3 = Circle(100, 100, 5)
c4 = Circle(10, 0, 4.999999999999)
c5 = Circle(0, 0, 2)
c6 = Circle(10, 0, 7)
# touching
self.assertTrue(
c.collidecircle(c2), "Expected True, circles should collide here"
)
# partly colliding
self.assertTrue(
c.collidecircle(c6), "Expected True, circles should collide here"
)
# self colliding
self.assertTrue(
c.collidecircle(c), "Expected True, circles should collide with self"
)
# completely colliding
self.assertTrue(
c.collidecircle(c_same), "Expected True, circles should collide with self"
)
# not touching
self.assertFalse(
c.collidecircle(c3), "Expected False, circles should not collide here"
)
# barely not touching
self.assertFalse(
c.collidecircle(c4), "Expected False, circles should not collide here"
)
# small circle inside big circle
self.assertTrue(
c.collidecircle(c5), "Expected True, circles should collide here"
)
# big circle outside small circle
self.assertTrue(
c5.collidecircle(c), "Expected False, circles should collide here"
)
def test_colliderect_argtype(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, [], "1", (1,), Vector3(1, 1, 1), 1, True, False)
c = Circle(10, 10, 4)
for value in invalid_types:
with self.assertRaises(TypeError):
c.colliderect(value)
def test_colliderect_argnum(self):
"""tests if the function correctly handles incorrect number of parameters"""
c = Circle(10, 10, 4)
args = [(1), (1, 1), (1, 1, 1), (1, 1, 1, 1, 1)]
# no params
with self.assertRaises(TypeError):
c.colliderect()
# invalid num
for arg in args:
with self.assertRaises(TypeError):
c.colliderect(*arg)
def test_colliderect(self):
"""ensures the function correctly detects collisions with rects"""
msgt = "Expected True, rect should collide here"
msgf = "Expected False, rect should not collide here"
# ====================================================
c = Circle(0, 0, 5)
r1, r2, r3 = Rect(2, 2, 4, 4), Rect(10, 15, 43, 24), Rect(0, 5, 4, 4)
fr1, fr2, fr3 = FRect(r1), FRect(r2), FRect(r3)
# colliding single
for r in (r1, fr1):
self.assertTrue(c.colliderect(r), msgt)
# not colliding single
for r in (r2, fr2):
self.assertFalse(c.colliderect(r), msgf)
# barely colliding single
for r in (r3, fr3):
self.assertTrue(c.colliderect(r), msgt)
# colliding 4 args
self.assertTrue(c.colliderect(2, 2, 4, 4), msgt)
# not colliding 4 args
self.assertFalse(c.colliderect(10, 15, 43, 24), msgf)
# barely colliding single
self.assertTrue(c.colliderect(0, 4.9999999999999, 4, 4), msgt)
# ensure FRects aren't truncated
c2 = Circle(0, 0, 0.35)
c3 = Circle(2, 0, 0.65)
fr9 = FRect(0.4, 0.0, 1, 1)
self.assertFalse(c2.colliderect(fr9), msgf)
self.assertFalse(c2.colliderect(0.4, 0.0, 1, 1), msgf)
self.assertFalse(c2.colliderect((0.4, 0.0), (1, 1)), msgf)
self.assertTrue(c3.colliderect(fr9), msgt)
self.assertTrue(c3.colliderect(0.4, 0.0, 1, 1), msgt)
self.assertTrue(c3.colliderect((0.4, 0.0), (1, 1)), msgt)
def test_collideswith_argtype(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, [], "1", (1,), Vector3(1, 1, 1), 1)
c = Circle(10, 10, 4)
for value in invalid_types:
with self.assertRaises(TypeError):
c.collideswith(value)
def test_collideswith_argnum(self):
c = Circle(10, 10, 4)
args = [tuple(range(x)) for x in range(2, 4)]
# no params
with self.assertRaises(TypeError):
c.collideswith()
# too many params
for arg in args:
with self.assertRaises(TypeError):
c.collideswith(*arg)
def test_collideswith(self):
"""Ensures the collideswith function correctly registers collisions with circles, lines, rects and points"""
c = Circle(0, 0, 5)
# circle
c2 = Circle(0, 10, 15)
c3 = Circle(100, 100, 1)
self.assertTrue(c.collideswith(c2))
self.assertFalse(c.collideswith(c3))
# rect
r = Rect(0, 0, 10, 10)
r2 = Rect(50, 0, 10, 10)
self.assertTrue(c.collideswith(r))
self.assertFalse(c.collideswith(r2))
# rect
r = FRect(0, 0, 10, 10)
r2 = FRect(50, 0, 10, 10)
self.assertTrue(c.collideswith(r))
self.assertFalse(c.collideswith(r2))
# point
p = (0, 0)
p2 = (50, 0)
self.assertTrue(c.collideswith(p))
self.assertFalse(c.collideswith(p2))
def test_collidelist_argtype(self):
"""Tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, "1", (1,), 1, (1, 2, 3), True, False)
c = Circle(10, 10, 4)
for value in invalid_types:
with self.assertRaises(TypeError):
c.collidelist(value)
def test_collidelist_argnum(self):
"""Tests if the function correctly handles incorrect number of parameters"""
c = Circle(10, 10, 4)
circles = [(Circle(10, 10, 4), Circle(10, 10, 4))]
with self.assertRaises(TypeError):
c.collidelist()
with self.assertRaises(TypeError):
c.collidelist(circles, 1)
def test_collidelist_return_type(self):
"""Tests if the function returns the correct type"""
c = Circle(10, 10, 4)
objects = [
Circle(10, 10, 4),
Rect(10, 10, 4, 4),
]
for object in objects:
self.assertIsInstance(c.collidelist([object]), int)
def test_collidelist(self):
"""Ensures that the collidelist method works correctly"""
c = Circle(10, 10, 4)
circles = [Circle(1000, 1000, 2), Circle(5, 10, 5), Circle(16, 10, 7)]
rects = [Rect(1000, 1000, 4, 4), Rect(1000, 200, 5, 5), Rect(5, 10, 7, 3)]
points = [(-10, -10), Vector2(1, 1), Vector2(10, -20), (10, 10)]
expected = [1, 2, 3]
for objects, expected in zip([circles, rects, points], expected):
self.assertEqual(c.collidelist(objects), expected)
def test_collidelistall_argtype(self):
"""Tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, "1", (1,), 1, (1, 2, 3), True, False)
c = Circle(10, 10, 4)
for value in invalid_types:
with self.assertRaises(TypeError):
c.collidelistall(value)
def test_collidelistall_argnum(self):
"""Tests if the function correctly handles incorrect number of parameters"""
c = Circle(10, 10, 4)
circles = [(Circle(10, 10, 4), Circle(10, 10, 4))]
with self.assertRaises(TypeError):
c.collidelistall()
with self.assertRaises(TypeError):
c.collidelistall(circles, 1)
def test_collidelistall_return_type(self):
"""Tests if the function returns the correct type"""
c = Circle(10, 10, 4)
objects = [
Circle(10, 10, 4),
Rect(10, 10, 4, 4),
(10, 10),
Vector2(9, 9),
]
for object in objects:
self.assertIsInstance(c.collidelistall([object]), list)
def test_collidelistall(self):
"""Ensures that the collidelistall method works correctly"""
c = Circle(10, 10, 4)
circles = [Circle(1000, 1000, 2), Circle(5, 10, 5), Circle(16, 10, 7)]
rects = [Rect(1000, 1000, 4, 4), Rect(1000, 200, 5, 5), Rect(5, 10, 7, 3)]
points = [Vector2(-10, -10), (8, 8), (10, -20), Vector2(10, 10)]
expected = [[1, 2], [2], [1, 3]]
for objects, expected in zip([circles, rects, points], expected):
self.assertEqual(c.collidelistall(objects), expected)
def test_update(self):
"""Ensures that updating the circle position
and dimension correctly updates position and dimension"""
c = Circle(0, 0, 10)
c.update(5, 5, 3)
self.assertEqual(c.x, 5.0)
self.assertEqual(c.y, 5.0)
self.assertEqual(c.r, 3.0)
self.assertEqual(c.r_sqr, 9.0)
def test_update_argtype(self):
"""tests if the function correctly handles incorrect types as parameters"""
invalid_types = (None, [], "1", (1,), Vector2(1, 1), 1, 0.2324)