1313from pandas .util import testing as tm
1414from pandas .util .testing import makeCustomDataframe as mkdf
1515from pandas .io .clipboard .exceptions import PyperclipException
16- from pandas .io .clipboard import clipboard_set , clipboard_get
1716
1817
1918try :
@@ -76,10 +75,53 @@ def df(request):
7675 raise ValueError
7776
7877
78+ @pytest .fixture
79+ def mock_clipboard (mock , request ):
80+ """Fixture mocking clipboard IO.
81+
82+ This mocks pandas.io.clipboard.clipboard_get and
83+ pandas.io.clipboard.clipboard_set.
84+
85+ This uses a local dict for storing data. The dictionary
86+ key used is the test ID, available with ``request.node.name``.
87+
88+ This returns the local dictionary, for direct manipulation by
89+ tests.
90+ """
91+
92+ # our local clipboard for tests
93+ _mock_data = {}
94+
95+ def _mock_set (data ):
96+ _mock_data [request .node .name ] = data
97+
98+ def _mock_get ():
99+ return _mock_data [request .node .name ]
100+
101+ mock_set = mock .patch ("pandas.io.clipboard.clipboard_set" ,
102+ side_effect = _mock_set )
103+ mock_get = mock .patch ("pandas.io.clipboard.clipboard_get" ,
104+ side_effect = _mock_get )
105+ with mock_get , mock_set :
106+ yield _mock_data
107+
108+
109+ @pytest .mark .clipboard
110+ def test_mock_clipboard (mock_clipboard ):
111+ import pandas .io .clipboard
112+ pandas .io .clipboard .clipboard_set ("abc" )
113+ assert "abc" in set (mock_clipboard .values ())
114+ result = pandas .io .clipboard .clipboard_get ()
115+ assert result == "abc"
116+
117+
79118@pytest .mark .single
119+ @pytest .mark .clipboard
80120@pytest .mark .skipif (not _DEPS_INSTALLED ,
81121 reason = "clipboard primitives not installed" )
122+ @pytest .mark .usefixtures ("mock_clipboard" )
82123class TestClipboard (object ):
124+
83125 def check_round_trip_frame (self , data , excel = None , sep = None ,
84126 encoding = None ):
85127 data .to_clipboard (excel = excel , sep = sep , encoding = encoding )
@@ -118,15 +160,18 @@ def test_copy_delim_warning(self, df):
118160 # delimited and excel="True"
119161 @pytest .mark .parametrize ('sep' , ['\t ' , None , 'default' ])
120162 @pytest .mark .parametrize ('excel' , [True , None , 'default' ])
121- def test_clipboard_copy_tabs_default (self , sep , excel , df ):
163+ def test_clipboard_copy_tabs_default (self , sep , excel , df , request ,
164+ mock_clipboard ):
122165 kwargs = build_kwargs (sep , excel )
123166 df .to_clipboard (** kwargs )
124167 if PY2 :
125168 # to_clipboard copies unicode, to_csv produces bytes. This is
126169 # expected behavior
127- assert clipboard_get ().encode ('utf-8' ) == df .to_csv (sep = '\t ' )
170+ result = mock_clipboard [request .node .name ].encode ('utf-8' )
171+ expected = df .to_csv (sep = '\t ' )
172+ assert result == expected
128173 else :
129- assert clipboard_get () == df .to_csv (sep = '\t ' )
174+ assert mock_clipboard [ request . node . name ] == df .to_csv (sep = '\t ' )
130175
131176 # Tests reading of white space separated tables
132177 @pytest .mark .parametrize ('sep' , [None , 'default' ])
@@ -138,7 +183,8 @@ def test_clipboard_copy_strings(self, sep, excel, df):
138183 assert result .to_string () == df .to_string ()
139184 assert df .shape == result .shape
140185
141- def test_read_clipboard_infer_excel (self ):
186+ def test_read_clipboard_infer_excel (self , request ,
187+ mock_clipboard ):
142188 # gh-19010: avoid warnings
143189 clip_kwargs = dict (engine = "python" )
144190
@@ -147,7 +193,7 @@ def test_read_clipboard_infer_excel(self):
147193 1 2
148194 4 Harry Carney
149195 """ .strip ())
150- clipboard_set ( text )
196+ mock_clipboard [ request . node . name ] = text
151197 df = pd .read_clipboard (** clip_kwargs )
152198
153199 # excel data is parsed correctly
@@ -159,15 +205,15 @@ def test_read_clipboard_infer_excel(self):
159205 1 2
160206 3 4
161207 """ .strip ())
162- clipboard_set ( text )
208+ mock_clipboard [ request . node . name ] = text
163209 res = pd .read_clipboard (** clip_kwargs )
164210
165211 text = dedent ("""
166212 a b
167213 1 2
168214 3 4
169215 """ .strip ())
170- clipboard_set ( text )
216+ mock_clipboard [ request . node . name ] = text
171217 exp = pd .read_clipboard (** clip_kwargs )
172218
173219 tm .assert_frame_equal (res , exp )
0 commit comments