7
7
import textwrap
8
8
import string
9
9
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
11
11
12
12
import pytest
13
13
from assertionlib import assertion
17
17
if TYPE_CHECKING :
18
18
import _pytest
19
19
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
+
20
28
21
29
class BasicMapping :
22
30
def __init__ (self , dct : dict [str , int ]) -> None :
@@ -75,9 +83,14 @@ def test_eq(self, obj: UserMapping[str, int]) -> None:
75
83
def test_getitem (self , obj : UserMapping [str , int ], key : str , value : int ) -> None :
76
84
assertion .eq (obj [key ], value )
77
85
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 :
79
92
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 )
81
94
82
95
cls = type (obj )
83
96
ref2 = cls (zip (string .ascii_lowercase [:12 ], range (12 )))
@@ -97,7 +110,12 @@ def test_repr(self, obj: UserMapping[str, int]) -> None:
97
110
'l': 11,
98
111
}})
99
112
""" ).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 )
101
119
102
120
def test_hash (self , obj : UserMapping [str , int ]) -> None :
103
121
if isinstance (obj , MutableUserMapping ):
@@ -134,6 +152,10 @@ def test_fromkeys(self, obj: UserMapping[str, int]) -> None:
134
152
assertion .isinstance (dct , cls )
135
153
assertion .eq (dct .keys (), obj .keys ())
136
154
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
+
137
159
def test_get (self , obj : UserMapping [str , int ]) -> None :
138
160
assertion .eq (obj .get ("a" ), 0 )
139
161
assertion .is_ (obj .get ("d" ), None )
0 commit comments