|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +from pandas.errors import ( |
| 7 | + PyperclipException, |
| 8 | + PyperclipWindowsException, |
| 9 | +) |
| 10 | + |
6 | 11 | from pandas import (
|
7 | 12 | DataFrame,
|
8 | 13 | get_option,
|
|
11 | 16 | import pandas._testing as tm
|
12 | 17 |
|
13 | 18 | from pandas.io.clipboard import (
|
| 19 | + CheckedCall, |
| 20 | + _stringifyText, |
14 | 21 | clipboard_get,
|
15 | 22 | clipboard_set,
|
16 | 23 | )
|
@@ -110,6 +117,81 @@ def df(request):
|
110 | 117 | raise ValueError
|
111 | 118 |
|
112 | 119 |
|
| 120 | +@pytest.fixture |
| 121 | +def mock_ctypes(monkeypatch): |
| 122 | + """ |
| 123 | + Mocks WinError to help with testing the clipboard. |
| 124 | + """ |
| 125 | + |
| 126 | + def _mock_win_error(): |
| 127 | + return "Window Error" |
| 128 | + |
| 129 | + # Set raising to False because WinError won't exist on non-windows platforms |
| 130 | + with monkeypatch.context() as m: |
| 131 | + m.setattr("ctypes.WinError", _mock_win_error, raising=False) |
| 132 | + yield |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.usefixtures("mock_ctypes") |
| 136 | +def test_checked_call_with_bad_call(monkeypatch): |
| 137 | + """ |
| 138 | + Give CheckCall a function that returns a falsey value and |
| 139 | + mock get_errno so it returns false so an exception is raised. |
| 140 | + """ |
| 141 | + |
| 142 | + def _return_false(): |
| 143 | + return False |
| 144 | + |
| 145 | + monkeypatch.setattr("pandas.io.clipboard.get_errno", lambda: True) |
| 146 | + msg = f"Error calling {_return_false.__name__} \\(Window Error\\)" |
| 147 | + |
| 148 | + with pytest.raises(PyperclipWindowsException, match=msg): |
| 149 | + CheckedCall(_return_false)() |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.usefixtures("mock_ctypes") |
| 153 | +def test_checked_call_with_valid_call(monkeypatch): |
| 154 | + """ |
| 155 | + Give CheckCall a function that returns a truthy value and |
| 156 | + mock get_errno so it returns true so an exception is not raised. |
| 157 | + The function should return the results from _return_true. |
| 158 | + """ |
| 159 | + |
| 160 | + def _return_true(): |
| 161 | + return True |
| 162 | + |
| 163 | + monkeypatch.setattr("pandas.io.clipboard.get_errno", lambda: False) |
| 164 | + |
| 165 | + # Give CheckedCall a callable that returns a truthy value s |
| 166 | + checked_call = CheckedCall(_return_true) |
| 167 | + assert checked_call() is True |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.parametrize( |
| 171 | + "text", |
| 172 | + [ |
| 173 | + "String_test", |
| 174 | + True, |
| 175 | + 1, |
| 176 | + 1.0, |
| 177 | + 1j, |
| 178 | + ], |
| 179 | +) |
| 180 | +def test_stringify_text(text): |
| 181 | + valid_types = (str, int, float, bool) |
| 182 | + |
| 183 | + if isinstance(text, valid_types): |
| 184 | + result = _stringifyText(text) |
| 185 | + assert result == str(text) |
| 186 | + else: |
| 187 | + msg = ( |
| 188 | + "only str, int, float, and bool values " |
| 189 | + f"can be copied to the clipboard, not {type(text).__name__}" |
| 190 | + ) |
| 191 | + with pytest.raises(PyperclipException, match=msg): |
| 192 | + _stringifyText(text) |
| 193 | + |
| 194 | + |
113 | 195 | @pytest.fixture
|
114 | 196 | def mock_clipboard(monkeypatch, request):
|
115 | 197 | """Fixture mocking clipboard IO.
|
|
0 commit comments