|
| 1 | +# coding: utf-8 |
| 2 | +import os |
| 3 | +import platform |
| 4 | +import random |
| 5 | +import string |
| 6 | +import unittest |
| 7 | + |
| 8 | +from pandas.io.clipboard import ( |
| 9 | + HAS_DISPLAY, |
| 10 | + PyperclipException, |
| 11 | + _executable_exists, |
| 12 | + init_dev_clipboard_clipboard, |
| 13 | + init_klipper_clipboard, |
| 14 | + init_no_clipboard, |
| 15 | + init_osx_pbcopy_clipboard, |
| 16 | + init_osx_pyobjc_clipboard, |
| 17 | + init_qt_clipboard, |
| 18 | + init_windows_clipboard, |
| 19 | + init_wsl_clipboard, |
| 20 | + init_xclip_clipboard, |
| 21 | + init_xsel_clipboard, |
| 22 | +) |
| 23 | + |
| 24 | +# import sys |
| 25 | +# sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
| 26 | + |
| 27 | + |
| 28 | +random.seed(42) # Make the "random" tests reproducible. |
| 29 | + |
| 30 | + |
| 31 | +class _TestClipboard(unittest.TestCase): |
| 32 | + clipboard = None |
| 33 | + supports_unicode = True |
| 34 | + |
| 35 | + @property |
| 36 | + def copy(self): |
| 37 | + return self.clipboard[0] |
| 38 | + |
| 39 | + @property |
| 40 | + def paste(self): |
| 41 | + return self.clipboard[1] |
| 42 | + |
| 43 | + def setUp(self): |
| 44 | + if not self.clipboard: |
| 45 | + self.skipTest("Clipboard not supported.") |
| 46 | + |
| 47 | + def test_copy_simple(self): |
| 48 | + self.copy("pyper\r\nclip") |
| 49 | + |
| 50 | + def test_copy_paste_simple(self): |
| 51 | + msg = "".join( |
| 52 | + random.choice(string.ascii_letters + string.digits) for _ in range(1000) |
| 53 | + ) |
| 54 | + self.copy(msg) |
| 55 | + self.assertEqual(self.paste(), msg) |
| 56 | + |
| 57 | + def test_copy_paste_whitespace(self): |
| 58 | + msg = "".join(random.choice(string.whitespace) for _ in range(1000)) |
| 59 | + self.copy(msg) |
| 60 | + self.assertEqual(self.paste(), msg) |
| 61 | + |
| 62 | + def test_copy_blank(self): |
| 63 | + self.copy("TEST") |
| 64 | + self.copy("") |
| 65 | + self.assertEqual(self.paste(), "") |
| 66 | + |
| 67 | + def test_copy_unicode(self): |
| 68 | + if not self.supports_unicode: |
| 69 | + raise unittest.SkipTest() |
| 70 | + self.copy(u"ಠ_ಠ") |
| 71 | + |
| 72 | + def test_copy_unicode_emoji(self): |
| 73 | + if not self.supports_unicode: |
| 74 | + raise unittest.SkipTest() |
| 75 | + self.copy(u"🙆") |
| 76 | + |
| 77 | + def test_copy_paste_unicode(self): |
| 78 | + if not self.supports_unicode: |
| 79 | + raise unittest.SkipTest() |
| 80 | + msg = u"ಠ_ಠ" |
| 81 | + self.copy(msg) |
| 82 | + self.assertEqual(self.paste(), msg) |
| 83 | + |
| 84 | + def test_copy_paste_unicode_emoji(self): |
| 85 | + if not self.supports_unicode: |
| 86 | + raise unittest.SkipTest() |
| 87 | + msg = u"🙆" |
| 88 | + self.copy(msg) |
| 89 | + self.assertEqual(self.paste(), msg) |
| 90 | + |
| 91 | + def test_non_str(self): |
| 92 | + # Test copying an int. |
| 93 | + self.copy(42) |
| 94 | + self.assertEqual(self.paste(), "42") |
| 95 | + |
| 96 | + self.copy(-1) |
| 97 | + self.assertEqual(self.paste(), "-1") |
| 98 | + |
| 99 | + # Test copying a float. |
| 100 | + self.copy(3.141592) |
| 101 | + self.assertEqual(self.paste(), "3.141592") |
| 102 | + |
| 103 | + # Test copying bools. |
| 104 | + self.copy(True) |
| 105 | + self.assertEqual(self.paste(), "True") |
| 106 | + |
| 107 | + self.copy(False) |
| 108 | + self.assertEqual(self.paste(), "False") |
| 109 | + |
| 110 | + # All other non-str values raise an exception. |
| 111 | + with self.assertRaises(PyperclipException): |
| 112 | + self.copy(None) |
| 113 | + |
| 114 | + with self.assertRaises(PyperclipException): |
| 115 | + self.copy([2, 4, 6, 8]) |
| 116 | + |
| 117 | + |
| 118 | +class TestCygwin(_TestClipboard): |
| 119 | + if "cygwin" in platform.system().lower(): |
| 120 | + clipboard = init_dev_clipboard_clipboard() |
| 121 | + |
| 122 | + |
| 123 | +class TestWindows(_TestClipboard): |
| 124 | + if os.name == "nt" or platform.system() == "Windows": |
| 125 | + clipboard = init_windows_clipboard() |
| 126 | + |
| 127 | + |
| 128 | +class TestWSL(_TestClipboard): |
| 129 | + if platform.system() == "Linux": |
| 130 | + with open("/proc/version", "r") as f: |
| 131 | + if "Microsoft" in f.read(): |
| 132 | + clipboard = init_wsl_clipboard() |
| 133 | + |
| 134 | + |
| 135 | +class TestOSX(_TestClipboard): |
| 136 | + if os.name == "mac" or platform.system() == "Darwin": |
| 137 | + try: |
| 138 | + import Foundation # check if pyobjc is installed |
| 139 | + import AppKit |
| 140 | + except ImportError: |
| 141 | + clipboard = init_osx_pbcopy_clipboard() # TODO |
| 142 | + else: |
| 143 | + clipboard = init_osx_pyobjc_clipboard() |
| 144 | + |
| 145 | + |
| 146 | +class TestQt(_TestClipboard): |
| 147 | + if HAS_DISPLAY: |
| 148 | + try: |
| 149 | + import PyQt5 |
| 150 | + except ImportError: |
| 151 | + try: |
| 152 | + import PyQt4 |
| 153 | + except ImportError: |
| 154 | + pass |
| 155 | + else: |
| 156 | + clipboard = init_qt_clipboard() |
| 157 | + else: |
| 158 | + clipboard = init_qt_clipboard() |
| 159 | + |
| 160 | + |
| 161 | +class TestXClip(_TestClipboard): |
| 162 | + if _executable_exists("xclip"): |
| 163 | + clipboard = init_xclip_clipboard() |
| 164 | + |
| 165 | + |
| 166 | +class TestXSel(_TestClipboard): |
| 167 | + if _executable_exists("xsel"): |
| 168 | + clipboard = init_xsel_clipboard() |
| 169 | + |
| 170 | + |
| 171 | +class TestKlipper(_TestClipboard): |
| 172 | + if _executable_exists("klipper") and _executable_exists("qdbus"): |
| 173 | + clipboard = init_klipper_clipboard() |
| 174 | + |
| 175 | + |
| 176 | +class TestNoClipboard(unittest.TestCase): |
| 177 | + copy, paste = init_no_clipboard() |
| 178 | + |
| 179 | + def test_copy(self): |
| 180 | + with self.assertRaises(RuntimeError): |
| 181 | + self.copy("foo") |
| 182 | + |
| 183 | + def test_paste(self): |
| 184 | + with self.assertRaises(RuntimeError): |
| 185 | + self.paste() |
| 186 | + |
| 187 | + |
| 188 | +if __name__ == "__main__": |
| 189 | + unittest.main() |
0 commit comments