Skip to content

Commit

Permalink
tests: Fix warts found by flake8
Browse files Browse the repository at this point in the history
This includes:
- unused variables and imports;
- ambiguous variable names which should have been fixed by ppb#85;
- a confusing chain of equalities that likely didn't test what it was meant to.
  • Loading branch information
nbraud committed Feb 4, 2019
1 parent a5d3c30 commit e6e4d90
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 177 deletions.
8 changes: 4 additions & 4 deletions tests/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
import perf # type: ignore
import perf # type: ignore
from ppb_vector import Vector2
from utils import *

Expand All @@ -8,11 +8,11 @@
y = Vector2(0, 1)
scalar = 123

for f in BINARY_OPS + BINARY_SCALAR_OPS + BOOL_OPS: # type: ignore
for f in BINARY_OPS + BINARY_SCALAR_OPS + BOOL_OPS: # type: ignore
r.bench_func(f.__name__, f, x, y)

for f in UNARY_OPS + UNARY_SCALAR_OPS: # type: ignore
for f in UNARY_OPS + UNARY_SCALAR_OPS: # type: ignore
r.bench_func(f.__name__, f, x)

for f in SCALAR_OPS: # type: ignore
for f in SCALAR_OPS: # type: ignore
r.bench_func(f.__name__, f, x, scalar)
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

def setup_hypothesis():
from hypothesis import settings, Verbosity

settings.register_profile("ci", max_examples=1000)
settings.register_profile("dev", max_examples=10)
settings.register_profile("debug", max_examples=10, verbosity=Verbosity.verbose)
Expand Down
17 changes: 12 additions & 5 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
from ppb_vector import Vector2
from utils import vector_likes, vectors

class V(Vector2): pass

@pytest.mark.parametrize('vector_like', vector_likes(), ids=lambda x: type(x).__name__) # type: ignore
@pytest.mark.parametrize('cls', [Vector2, V]) # type: ignore
class V(Vector2):
pass


@pytest.mark.parametrize(
"vector_like", vector_likes(), ids=lambda x: type(x).__name__
) # type: ignore
@pytest.mark.parametrize("cls", [Vector2, V]) # type: ignore
def test_convert_class(cls, vector_like):
vector = cls.convert(vector_like)
assert isinstance(vector, cls)
Expand All @@ -18,22 +23,24 @@ def test_convert_class(cls, vector_like):
def test_convert_tuple(vector: Vector2):
assert vector == tuple(vector) == (vector.x, vector.y)


@given(vector=vectors())
def test_convert_list(vector: Vector2):
assert vector == list(vector) == [vector.x, vector.y]


@given(vector=vectors())
def test_convert_dict(vector: Vector2):
assert vector == vector.asdict()


@pytest.mark.parametrize('coerce', [tuple, list, Vector2.asdict])
@pytest.mark.parametrize("coerce", [tuple, list, Vector2.asdict])
@given(x=vectors())
def test_convert_roundtrip(coerce, x: Vector2):
assert x == Vector2(coerce(x))


@pytest.mark.parametrize('coerce', [tuple, list])
@pytest.mark.parametrize("coerce", [tuple, list])
@given(x=vectors())
def test_convert_roundtrip_positional(coerce, x: Vector2):
assert x == Vector2(*coerce(x))
19 changes: 13 additions & 6 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,43 @@

class V1(Vector2):
"""Arbitrary subclass of Vector2."""

pass


class V11(V1):
"""Subclass of V1."""

pass


class V2(Vector2):
"""Arbitrary subclass of Vector2, distinct from V1."""

pass


@pytest.mark.parametrize('op', BINARY_OPS)
@pytest.mark.parametrize("op", BINARY_OPS)
@given(x=st.builds(V1, vectors()), y=st.builds(V1, units()))
def test_binop_same(op, x: V1, y: V2):
assert isinstance(op(x, y), V1)


@pytest.mark.parametrize('op', BINARY_OPS)
@pytest.mark.parametrize("op", BINARY_OPS)
@given(x=vectors(), y=units())
def test_binop_different(op, x: Vector2, y: Vector2):
assert isinstance(op(V1(x), V2(y)), (V1, V2))
assert isinstance(op(V2(x), V1(y)), (V1, V2))


@pytest.mark.parametrize('op', BINARY_OPS)
@pytest.mark.parametrize("op", BINARY_OPS)
@given(x=st.builds(V1, vectors()), y=st.builds(V1, units()))
def test_binop_subclass(op, x: V1, y: V1):
assert isinstance(op(V11(x), y), V11)
assert isinstance(op(x, V11(y)), V11)


