PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/Tests/test_property.py

#
Python | 243 lines | 153 code | 58 blank | 32 comment | 9 complexity | 2aca4b542d9803a825d8d22a1a8ff059 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. from iptest.assert_util import *
  16. array_list_options = []
  17. if is_cli or is_silverlight:
  18. array_list_options.append(System.Collections.Generic.List[int])
  19. if not is_silverlight:
  20. array_list_options.append(System.Collections.ArrayList)
  21. for ArrayList in array_list_options:
  22. l = ArrayList()
  23. index = l.Add(22)
  24. Assert(l[0] == 22)
  25. l[0] = 33
  26. Assert(l[0] == 33)
  27. import sys
  28. Assert('<stdin>' in str(sys.stdin))
  29. #Assert('<stdout>' in str(sys.stdout))
  30. #Assert('<stderr>' in str(sys.stderr))
  31. # getting a property from a class should return the property,
  32. # getting it from an instance should do the descriptor check
  33. def test_sanity():
  34. class foo(object):
  35. def myset(self, value): pass
  36. def myget(self): return "hello"
  37. prop = property(fget=myget,fset=myset)
  38. AreEqual(type(foo.prop), property)
  39. a = foo()
  40. AreEqual(a.prop, 'hello')
  41. @skip("win32")
  42. def test_builtinclrtype_set():
  43. # setting an instance property on a built-in type should
  44. # throw that you can't set on built-in types
  45. for ArrayList in array_list_options:
  46. def setCount():
  47. ArrayList.Count = 23
  48. AssertError(AttributeError, setCount)
  49. # System.String.Empty is a read-only static field
  50. AssertError(AttributeError, setattr, System.String, "Empty", "foo")
  51. # a class w/ a metaclass that has a property
  52. # defined should hit the descriptor when getting
  53. # it on the class.
  54. def test_metaclass():
  55. class MyType(type):
  56. def myget(self): return 'hello'
  57. aaa = property(fget=myget)
  58. class foo(object):
  59. __metaclass__ = MyType
  60. AreEqual(foo.aaa, 'hello')
  61. def test_reflected_property():
  62. # ReflectedProperty tests
  63. for ArrayList in array_list_options:
  64. alist = ArrayList()
  65. AreEqual(ArrayList.Count.__set__(None, 5), None)
  66. AssertError(TypeError, ArrayList.Count, alist, 5)
  67. AreEqual(alist.Count, 0)
  68. AreEqual(str(ArrayList.__dict__['Count']), '<property# Count on %s>' % ArrayList.__name__)
  69. def tryDelReflectedProp():
  70. del ArrayList.Count
  71. AssertError(AttributeError, tryDelReflectedProp)
  72. @skip("win32")
  73. def test_reflected_extension_property_ops():
  74. '''
  75. Test to hit IronPython.RunTime.Operations.ReflectedExtensionPropertyOps
  76. '''
  77. t_list = [ (complex.__dict__['real'], 'complex', 'float', 'real'),
  78. (complex.__dict__['imag'], 'complex', 'float', 'imag'),
  79. ]
  80. for stuff, typename, returnType, propName in t_list:
  81. expected = "Get: " + propName + "(self: " + typename + ") -> " + returnType + newline
  82. Assert(stuff.__doc__.startswith(expected), stuff.__doc__)
  83. def test_class_doc():
  84. AreEqual(object.__dict__['__class__'].__doc__, "the object's class")
  85. def test_prop_doc_only():
  86. # define a property w/ only the doc
  87. x = property(None, None, doc = 'Holliday')
  88. AreEqual(x.fget, None)
  89. AreEqual(x.fset, None)
  90. AreEqual(x.fdel, None)
  91. AreEqual(x.__doc__, 'Holliday')
  92. def test_member_lookup_oldclass():
  93. class OldC:
  94. xprop = property(lambda self: self._xprop)
  95. def __init__(self):
  96. self._xprop = 42
  97. self.xmember = 42
  98. c = OldC()
  99. c.__dict__['xprop'] = 43
  100. c.__dict__['xmember'] = 43
  101. AreEqual(c.xprop, 43)
  102. AreEqual(c.xmember, 43)
  103. c.xprop = 41
  104. c.xmember = 41
  105. AreEqual(c.xprop, 41)
  106. AreEqual(c.xmember, 41)
  107. AreEqual(c.__dict__['xprop'], 41)
  108. AreEqual(c.__dict__['xmember'], 41)
  109. def test_member_lookup_newclass():
  110. class NewC(object):
  111. def xprop_setter(self, xprop):
  112. self._xprop = xprop
  113. xprop = property(lambda self: self._xprop,
  114. xprop_setter)
  115. def __init__(self):
  116. self._xprop = 42
  117. self.xmember = 42
  118. c = NewC()
  119. c.__dict__['xprop'] = 43
  120. c.__dict__['xmember'] = 43
  121. AreEqual(c.xprop, 42)
  122. AreEqual(c.xmember, 43)
  123. c.xprop = 41
  124. c.xmember = 41
  125. AreEqual(c.xprop, 41)
  126. AreEqual(c.xmember, 41)
  127. AreEqual(c.__dict__['xprop'], 43)
  128. AreEqual(c.__dict__['xmember'], 41)
  129. def test_inheritance():
  130. class MyProperty(property):
  131. def __init__(self, *args):
  132. property.__init__(self, *args)
  133. x = MyProperty(1,2,3)
  134. AreEqual(x.fget, 1)
  135. AreEqual(x.fset, 2)
  136. AreEqual(x.fdel, 3)
  137. class MyProperty(property):
  138. def __init__(self, *args):
  139. property.__init__(self, *args)
  140. def __get__(self, *args):
  141. return 42
  142. def __set__(self, inst, value):
  143. inst.foo = value
  144. def __delete__(self, *args):
  145. inst.bar = 'baz'
  146. class MyClass(object):
  147. x = MyProperty()
  148. inst = MyClass()
  149. AreEqual(inst.x, 42)
  150. inst.x = 'abc'
  151. AreEqual(inst.foo, 'abc')
  152. del inst.x
  153. AreEqual(inst.bar, 'baz')
  154. def test_property_mutation():
  155. class x(object): pass
  156. prop = property()
  157. x.foo = prop
  158. inst = x()
  159. for i in xrange(42):
  160. prop.__init__(lambda self: i)
  161. AreEqual(inst.foo, i)
  162. def test_property_doc():
  163. def getter(self):
  164. """getter doc"""
  165. AreEqual(property(getter).__doc__, "getter doc")
  166. AreEqual(property(None).__doc__, None)
  167. AreEqual(property(None, getter, getter).__doc__, None)
  168. Assert(type(property.__doc__) is str)
  169. def assignerror():
  170. property.__doc__ = None
  171. AssertErrorWithMessage(TypeError, "can't set attributes of built-in/extension type 'property'", assignerror)
  172. def test_class_assign():
  173. """assigning to a property through the class should replace the
  174. property in the class dictionary"""
  175. class x(object):
  176. def set(self, value):
  177. AssertUnreachable()
  178. prop = property(lambda x:42, set)
  179. x.prop = 42
  180. AreEqual(x.__dict__['prop'], 42)
  181. def test_assign():
  182. x = property()
  183. for attr in ['__doc__', 'fdel', 'fget', 'fset']:
  184. AssertErrorWithMessage(TypeError, "readonly attribute", lambda : setattr(x, attr, 'abc'))
  185. run_test(__name__)