mirror of
https://github.com/ChristianLight/tutor.git
synced 2024-09-27 11:49:02 +00:00
33e4f33afe
Now that the mypy bugs have been resolved, we are able to define more precisely and cleanly the types of Actions and Filters. Moreover, can now strongly type named actions and hooks (in consts.py). With such a strong typing, we get early alerts of hooks called with incorrect arguments, which is nothing short of awesome :) This change breaks the hooks API by removing the `context=...` argument. The reason for that is that we cannot insert arbitrary arguments between `P.args, P.kwargs`: https://peps.python.org/pep-0612/#the-components-of-a-paramspec > A function declared as def inner(a: A, b: B, *args: P.args, **kwargs: > P.kwargs) -> R has type Callable[Concatenate[A, B, P], R]. Placing > keyword-only parameters between the *args and **kwargs is forbidden. Getting the documentation to build in nitpicky mode is quite difficult... We need to add `nitpick_ignore` to the docs conf.py, otherwise sphinx complains about many missing class references. This, despite upgrading almost all doc requirements (except docutils).
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
import typing as t
|
|
import unittest
|
|
|
|
from tutor import hooks
|
|
|
|
|
|
class PluginFiltersTests(unittest.TestCase):
|
|
def tearDown(self) -> None:
|
|
super().tearDown()
|
|
hooks.filters.clear_all(context="tests")
|
|
|
|
def run(self, result: t.Any = None) -> t.Any:
|
|
with hooks.contexts.enter("tests"):
|
|
return super().run(result=result)
|
|
|
|
def test_add(self) -> None:
|
|
@hooks.filters.add("tests:count-sheeps")
|
|
def filter1(value: int) -> int:
|
|
return value + 1
|
|
|
|
value = hooks.filters.apply("tests:count-sheeps", 0)
|
|
self.assertEqual(1, value)
|
|
|
|
def test_add_items(self) -> None:
|
|
@hooks.filters.add("tests:add-sheeps")
|
|
def filter1(sheeps: t.List[int]) -> t.List[int]:
|
|
return sheeps + [0]
|
|
|
|
hooks.filters.add_item("tests:add-sheeps", 1)
|
|
hooks.filters.add_item("tests:add-sheeps", 2)
|
|
hooks.filters.add_items("tests:add-sheeps", [3, 4])
|
|
|
|
sheeps: t.List[int] = hooks.filters.apply("tests:add-sheeps", [])
|
|
self.assertEqual([0, 1, 2, 3, 4], sheeps)
|
|
|
|
def test_filter_callbacks(self) -> None:
|
|
callback = hooks.filters.FilterCallback(lambda _: 1)
|
|
self.assertTrue(callback.is_in_context(None))
|
|
self.assertFalse(callback.is_in_context("customcontext"))
|
|
self.assertEqual(1, callback.apply(0))
|
|
|
|
def test_filter_context(self) -> None:
|
|
with hooks.contexts.enter("testcontext"):
|
|
hooks.filters.add_item("test:sheeps", 1)
|
|
hooks.filters.add_item("test:sheeps", 2)
|
|
|
|
self.assertEqual([1, 2], hooks.filters.apply("test:sheeps", []))
|
|
self.assertEqual(
|
|
[1], hooks.filters.apply_from_context("testcontext", "test:sheeps", [])
|
|
)
|
|
|
|
def test_clear_context(self) -> None:
|
|
with hooks.contexts.enter("testcontext"):
|
|
hooks.filters.add_item("test:sheeps", 1)
|
|
hooks.filters.add_item("test:sheeps", 2)
|
|
|
|
self.assertEqual([1, 2], hooks.filters.apply("test:sheeps", []))
|
|
hooks.filters.clear("test:sheeps", context="testcontext")
|
|
self.assertEqual([2], hooks.filters.apply("test:sheeps", []))
|