Skip to content

Commit 8f095f6

Browse files
author
Bas van Beek
committed
TST: Add tests for the IPython entry points
1 parent 52ddf8b commit 8f095f6

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
'pyyaml',
4343
'h5py',
4444
'numpy',
45+
'ipython',
4546
]
4647
tests_require += docs_require
4748
tests_require += build_requires

tests/test_dtype_mapping.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import textwrap
55
from typing import TYPE_CHECKING, no_type_check
6-
from collections.abc import Iterator
6+
from collections.abc import Iterator, Callable
77

88
import pytest
99
from assertionlib import assertion
@@ -18,6 +18,14 @@
1818
import numpy.typing as npt
1919
import _pytest
2020

21+
try:
22+
from IPython.lib.pretty import pretty
23+
except ModuleNotFoundError:
24+
IPYTHON: bool = False
25+
pretty = NotImplemented
26+
else:
27+
IPYTHON = True
28+
2129

2230
class BasicMapping:
2331
def __init__(self, dct: dict[str, npt.DTypeLike]) -> None:
@@ -128,16 +136,21 @@ def test_repr(self, obj: DTypeMapping) -> None:
128136
)""").strip()
129137
assertion.str_eq(obj, string1, str_converter=repr)
130138

131-
string2 = textwrap.dedent(f"""
139+
string2 = f"{type(obj).__name__}()"
140+
assertion.str_eq(type(obj)(), string2, str_converter=repr)
141+
142+
@pytest.mark.parametrize("str_func", [
143+
str,
144+
pytest.param(pretty, marks=pytest.mark.skipif(not IPYTHON, reason="Requires IPython")),
145+
], ids=["str", "pretty"])
146+
def test_str(self, obj: DTypeMapping, str_func: Callable[[object], str]) -> None:
147+
string = textwrap.dedent(f"""
132148
{type(obj).__name__}(
133149
a = int64,
134150
b = float64,
135151
c = <U5,
136152
)""").strip()
137-
assertion.str_eq(obj, string2, str_converter=str)
138-
139-
string3 = f"{type(obj).__name__}()"
140-
assertion.str_eq(type(obj)(), string3, str_converter=repr)
153+
assertion.str_eq(obj, string, str_converter=str_func)
141154

142155
@pytest.mark.skipif(sys.version_info < (3, 9), reason="Requires python >= 3.9")
143156
@no_type_check

tests/test_user_mapping.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import textwrap
88
import string
99
from typing import TYPE_CHECKING, no_type_check
10-
from collections.abc import KeysView, ValuesView, ItemsView, Iterator
10+
from collections.abc import KeysView, ValuesView, ItemsView, Iterator, Callable
1111

1212
import pytest
1313
from assertionlib import assertion
@@ -17,6 +17,14 @@
1717
if TYPE_CHECKING:
1818
import _pytest
1919

20+
try:
21+
from IPython.lib.pretty import pretty
22+
except ModuleNotFoundError:
23+
IPYTHON: bool = False
24+
pretty = NotImplemented
25+
else:
26+
IPYTHON = True
27+
2028

2129
class BasicMapping:
2230
def __init__(self, dct: dict[str, int]) -> None:
@@ -75,9 +83,14 @@ def test_eq(self, obj: UserMapping[str, int]) -> None:
7583
def test_getitem(self, obj: UserMapping[str, int], key: str, value: int) -> None:
7684
assertion.eq(obj[key], value)
7785

78-
def test_repr(self, obj: UserMapping[str, int]) -> None:
86+
@pytest.mark.parametrize("str_func", [
87+
str,
88+
repr,
89+
pytest.param(pretty, marks=pytest.mark.skipif(not IPYTHON, reason="Requires IPython")),
90+
], ids=["str", "repr", "pretty"])
91+
def test_repr(self, obj: UserMapping[str, int], str_func: Callable[[object], str]) -> None:
7992
string1 = f"{type(obj).__name__}({{'a': 0, 'b': 1, 'c': 2}})"
80-
assertion.str_eq(obj, string1)
93+
assertion.str_eq(obj, string1, str_converter=str_func)
8194

8295
cls = type(obj)
8396
ref2 = cls(zip(string.ascii_lowercase[:12], range(12)))
@@ -97,7 +110,12 @@ def test_repr(self, obj: UserMapping[str, int]) -> None:
97110
'l': 11,
98111
}})
99112
""").strip()
100-
assertion.str_eq(ref2, string2)
113+
assertion.str_eq(ref2, string2, str_converter=str_func)
114+
115+
@pytest.mark.skipif(not IPYTHON, reason="Rquires IPython")
116+
def test_pretty_repr(self, obj: UserMapping[str, int]) -> None:
117+
string1 = f"{type(obj).__name__}({{'a': 0, 'b': 1, 'c': 2}})"
118+
assertion.str_eq(obj, string1, str_converter=pretty)
101119

102120
def test_hash(self, obj: UserMapping[str, int]) -> None:
103121
if isinstance(obj, MutableUserMapping):
@@ -134,6 +152,10 @@ def test_fromkeys(self, obj: UserMapping[str, int]) -> None:
134152
assertion.isinstance(dct, cls)
135153
assertion.eq(dct.keys(), obj.keys())
136154

155+
def test_key_completions(self, obj: UserMapping[str, int]) -> None:
156+
assertion.isinstance(obj._ipython_key_completions_(), KeysView)
157+
assertion.eq(obj._ipython_key_completions_(), obj.keys())
158+
137159
def test_get(self, obj: UserMapping[str, int]) -> None:
138160
assertion.eq(obj.get("a"), 0)
139161
assertion.is_(obj.get("d"), None)

0 commit comments

Comments
 (0)