1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import unittest
from pickle import dumps
from pyface.qt import QtCore
from ..mimedata import PyMimeData, str2bytes
class PMDSubclass(PyMimeData):
pass
class PyMimeDataTestCase(unittest.TestCase):
# Basic functionality tests
def test_pickle(self):
md = PyMimeData(data=0)
self.assertEqual(md._local_instance, 0)
self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertEqual(
md.data(PyMimeData.MIME_TYPE).data(), dumps(int) + dumps(0)
)
def test_nopickle(self):
md = PyMimeData(data=0, pickle=False)
self.assertEqual(md._local_instance, 0)
self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertEqual(
md.data(PyMimeData.NOPICKLE_MIME_TYPE).data(),
str2bytes(str(id(0))),
)
def test_cant_pickle(self):
unpicklable = lambda: None
md = PyMimeData(data=unpicklable)
self.assertEqual(md._local_instance, unpicklable)
self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertEqual(
md.data(PyMimeData.NOPICKLE_MIME_TYPE).data(),
str2bytes(str(id(unpicklable))),
)
def test_coerce_pymimedata(self):
md = PyMimeData(data=0)
md2 = PyMimeData.coerce(md)
self.assertEqual(md, md2)
def test_coerce_subclass(self):
md = PMDSubclass(data=0)
md2 = PyMimeData.coerce(md)
self.assertEqual(md, md2)
def test_coerce_QMimeData(self):
md = QtCore.QMimeData()
md.setText("test")
md2 = PyMimeData.coerce(md)
self.assertTrue(md2.hasText())
self.assertEqual(md2.text(), "test")
def test_coerce_object(self):
md = PyMimeData.coerce(0)
self.assertEqual(md._local_instance, 0)
self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertEqual(
md.data(PyMimeData.MIME_TYPE).data(), dumps(int) + dumps(0)
)
def test_coerce_unpicklable(self):
unpicklable = lambda: None
md = PyMimeData.coerce(unpicklable)
self.assertEqual(md._local_instance, unpicklable)
self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
def test_coerce_list(self):
md = PyMimeData.coerce([0])
self.assertEqual(md._local_instance, [0])
self.assertTrue(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertFalse(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertEqual(
md.data(PyMimeData.MIME_TYPE).data(), dumps(list) + dumps([0])
)
def test_coerce_list_pymimedata(self):
md = PyMimeData(data=0)
md2 = PyMimeData.coerce([md])
self.assertEqual(md2._local_instance, [0])
self.assertTrue(md2.hasFormat(PyMimeData.MIME_TYPE))
self.assertFalse(md2.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertEqual(
md2.data(PyMimeData.MIME_TYPE).data(), dumps(list) + dumps([0])
)
def test_coerce_list_pymimedata_nopickle(self):
md = PyMimeData(data=0, pickle=False)
md2 = PyMimeData.coerce([md])
self.assertEqual(md2._local_instance, [0])
self.assertFalse(md2.hasFormat(PyMimeData.MIME_TYPE))
self.assertTrue(md2.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
def test_coerce_list_pymimedata_mixed(self):
md1 = PyMimeData(data=0, pickle=False)
md2 = PyMimeData(data=0)
md = PyMimeData.coerce([md1, md2])
self.assertEqual(md._local_instance, [0, 0])
self.assertFalse(md.hasFormat(PyMimeData.MIME_TYPE))
self.assertTrue(md.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
def test_subclass_coerce_pymimedata(self):
md = PyMimeData(data=0)
md2 = PMDSubclass.coerce(md)
self.assertTrue(isinstance(md2, PMDSubclass))
self.assertTrue(md2.hasFormat(PyMimeData.MIME_TYPE))
self.assertFalse(md2.hasFormat(PyMimeData.NOPICKLE_MIME_TYPE))
self.assertEqual(
md2.data(PyMimeData.MIME_TYPE).data(), dumps(int) + dumps(0)
)
def test_instance(self):
md = PyMimeData(data=0)
self.assertEqual(md.instance(), 0)
def test_instance_unpickled(self):
md = PyMimeData(data=0)
# remove local instance to simulate cross-process
md._local_instance = None
self.assertEqual(md.instance(), 0)
def test_instance_nopickle(self):
md = PyMimeData(data=0, pickle=False)
# remove local instance to simulate cross-process
md._local_instance = None
self.assertEqual(md.instance(), None)
def test_instance_type(self):
md = PyMimeData(data=0)
self.assertEqual(md.instanceType(), int)
def test_instance_type_unpickled(self):
md = PyMimeData(data=0)
# remove local instance to simulate cross-process
md._local_instance = None
self.assertEqual(md.instanceType(), int)
def test_instance_type_nopickle(self):
md = PyMimeData(data=0, pickle=False)
# remove local instance to simulate cross-process
md._local_instance = None
self.assertEqual(md.instanceType(), None)
|