forked from pygame-community/pygame-ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect_test.py
3403 lines (2798 loc) · 120 KB
/
rect_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 collections.abc import Collection, Sequence
from pygame import Vector2, FRect, Rect as IRect
from pygame.tests import test_utils
Rect = IRect
class RectTypeTest(unittest.TestCase):
def setUp(self):
global Rect
Rect = IRect
def _assertCountEqual(self, *args, **kwargs):
self.assertCountEqual(*args, **kwargs)
def testConstructionXYWidthHeight(self):
r = Rect(1, 2, 3, 4)
self.assertEqual(1, r.left)
self.assertEqual(2, r.top)
self.assertEqual(3, r.width)
self.assertEqual(4, r.height)
def testConstructionTopLeftSize(self):
r = Rect((1, 2), (3, 4))
self.assertEqual(1, r.left)
self.assertEqual(2, r.top)
self.assertEqual(3, r.width)
self.assertEqual(4, r.height)
def testCalculatedAttributes(self):
r = Rect(1, 2, 3, 4)
self.assertEqual(r.left + r.width, r.right)
self.assertEqual(r.top + r.height, r.bottom)
self.assertEqual((r.width, r.height), r.size)
self.assertEqual((r.left, r.top), r.topleft)
self.assertEqual((r.right, r.top), r.topright)
self.assertEqual((r.left, r.bottom), r.bottomleft)
self.assertEqual((r.right, r.bottom), r.bottomright)
midx = r.left + r.width // 2
midy = r.top + r.height // 2
self.assertEqual(midx, r.centerx)
self.assertEqual(midy, r.centery)
self.assertEqual((r.centerx, r.centery), r.center)
self.assertEqual((r.centerx, r.top), r.midtop)
self.assertEqual((r.centerx, r.bottom), r.midbottom)
self.assertEqual((r.left, r.centery), r.midleft)
self.assertEqual((r.right, r.centery), r.midright)
def testRepr(self):
rect = Rect(12, 34, 56, 78)
self.assertEqual(repr(rect), "Rect(12, 34, 56, 78)")
def test_rect_iter(self):
rect = Rect(50, 100, 150, 200)
# call __iter__ explicitly to test that it is defined
rect_iterator = rect.__iter__()
for i, val in enumerate(rect_iterator):
self.assertEqual(rect[i], val)
def test_normalize(self):
"""Ensures normalize works when width and height are both negative."""
test_rect = Rect((1, 2), (-3, -6))
expected_normalized_rect = (
(test_rect.x + test_rect.w, test_rect.y + test_rect.h),
(-test_rect.w, -test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_normalize__positive_height(self):
"""Ensures normalize works with a negative width and a positive height."""
test_rect = Rect((1, 2), (-3, 6))
expected_normalized_rect = (
(test_rect.x + test_rect.w, test_rect.y),
(-test_rect.w, test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_normalize__positive_width(self):
"""Ensures normalize works with a positive width and a negative height."""
test_rect = Rect((1, 2), (3, -6))
expected_normalized_rect = (
(test_rect.x, test_rect.y + test_rect.h),
(test_rect.w, -test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_normalize__zero_height(self):
"""Ensures normalize works with a negative width and a zero height."""
test_rect = Rect((1, 2), (-3, 0))
expected_normalized_rect = (
(test_rect.x + test_rect.w, test_rect.y),
(-test_rect.w, test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_normalize__zero_width(self):
"""Ensures normalize works with a zero width and a negative height."""
test_rect = Rect((1, 2), (0, -6))
expected_normalized_rect = (
(test_rect.x, test_rect.y + test_rect.h),
(test_rect.w, -test_rect.h),
)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_normalize__non_negative(self):
"""Ensures normalize works when width and height are both non-negative.
Tests combinations of positive and zero values for width and height.
The normalize method has no impact when both width and height are
non-negative.
"""
for size in ((3, 6), (3, 0), (0, 6), (0, 0)):
test_rect = Rect((1, 2), size)
expected_normalized_rect = Rect(test_rect)
test_rect.normalize()
self.assertEqual(test_rect, expected_normalized_rect)
def test_x(self):
"""Ensures changing the x attribute moves the rect and does not change
the rect's size.
"""
expected_x = 10
expected_y = 2
expected_size = (3, 4)
r = Rect((1, expected_y), expected_size)
r.x = expected_x
self.assertEqual(r.x, expected_x)
self.assertEqual(r.x, r.left)
self.assertEqual(r.y, expected_y)
self.assertEqual(r.size, expected_size)
def test_x__invalid_value(self):
"""Ensures the x attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.x = value
def test_x__del(self):
"""Ensures the x attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.x
def test_y(self):
"""Ensures changing the y attribute moves the rect and does not change
the rect's size.
"""
expected_x = 1
expected_y = 20
expected_size = (3, 4)
r = Rect((expected_x, 2), expected_size)
r.y = expected_y
self.assertEqual(r.y, expected_y)
self.assertEqual(r.y, r.top)
self.assertEqual(r.x, expected_x)
self.assertEqual(r.size, expected_size)
def test_y__invalid_value(self):
"""Ensures the y attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.y = value
def test_y__del(self):
"""Ensures the y attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.y
def test_left(self):
"""Changing the left attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_left = 10
r.left = new_left
self.assertEqual(new_left, r.left)
self.assertEqual(Rect(new_left, 2, 3, 4), r)
def test_left__invalid_value(self):
"""Ensures the left attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.left = value
def test_left__del(self):
"""Ensures the left attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.left
def test_right(self):
"""Changing the right attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_right = r.right + 20
expected_left = r.left + 20
old_width = r.width
r.right = new_right
self.assertEqual(new_right, r.right)
self.assertEqual(expected_left, r.left)
self.assertEqual(old_width, r.width)
def test_right__invalid_value(self):
"""Ensures the right attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.right = value
def test_right__del(self):
"""Ensures the right attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.right
def test_top(self):
"""Changing the top attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_top = 10
r.top = new_top
self.assertEqual(Rect(1, new_top, 3, 4), r)
self.assertEqual(new_top, r.top)
def test_top__invalid_value(self):
"""Ensures the top attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.top = value
def test_top__del(self):
"""Ensures the top attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.top
def test_bottom(self):
"""Changing the bottom attribute moves the rect and does not change
the rect's height
"""
r = Rect(1, 2, 3, 4)
new_bottom = r.bottom + 20
expected_top = r.top + 20
old_height = r.height
r.bottom = new_bottom
self.assertEqual(new_bottom, r.bottom)
self.assertEqual(expected_top, r.top)
self.assertEqual(old_height, r.height)
def test_bottom__invalid_value(self):
"""Ensures the bottom attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.bottom = value
def test_bottom__del(self):
"""Ensures the bottom attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.bottom
def test_centerx(self):
"""Changing the centerx attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_centerx = r.centerx + 20
expected_left = r.left + 20
old_width = r.width
r.centerx = new_centerx
self.assertEqual(new_centerx, r.centerx)
self.assertEqual(expected_left, r.left)
self.assertEqual(old_width, r.width)
def test_centerx__invalid_value(self):
"""Ensures the centerx attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.centerx = value
def test_centerx__del(self):
"""Ensures the centerx attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.centerx
def test_centery(self):
"""Changing the centery attribute moves the rect and does not change
the rect's width
"""
r = Rect(1, 2, 3, 4)
new_centery = r.centery + 20
expected_top = r.top + 20
old_height = r.height
r.centery = new_centery
self.assertEqual(new_centery, r.centery)
self.assertEqual(expected_top, r.top)
self.assertEqual(old_height, r.height)
def test_centery__invalid_value(self):
"""Ensures the centery attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.centery = value
def test_centery__del(self):
"""Ensures the centery attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.centery
def test_topleft(self):
"""Changing the topleft attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.topleft = new_topleft
self.assertEqual(new_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_topleft__invalid_value(self):
"""Ensures the topleft attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.topleft = value
def test_topleft__del(self):
"""Ensures the topleft attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.topleft
def test_bottomleft(self):
"""Changing the bottomleft attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_bottomleft = (r.left + 20, r.bottom + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.bottomleft = new_bottomleft
self.assertEqual(new_bottomleft, r.bottomleft)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_bottomleft__invalid_value(self):
"""Ensures the bottomleft attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.bottomleft = value
def test_bottomleft__del(self):
"""Ensures the bottomleft attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.bottomleft
def test_topright(self):
"""Changing the topright attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_topright = (r.right + 20, r.top + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.topright = new_topright
self.assertEqual(new_topright, r.topright)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_topright__invalid_value(self):
"""Ensures the topright attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.topright = value
def test_topright__del(self):
"""Ensures the topright attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.topright
def test_bottomright(self):
"""Changing the bottomright attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_bottomright = (r.right + 20, r.bottom + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.bottomright = new_bottomright
self.assertEqual(new_bottomright, r.bottomright)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_bottomright__invalid_value(self):
"""Ensures the bottomright attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.bottomright = value
def test_bottomright__del(self):
"""Ensures the bottomright attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.bottomright
def test_center(self):
"""Changing the center attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_center = (r.centerx + 20, r.centery + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.center = new_center
self.assertEqual(new_center, r.center)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_center__invalid_value(self):
"""Ensures the center attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.center = value
def test_center__del(self):
"""Ensures the center attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.center
def test_midleft(self):
"""Changing the midleft attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midleft = (r.left + 20, r.centery + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midleft = new_midleft
self.assertEqual(new_midleft, r.midleft)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midleft__invalid_value(self):
"""Ensures the midleft attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midleft = value
def test_midleft__del(self):
"""Ensures the midleft attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midleft
def test_midright(self):
"""Changing the midright attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midright = (r.right + 20, r.centery + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midright = new_midright
self.assertEqual(new_midright, r.midright)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midright__invalid_value(self):
"""Ensures the midright attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midright = value
def test_midright__del(self):
"""Ensures the midright attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midright
def test_midtop(self):
"""Changing the midtop attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midtop = (r.centerx + 20, r.top + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midtop = new_midtop
self.assertEqual(new_midtop, r.midtop)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midtop__invalid_value(self):
"""Ensures the midtop attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midtop = value
def test_midtop__del(self):
"""Ensures the midtop attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midtop
def test_midbottom(self):
"""Changing the midbottom attribute moves the rect and does not change
the rect's size
"""
r = Rect(1, 2, 3, 4)
new_midbottom = (r.centerx + 20, r.bottom + 30)
expected_topleft = (r.left + 20, r.top + 30)
old_size = r.size
r.midbottom = new_midbottom
self.assertEqual(new_midbottom, r.midbottom)
self.assertEqual(expected_topleft, r.topleft)
self.assertEqual(old_size, r.size)
def test_midbottom__invalid_value(self):
"""Ensures the midbottom attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.midbottom = value
def test_midbottom__del(self):
"""Ensures the midbottom attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.midbottom
def test_width(self):
"""Changing the width resizes the rect from the top-left corner"""
r = Rect(1, 2, 3, 4)
new_width = 10
old_topleft = r.topleft
old_height = r.height
r.width = new_width
self.assertEqual(new_width, r.width)
self.assertEqual(old_height, r.height)
self.assertEqual(old_topleft, r.topleft)
def test_width__invalid_value(self):
"""Ensures the width attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.width = value
def test_width__del(self):
"""Ensures the width attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.width
def test_height(self):
"""Changing the height resizes the rect from the top-left corner"""
r = Rect(1, 2, 3, 4)
new_height = 10
old_topleft = r.topleft
old_width = r.width
r.height = new_height
self.assertEqual(new_height, r.height)
self.assertEqual(old_width, r.width)
self.assertEqual(old_topleft, r.topleft)
def test_height__invalid_value(self):
"""Ensures the height attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.height = value
def test_height__del(self):
"""Ensures the height attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.height
def test_size(self):
"""Changing the size resizes the rect from the top-left corner"""
r = Rect(1, 2, 3, 4)
new_size = (10, 20)
old_topleft = r.topleft
r.size = new_size
self.assertEqual(new_size, r.size)
self.assertEqual(old_topleft, r.topleft)
def test_size__invalid_value(self):
"""Ensures the size attribute handles invalid values correctly."""
r = Rect(0, 0, 1, 1)
for value in (None, [], "1", 1, (1,), [1, 2, 3]):
with self.assertRaises(TypeError):
r.size = value
def test_size__del(self):
"""Ensures the size attribute can't be deleted."""
r = Rect(0, 0, 1, 1)
with self.assertRaises(AttributeError):
del r.size
def test_contains(self):
r = Rect(1, 2, 3, 4)
self.assertTrue(
r.contains(Rect(2, 3, 1, 1)), "r does not contain Rect(2, 3, 1, 1)"
)
self.assertTrue(Rect(2, 3, 1, 1) in r, "r does not contain Rect(2, 3, 1, 1) 2")
self.assertTrue(
r.contains(Rect(r)), "r does not contain the same rect as itself"
)
self.assertTrue(r in Rect(r), "r does not contain the same rect as itself")
self.assertTrue(
r.contains(Rect(2, 3, 0, 0)),
"r does not contain an empty rect within its bounds",
)
self.assertTrue(
Rect(2, 3, 0, 0) in r,
"r does not contain an empty rect within its bounds",
)
self.assertFalse(r.contains(Rect(0, 0, 1, 2)), "r contains Rect(0, 0, 1, 2)")
self.assertFalse(r.contains(Rect(4, 6, 1, 1)), "r contains Rect(4, 6, 1, 1)")
self.assertFalse(r.contains(Rect(4, 6, 0, 0)), "r contains Rect(4, 6, 0, 0)")
self.assertFalse(Rect(0, 0, 1, 2) in r, "r contains Rect(0, 0, 1, 2)")
self.assertFalse(Rect(4, 6, 1, 1) in r, "r contains Rect(4, 6, 1, 1)")
self.assertFalse(Rect(4, 6, 0, 0) in r, "r contains Rect(4, 6, 0, 0)")
self.assertTrue(2 in Rect(0, 0, 1, 2), "r does not contain 2")
self.assertFalse(3 in Rect(0, 0, 1, 2), "r contains 3")
self.assertRaises(TypeError, lambda: "string" in Rect(0, 0, 1, 2))
self.assertRaises(TypeError, lambda: 4 + 3j in Rect(0, 0, 1, 2))
def test_collidepoint(self):
r = Rect(1, 2, 3, 4)
self.assertTrue(
r.collidepoint(r.left, r.top), "r does not collide with point (left, top)"
)
self.assertFalse(
r.collidepoint(r.left - 1, r.top), "r collides with point (left - 1, top)"
)
self.assertFalse(
r.collidepoint(r.left, r.top - 1), "r collides with point (left, top - 1)"
)
self.assertFalse(
r.collidepoint(r.left - 1, r.top - 1),
"r collides with point (left - 1, top - 1)",
)
self.assertTrue(
r.collidepoint(r.right - 1, r.bottom - 1),
"r does not collide with point (right - 1, bottom - 1)",
)
self.assertFalse(
r.collidepoint(r.right, r.bottom), "r collides with point (right, bottom)"
)
self.assertFalse(
r.collidepoint(r.right - 1, r.bottom),
"r collides with point (right - 1, bottom)",
)
self.assertFalse(
r.collidepoint(r.right, r.bottom - 1),
"r collides with point (right, bottom - 1)",
)
def test_inflate__larger(self):
"""Ensures inflating a rect keeps its center the same
and grows dimensions by correct values."""
r = Rect(2, 4, 6, 8)
r2 = r.inflate(4, 6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 2, r2.left)
self.assertEqual(r.top - 3, r2.top)
self.assertEqual(r.right + 2, r2.right)
self.assertEqual(r.bottom + 3, r2.bottom)
self.assertEqual(r.width + 4, r2.width)
self.assertEqual(r.height + 6, r2.height)
def test_inflate__smaller(self):
"""Ensures deflating a rect keeps its center the same
and shrinks dimensions by correct values."""
r = Rect(2, 4, 6, 8)
r2 = r.inflate(-4, -6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.top + 3, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.bottom - 3, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height - 6, r2.height)
def test_inflate_ip__larger(self):
"""Ensures inflating a rect in place keeps its center the same
and grows dimensions by correct values."""
r = Rect(2, 4, 6, 8)
r2 = Rect(r)
r2.inflate_ip(4, 6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 2, r2.left)
self.assertEqual(r.top - 3, r2.top)
self.assertEqual(r.right + 2, r2.right)
self.assertEqual(r.bottom + 3, r2.bottom)
self.assertEqual(r.width + 4, r2.width)
self.assertEqual(r.height + 6, r2.height)
def test_inflate_ip__smaller(self):
"""Ensures deflating a rect in place keeps its center the same
and shrinks dimensions by correct values."""
r = Rect(2, 4, 6, 8)
r2 = Rect(r)
r2.inflate_ip(-4, -6)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.top + 3, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.bottom - 3, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height - 6, r2.height)
def test_scale_by__larger_single_argument(self):
"""The scale method scales around the center of the rectangle"""
r = Rect(2, 4, 6, 8)
r2 = r.scale_by(2)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 3, r2.left)
self.assertEqual(r.top - 4, r2.top)
self.assertEqual(r.right + 3, r2.right)
self.assertEqual(r.bottom + 4, r2.bottom)
self.assertEqual(r.width * 2, r2.width)
self.assertEqual(r.height * 2, r2.height)
def test_scale_by__larger_single_argument_kwarg(self):
"""The scale method scales around the center of the rectangle using
keyword arguments 'x' and 'y'"""
r = Rect(2, 4, 6, 8)
r2 = r.scale_by(x=2)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 3, r2.left)
self.assertEqual(r.top - 4, r2.top)
self.assertEqual(r.right + 3, r2.right)
self.assertEqual(r.bottom + 4, r2.bottom)
self.assertEqual(r.width * 2, r2.width)
self.assertEqual(r.height * 2, r2.height)
def test_scale_by__smaller_single_argument(self):
"""The scale method scales around the center of the rectangle"""
r = Rect(2, 4, 8, 8)
r2 = r.scale_by(0.5)
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.top + 2, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.bottom - 2, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height - 4, r2.height)
def test_scale_by__larger(self):
"""The scale method scales around the center of the rectangle"""
# arrange
r = Rect(2, 4, 6, 8)
# act
r2 = r.scale_by(2, 4)
# assert
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 3, r2.left)
self.assertEqual(r.centery - r.h * 4 / 2, r2.top)
self.assertEqual(r.right + 3, r2.right)
self.assertEqual(r.centery + r.h * 4 / 2, r2.bottom)
self.assertEqual(r.width * 2, r2.width)
self.assertEqual(r.height * 4, r2.height)
def test_scale_by__larger_kwargs_scale_by(self):
"""
The scale method scales around the center of the rectangle
Uses 'scale_by' kwarg.
"""
# arrange
r = Rect(2, 4, 6, 8)
# act
r2 = r.scale_by(scale_by=(2, 4))
r3 = r.scale_by((2, 4))
self.assertEqual(r2, r3)
# assert
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 3, r2.left)
self.assertEqual(r.centery - r.h * 4 / 2, r2.top)
self.assertEqual(r.right + 3, r2.right)
self.assertEqual(r.centery + r.h * 4 / 2, r2.bottom)
self.assertEqual(r.width * 2, r2.width)
self.assertEqual(r.height * 4, r2.height)
def test_scale_by__larger_kwargs(self):
"""
The scale method scales around the center of the rectangle
Uses 'x' and 'y' kwargs.
"""
# arrange
r = Rect(2, 4, 6, 8)
# act
r2 = r.scale_by(x=2, y=4)
# assert
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left - 3, r2.left)
self.assertEqual(r.centery - r.h * 4 / 2, r2.top)
self.assertEqual(r.right + 3, r2.right)
self.assertEqual(r.centery + r.h * 4 / 2, r2.bottom)
self.assertEqual(r.width * 2, r2.width)
self.assertEqual(r.height * 4, r2.height)
def test_scale_by__smaller(self):
"""The scale method scales around the center of the rectangle"""
# arrange
r = Rect(2, 4, 8, 8)
# act
r2 = r.scale_by(0.5, 0.25)
# assert
self.assertEqual(r.center, r2.center)
self.assertEqual(r.left + 2, r2.left)
self.assertEqual(r.centery - r.h / 4 / 2, r2.top)
self.assertEqual(r.right - 2, r2.right)
self.assertEqual(r.centery + r.h / 4 / 2, r2.bottom)
self.assertEqual(r.width - 4, r2.width)
self.assertEqual(r.height // 4, r2.height)
def test_scale_by__subzero(self):
"""The scale method scales around the center of the rectangle"""
r = Rect(2, 4, 6, 8)
r.scale_by(0)
r.scale_by(-1)
r.scale_by(-0.000001)
r.scale_by(0.00001)
rx1 = r.scale_by(10, 1)
self.assertEqual(r.centerx - r.w * 10 / 2, rx1.x)
self.assertEqual(r.y, rx1.y)
self.assertEqual(r.w * 10, rx1.w)
self.assertEqual(r.h, rx1.h)
rx2 = r.scale_by(-10, 1)
self.assertEqual(rx1.x, rx2.x)
self.assertEqual(rx1.y, rx2.y)
self.assertEqual(rx1.w, rx2.w)
self.assertEqual(rx1.h, rx2.h)
ry1 = r.scale_by(1, 10)
self.assertEqual(r.x, ry1.x)
self.assertEqual(r.centery - r.h * 10 / 2, ry1.y)
self.assertEqual(r.w, ry1.w)
self.assertEqual(r.h * 10, ry1.h)
ry2 = r.scale_by(1, -10)
self.assertEqual(ry1.x, ry2.x)
self.assertEqual(ry1.y, ry2.y)
self.assertEqual(ry1.w, ry2.w)
self.assertEqual(ry1.h, ry2.h)
r1 = r.scale_by(10)
self.assertEqual(r.centerx - r.w * 10 / 2, r1.x)
self.assertEqual(r.centery - r.h * 10 / 2, r1.y)
self.assertEqual(r.w * 10, r1.w)
self.assertEqual(r.h * 10, r1.h)
def test_scale_by_identity(self):
"""The scale method scales around the center of the rectangle"""
# arrange
r = Rect(2, 4, 6, 8)
# act
actual = r.scale_by(1, 1)
# assert
self.assertEqual(r.x, actual.x)
self.assertEqual(r.y, actual.y)
self.assertEqual(r.w, actual.w)
self.assertEqual(r.h, actual.h)
def test_scale_by_negative_identity(self):
"""The scale method scales around the center of the rectangle"""
# arrange
r = Rect(2, 4, 6, 8)
# act
actual = r.scale_by(-1, -1)
# assert
self.assertEqual(r.x, actual.x)
self.assertEqual(r.y, actual.y)
self.assertEqual(r.w, actual.w)
self.assertEqual(r.h, actual.h)
def test_scale_by_identity_single_argument(self):