@pytest.mark.parametrize('op', SCALAR_OPS)
@pytest.mark.parametrize("op", SCALAR_OPS)
@given(x=st.builds(V1, vectors()), scalar=floats())
def test_vnumop(op, x: V1, scalar: float):
try:
Expand All @@ -48,7 +53,7 @@ def test_vnumop(op, x: V1, scalar: float):
reject()


@pytest.mark.parametrize('op', UNARY_OPS)
@pytest.mark.parametrize("op", UNARY_OPS)
@given(x=st.builds(V1, vectors()))
def test_monop(op, x):
try:
Expand All @@ -58,7 +63,9 @@ def test_monop(op, x):
reject()


@pytest.mark.parametrize('op', BINARY_OPS + BINARY_SCALAR_OPS + BOOL_OPS) # type: ignore
@pytest.mark.parametrize(
"op", BINARY_OPS + BINARY_SCALAR_OPS + BOOL_OPS # type: ignore
)
@given(x=vectors(), y=units())
def test_binop_vectorlike(op, x: Vector2, y: Vector2):
"""Test that `op` accepts a vector-like second parameter."""
Expand Down
6 changes: 3 additions & 3 deletions tests/test_vector2_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ def test_addition_vector_list():

def test_addition_vector_dict():
test_vector = Vector2(1, 1)
test_dict = {'x': 3, 'y': 5}
test_dict = {"x": 3, "y": 5}
result = test_vector + test_dict
assert result == Vector2(4, 6)


data = [
([Vector2(1, 1), (2, 2)], Vector2(3, 3)),
([Vector2(1, 2), [2, 2]], Vector2(3, 4)),
([Vector2(1, 2), {'x': 2, 'y': 2}], Vector2(3, 4)),
([Vector2(1, 2), {"x": 2, "y": 2}], Vector2(3, 4)),
([Vector2(10, 16), Vector2(2, 2)], Vector2(12, 18)),
([Vector2(25, 22), (12, 92)], Vector2(37, 114)),
([Vector2(25, 22), Vector2(22, 61)], Vector2(47, 83)),
Expand All @@ -43,6 +43,6 @@ def test_addition_vector_dict():
]


@pytest.mark.parametrize('test_input, expected', data)
@pytest.mark.parametrize("test_input, expected", data)
def test_multiples_values(test_input, expected):
assert (test_input[0] + test_input[1]) == expected
36 changes: 17 additions & 19 deletions tests/test_vector2_angle.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
from ppb_vector import Vector2
from math import isclose
import pytest # type: ignore
from hypothesis import assume, given, note
from hypothesis import assume, given
from utils import angle_isclose, floats, vectors


@pytest.mark.parametrize("left, right, expected", [
(Vector2(1, 1), Vector2(0, -1), -135),
(Vector2(1, 1), Vector2(-1, 0), 135),
(Vector2(0, 1), Vector2(0, -1), 180),
(Vector2(-1, -1), Vector2(1, 0), 135),
(Vector2(-1, -1), Vector2(-1, 0), -45),
(Vector2(1, 0), Vector2(0, 1), 90),
(Vector2(1, 0), Vector2(1, 0), 0),
])
@pytest.mark.parametrize(
"left, right, expected",
[
(Vector2(1, 1), Vector2(0, -1), -135),
(Vector2(1, 1), Vector2(-1, 0), 135),
(Vector2(0, 1), Vector2(0, -1), 180),
(Vector2(-1, -1), Vector2(1, 0), 135),
(Vector2(-1, -1), Vector2(-1, 0), -45),
(Vector2(1, 0), Vector2(0, 1), 90),
(Vector2(1, 0), Vector2(1, 0), 0),
],
)
def test_angle(left, right, expected):
lr = left.angle(right)
rl = right.angle(left)
Expand All @@ -23,10 +26,7 @@ def test_angle(left, right, expected):
assert isclose(rl, 180 if expected == 180 else -expected)


