Skip to content

Commit

Permalink
🔀 Merge pull request #18 from FleksySDK/tests
Browse files Browse the repository at this point in the history
Add unit-tests for Tokenizer, Layout, gesture, and noise model
  • Loading branch information
astariul authored May 9, 2024
2 parents 8bd4cc7 + 2ea49f0 commit b2e63b1
Show file tree
Hide file tree
Showing 7 changed files with 589 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion kebbie/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ def _extract_infos( # noqa: C901
pass
else:
# Other special characters are ignored
continue
char = None

if char is None:
continue

# Save the character and its key information
# Save it only if it's not already in a previous klayer
Expand Down
21 changes: 21 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import random

import pytest


@pytest.fixture(autouse=True)
def no_requests(monkeypatch):
# Make sure unit-tests don't use any resources pulled from the internet
# using `requests`
monkeypatch.delattr("requests.sessions.Session.request")


@pytest.fixture(scope="session")
def monkeypatch_session():
with pytest.MonkeyPatch.context() as mp:
yield mp


@pytest.fixture
def seeded():
random.seed(36)
29 changes: 29 additions & 0 deletions tests/test_gesture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from kebbie.gesture import MAX_N_POINTS_PER_DIST, MIN_N_POINTS_PER_DIST, make_swipe_gesture
from kebbie.utils import euclidian_dist


def test_make_swipe_gesture_between_2_points(seeded):
control_points = [(0, 0), (100, 100)]

points = make_swipe_gesture(control_points)

d = euclidian_dist(control_points[0], control_points[1])
assert int(d * MIN_N_POINTS_PER_DIST) <= len(points) <= int(d * MAX_N_POINTS_PER_DIST)


def test_make_swipe_gesture_between_more_points(seeded):
points = make_swipe_gesture([(0, 0), (100, 100), (50, 80), (-100, 10)])
assert len(points) > 4


def test_make_swipe_gesture_single_control_point():
assert make_swipe_gesture([(0, 0)]) == [(0, 0)]


def test_make_swipe_gesture_same_points():
assert make_swipe_gesture([(0, 0), (0, 0)]) == [(0, 0)]


def test_make_swipe_gesture_too_small_points(seeded):
points = make_swipe_gesture([(0, 0), (0.1, 0)])
assert len(points) > 2
93 changes: 93 additions & 0 deletions tests/test_layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import pytest

from kebbie.layout import LayoutHelper


@pytest.fixture
def layout():
return LayoutHelper()


def test_get_existing_key_info_same_layer(layout):
q_info = layout.get_key_info("q")
w_info = layout.get_key_info("w")

q_width, q_height, q_x_center, q_y_center, q_layer = q_info
w_width, w_height, w_x_center, w_y_center, w_layer = w_info
assert q_width == w_width
assert q_height == w_height
assert q_x_center < w_x_center
assert q_y_center == w_y_center
assert q_layer == w_layer


def test_get_existing_key_info_different_layer(layout):
q_info = layout.get_key_info("q")
uq_info = layout.get_key_info("Q")

q_width, q_height, q_x_center, q_y_center, q_layer = q_info
uq_width, uq_height, uq_x_center, uq_y_center, uq_layer = uq_info
assert q_width == uq_width
assert q_height == uq_height
assert q_x_center == uq_x_center
assert q_y_center == uq_y_center
assert q_layer < uq_layer


def test_get_existing_key_info_accent(layout):
e_info = layout.get_key_info("e")
é_info = layout.get_key_info("é")

e_width, e_height, e_x_center, e_y_center, e_layer = e_info
é_width, é_height, é_x_center, é_y_center, é_layer = é_info
assert e_width == é_width
assert e_height == é_height
assert e_layer < é_layer


def test_get_non_existing_key_info(layout):
with pytest.raises(KeyError):
layout.get_key_info("☯")


def test_get_key_within_bounds(layout):
f_info = layout.get_key_info("f")
f_width, f_height, f_x_center, f_y_center, f_layer = f_info

assert layout.get_key((f_x_center, f_y_center), 0) == "f"
assert layout.get_key((f_x_center + f_width / 3, f_y_center + f_height / 3), 0) == "f"
assert layout.get_key((f_x_center - f_width / 3, f_y_center - f_height / 3), 0) == "f"

assert layout.get_key((f_x_center, f_y_center), 1) == "F"


def test_get_key_outside_of_bounds(layout):
f_info = layout.get_key_info("f")
f_width, f_height, f_x_center, f_y_center, f_layer = f_info

assert layout.get_key((f_x_center + f_width + 1, f_y_center), 0) != "f"
assert layout.get_key((f_x_center, f_y_center + f_height + 1), 0) != "f"


def test_get_closest_border_key(layout):
assert layout.get_key((-5000, -5000), 0) == "q"


def test_ignore_additional_keyboard_layers():
layout = LayoutHelper(ignore_layers_after=0)
with pytest.raises(KeyError):
layout.get_key_info("Q")


@pytest.mark.parametrize("k", [" ", "."])
def test_special_keys_that_should_exist(layout, k):
try:
layout.get_key_info(k)
except KeyError:
pytest.fail(f"Key `{k}` is not part of the layout")


@pytest.mark.parametrize("k", ["shift", "SHIFT", "mic"])
def test_special_keys_that_should_not_exist(layout, k):
with pytest.raises(KeyError):
layout.get_key_info(k)
Loading

0 comments on commit b2e63b1

Please sign in to comment.