Skip to content

Commit

Permalink
added tests for hook register guards
Browse files Browse the repository at this point in the history
  • Loading branch information
tclose committed Dec 6, 2023
1 parent f4ef5bb commit 2f6af34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 28 deletions.
2 changes: 1 addition & 1 deletion fileformats/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ._version import __version__
from .classifier import Classifier, ClassifierCategory
from .datatype import DataType
from .fileset import FileSet
from .fileset import FileSet, MockMixin
from .field import Field
from .utils import (
to_mime,
Expand Down
4 changes: 2 additions & 2 deletions fileformats/core/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ def type_match(a, b) -> bool:
ftype = fparam.annotation
if mname != fname:
differences.append(
f"Name of parameter at position {i}: {mname} vs {fname}"
f"name of parameter at position {i}: {mname} vs {fname}"
)
elif not type_match(mtype, ftype):
differences.append(f"Type of '{mname}' arg: {mtype} vs {ftype}")
if not type_match(msig.return_annotation, fsig.return_annotation):
differences.append(
f"Return type: {msig.return_annotation} vs {fsig.return_annotation}"
f"return type: {msig.return_annotation} vs {fsig.return_annotation}"
)
if differences:
raise TypeError(
Expand Down
25 changes: 0 additions & 25 deletions fileformats/core/tests/test_extras.py

This file was deleted.

73 changes: 73 additions & 0 deletions fileformats/core/tests/test_hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from pathlib import Path
import platform
import typing as ty
import pytest
from fileformats.core import hook, MockMixin, FileSet
from fileformats.testing import Foo


def test_sample():
test_inst = Foo.sample()
assert test_inst.fspath.exists()
assert test_inst.fspath.suffix == ".foo"


def test_mock():
mock = Foo.mock()
if platform.system() == "Windows":
expected = Path(f"{Path().cwd().drive}\\mock\\foo.foo")
else:
expected = Path("/mock/foo.foo")
assert mock.fspath == expected
assert not mock.fspath.exists()
assert isinstance(mock, MockMixin)


class Woo(FileSet):
@hook.extra
def test_hook(self, a: int, b: float, c: ty.Optional[str] = None) -> float:
raise NotImplementedError


def test_hook_signature_no_default():
@Woo.test_hook.register
def woo_test_hook(woo: Woo, a: int, b: float) -> float:
pass


def test_hook_signature1():

with pytest.raises(TypeError, match="missing required argument"):

@Woo.test_hook.register
def woo_test_hook(woo: Woo, a: int) -> float:
pass


def test_hook_signature2():

with pytest.raises(TypeError, match="name of parameter"):

@Woo.test_hook.register
def woo_test_hook(woo: Woo, a: int, d: str) -> float:
pass


def test_hook_signature3():

with pytest.raises(TypeError, match="found additional argument"):

@Woo.test_hook.register
def woo_test_hook(
woo: Woo, a: int, b: float, c: ty.Optional[str], d: int
) -> float:
pass


def test_hook_signature4():

with pytest.raises(TypeError, match="return type"):

@Woo.test_hook.register
def woo_test_hook(woo: Woo, a: int, b: str) -> int:
pass

0 comments on commit 2f6af34

Please sign in to comment.