@given(
left=vectors(),
right=vectors(),
)
@given(left=vectors(), right=vectors())
def test_angle_range(left, right):
"""Vector2.angle produces values in [-180; 180] and is antisymmetric.
Expand All @@ -38,18 +38,16 @@ def test_angle_range(left, right):
assert -180 < rl <= 180
assert angle_isclose(lr, -rl)

@given(
left=vectors(),
middle=vectors(),
right=vectors(),
)

@given(left=vectors(), middle=vectors(), right=vectors())
def test_angle_additive(left, middle, right):
"""left.angle(middle) + middle.angle(right) == left.angle(right)"""
lm = left.angle(middle)
mr = middle.angle(right)
lr = left.angle(right)
assert angle_isclose(lm + mr, lr)


@given(x=vectors(), scalar=floats())
def test_angle_aligned(x: Vector2, scalar: float):
"""x.angle(scalar * x) is 0 or 180, depending on whether scalar > 0"""
Expand Down
7 changes: 4 additions & 3 deletions tests/test_vector2_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from ppb_vector import Vector2


class V(Vector2): pass
class V(Vector2):
pass


@pytest.mark.parametrize('cls', [Vector2, V])
@pytest.mark.parametrize("cls", [Vector2, V])
@given(x=vectors())
def test_ctor_vector_like(cls, x: Vector2):
for x_like in vector_likes(x):
Expand All @@ -17,7 +18,7 @@ def test_ctor_vector_like(cls, x: Vector2):
assert isinstance(vector, cls)


@pytest.mark.parametrize('cls', [Vector2, V])
@pytest.mark.parametrize("cls", [Vector2, V])
@given(x=floats(), y=floats())
def test_ctor_coordinates(cls, x: float, y: float):
assert cls(x, y) == cls((x, y))
21 changes: 14 additions & 7 deletions tests/test_vector2_dot.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ppb_vector import Vector2

from math import sqrt
import pytest # type: ignore
from hypothesis import assume, given, note
from utils import angles, floats, isclose, vectors

Expand All @@ -11,19 +10,23 @@ def test_dot_axis(vector: Vector2):
assert vector * (1, 0) == vector.x
assert vector * (0, 1) == vector.y


@given(x=vectors(), y=vectors())
def test_dot_commutes(x: Vector2, y: Vector2):
assert x * y == y * x


@given(x=vectors())
def test_dot_length(x: Vector2):
assert isclose(x * x, x.length * x.length)


@given(x=vectors(), y=vectors())
def test_cauchy_schwarz(x: Vector2, y: Vector2):
"""Test the Cauchy-Schwarz inequality: |x·y| ⩽ |x| |y|"""
assert abs(x * y) <= (1 + 1e-12) * x.length * y.length


@given(x=vectors(), y=vectors(), angle=angles())
def test_dot_rotational_invariance(x: Vector2, y: Vector2, angle: float):
"""Test that rotating vectors doesn't change their dot product."""
Expand All @@ -34,14 +37,18 @@ def test_dot_rotational_invariance(x: Vector2, y: Vector2, angle: float):

# Exclude near-orthogonal test inputs
assume(abs(cos_t) > 1e-6)
assert isclose(x * y, x.rotate(angle) * y.rotate(angle),
rel_to=(x, y), rel_exp=2)
assert isclose(x * y, x.rotate(angle) * y.rotate(angle), rel_to=(x, y), rel_exp=2)


MAGNITUDE = 1e10


MAGNITUDE=1e10
@given(x=vectors(max_magnitude=MAGNITUDE), z=vectors(max_magnitude=MAGNITUDE),
y=vectors(max_magnitude=sqrt(MAGNITUDE)),
scalar=floats(max_magnitude=sqrt(MAGNITUDE)))
@given(
x=vectors(max_magnitude=MAGNITUDE),
z=vectors(max_magnitude=MAGNITUDE),
y=vectors(max_magnitude=sqrt(MAGNITUDE)),
scalar=floats(max_magnitude=sqrt(MAGNITUDE)),
)
def test_dot_linear(x: Vector2, y: Vector2, z: Vector2, scalar: float):
"""Test that x · (λ y + z) = λ x·y + x·z"""
inner, outer = x * (scalar * y + z), scalar * x * y + x * z
Expand Down
22 changes: 13 additions & 9 deletions tests/test_vector2_equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@

@given(x=vectors())
def test_equal_self(x: Vector2):
assert x == x
assert x == x


@given(x=vectors())
def test_equal_non_vector(x: Vector2):
assert (x == "foo") == ("foo" == x) == False
assert x != "foo"
assert (x == "foo") == ("foo" == x)


@given(x=vectors(), y=vectors())
def test_equal_symmetric(x: Vector2, y):
assert (x == y) == (y == x)
assert (x == y) == (y == x)

for y_like in vector_likes(y):
assert (x == y_like) == (y_like == x)
for y_like in vector_likes(y):
assert (x == y_like) == (y_like == x)


@given(x=vectors())
def test_non_zero_equal(x: Vector2):
assume(x != (0, 0))
assert x != 1.1 * x
assert x != -x
assume(x != (0, 0))
assert x != 1.1 * x
assert x != -x


@given(x=vectors(), y=vectors())
def test_not_equal_equivalent(x: Vector2, y: Vector2):
assert (x != y) == (not x == y)
assert (x != y) == (not x == y)
Loading

0 comments on commit e6e4d90

Please sign in to comment.