From c44a4774d9359e71a05835c05e399f153f9c6276 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Mon, 1 Apr 2019 16:31:33 +0200 Subject: [PATCH 1/7] Add test dependency on Hypothesis --- requirements-tests.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements-tests.txt b/requirements-tests.txt index e079f8a6..9a54149c 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -1 +1,2 @@ +hypothesis pytest From d6bdf97534ef999b8ef027cd4870b231f2249b8f Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Sat, 30 Mar 2019 11:57:32 +0100 Subject: [PATCH 2/7] tests/camera: Remove test_camera_move It only tests adding vectors, through the `camera.position` attribute. Since it's a plain attribute, this is only testing ppb-vector's addition... --- tests/test_camera.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/tests/test_camera.py b/tests/test_camera.py index 6b29781e..87959a96 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -3,14 +3,6 @@ from ppb.camera import Camera -def test_camera_move(): - cam = Camera() - cam.position = Vector(500, 500) - assert cam.position == Vector(500, 500) - cam.position += Vector(100, 100) - assert cam.position == Vector(600, 600) - - def test_camera_viewport(): cam = Camera(viewport=(0, 0, 800, 600)) assert cam.point_in_viewport(Vector(400, 400)) @@ -70,4 +62,4 @@ def test_viewport_change_affects_frame_height(): cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=80) assert cam.frame_left == -5 cam.viewport_width = 400 - assert cam.frame_left == -2.5 \ No newline at end of file + assert cam.frame_left == -2.5 From 7716876a710df60b5705bc685547c0139a24a134 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Sun, 31 Mar 2019 20:40:22 +0200 Subject: [PATCH 3/7] ppb.testutils: Add an Hypothesis generator for integer-valued vectors --- ppb/testutils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ppb/testutils.py b/ppb/testutils.py index 983090cf..f402639b 100644 --- a/ppb/testutils.py +++ b/ppb/testutils.py @@ -1,11 +1,22 @@ import time from typing import Callable +from hypothesis import strategies as st + +from ppb import Vector from ppb.engine import GameEngine from ppb.events import Quit from ppb.systems import System +def integer_vectors(min_value=None, max_value=None): + return st.builds( + Vector, + st.integers(min_value=min_value, max_value=max_value), + st.integers(min_value=min_value, max_value=max_value), + ) + + class Failer(System): def __init__(self, *, fail: Callable[[GameEngine], bool], message: str, From 0b5d85d3d2212d4b6b87e4230ab9aa5df5698f98 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Sun, 31 Mar 2019 20:41:53 +0200 Subject: [PATCH 4/7] tests/camera: Convert test_camera_viewport to Hypothesis --- tests/test_camera.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/test_camera.py b/tests/test_camera.py index 87959a96..b5906a33 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -1,13 +1,22 @@ +from hypothesis import given + from ppb import BaseSprite from ppb import Vector from ppb.camera import Camera +from ppb.testutils import integer_vectors + + +ONE_K = 1024 +ONE_M = ONE_K * ONE_K -def test_camera_viewport(): - cam = Camera(viewport=(0, 0, 800, 600)) - assert cam.point_in_viewport(Vector(400, 400)) - assert not cam.point_in_viewport(Vector(900, 600)) - assert cam.viewport_offset == Vector(400, 300) +@given(diagonal=integer_vectors(min_value=2, max_value=ONE_M)) +def test_camera_viewport(diagonal: Vector): + x, y = diagonal + cam = Camera(viewport=(0, 0, x, y)) + assert cam.point_in_viewport(0.5 * diagonal) + assert not cam.point_in_viewport(diagonal + (100, 100)) + assert cam.viewport_offset == 0.5 * diagonal def test_camera_point_in_viewport_not_at_origin(): From 903e6debd9d17f25cb13b4b85208b2923e46ee26 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Thu, 4 Apr 2019 13:01:58 +0200 Subject: [PATCH 5/7] testutils: Provide an Hypothesis strategy for ppb.Vector It's the same as ppb-vector's. --- ppb/testutils.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ppb/testutils.py b/ppb/testutils.py index f402639b..000dbbb5 100644 --- a/ppb/testutils.py +++ b/ppb/testutils.py @@ -16,6 +16,13 @@ def integer_vectors(min_value=None, max_value=None): st.integers(min_value=min_value, max_value=max_value), ) +def vectors(max_magnitude=1e75): + return st.builds( + Vector, + st.floats(min_value=-max_magnitude, max_value=max_magnitude), + st.floats(min_value=-max_magnitude, max_value=max_magnitude), + ) + class Failer(System): From f03120474339a0ba56dcb059e037f5f9f3531963 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Thu, 4 Apr 2019 13:03:08 +0200 Subject: [PATCH 6/7] tests/camera: Add Hypothesis strategy for generating random Cameras --- tests/test_camera.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_camera.py b/tests/test_camera.py index b5906a33..b97c5523 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -1,15 +1,23 @@ -from hypothesis import given +from hypothesis import given, note, strategies as st from ppb import BaseSprite from ppb import Vector from ppb.camera import Camera -from ppb.testutils import integer_vectors +from ppb.testutils import integer_vectors, vectors ONE_K = 1024 ONE_M = ONE_K * ONE_K +def cameras(): + return st.builds( + lambda offset, diagonal: Camera(viewport=(*offset, *(offset+diagonal))), + integer_vectors(min_value=-ONE_M, max_value=ONE_M), + integer_vectors(min_value=2, max_value=ONE_M), + ) + + @given(diagonal=integer_vectors(min_value=2, max_value=ONE_M)) def test_camera_viewport(diagonal: Vector): x, y = diagonal From 5d62d216db6e0bb7927d477388a7121c52b53988 Mon Sep 17 00:00:00 2001 From: Nicolas Braud-Santoni Date: Thu, 4 Apr 2019 13:03:58 +0200 Subject: [PATCH 7/7] tests/camera: Roundtrip through viewport and frame coordinates --- tests/test_camera.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_camera.py b/tests/test_camera.py index b97c5523..cdcd1a12 100644 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -35,6 +35,13 @@ def test_camera_point_in_viewport_not_at_origin(): assert not cam.point_in_viewport(Vector(901, 600)) +@given(cam=cameras(), v=vectors()) +def test_camera_roundtrip_frame_viewport(cam: Camera, v: Vector): + """Check that Camera.translate_to_{frame,viewport} are inverse of one another.""" + assert cam.translate_to_frame(cam.translate_to_viewport(v)).isclose(v) + assert cam.translate_to_viewport(cam.translate_to_frame(v)).isclose(v) + + def test_camera_translate_to_frame(): cam = Camera(viewport=(0, 0, 800, 600), pixel_ratio=80) assert cam.position == Vector(0, 0)