/Doc/includes/test.py

http://unladen-swallow.googlecode.com/ · Python · 213 lines · 194 code · 5 blank · 14 comment · 0 complexity · d37dc1dfd925dc025b4c5e16679d8b8e MD5 · raw file

  1. """Test module for the noddy examples
  2. Noddy 1:
  3. >>> import noddy
  4. >>> n1 = noddy.Noddy()
  5. >>> n2 = noddy.Noddy()
  6. >>> del n1
  7. >>> del n2
  8. Noddy 2
  9. >>> import noddy2
  10. >>> n1 = noddy2.Noddy('jim', 'fulton', 42)
  11. >>> n1.first
  12. 'jim'
  13. >>> n1.last
  14. 'fulton'
  15. >>> n1.number
  16. 42
  17. >>> n1.name()
  18. 'jim fulton'
  19. >>> n1.first = 'will'
  20. >>> n1.name()
  21. 'will fulton'
  22. >>> n1.last = 'tell'
  23. >>> n1.name()
  24. 'will tell'
  25. >>> del n1.first
  26. >>> n1.name()
  27. Traceback (most recent call last):
  28. ...
  29. AttributeError: first
  30. >>> n1.first
  31. Traceback (most recent call last):
  32. ...
  33. AttributeError: first
  34. >>> n1.first = 'drew'
  35. >>> n1.first
  36. 'drew'
  37. >>> del n1.number
  38. Traceback (most recent call last):
  39. ...
  40. TypeError: can't delete numeric/char attribute
  41. >>> n1.number=2
  42. >>> n1.number
  43. 2
  44. >>> n1.first = 42
  45. >>> n1.name()
  46. '42 tell'
  47. >>> n2 = noddy2.Noddy()
  48. >>> n2.name()
  49. ' '
  50. >>> n2.first
  51. ''
  52. >>> n2.last
  53. ''
  54. >>> del n2.first
  55. >>> n2.first
  56. Traceback (most recent call last):
  57. ...
  58. AttributeError: first
  59. >>> n2.first
  60. Traceback (most recent call last):
  61. ...
  62. AttributeError: first
  63. >>> n2.name()
  64. Traceback (most recent call last):
  65. File "<stdin>", line 1, in ?
  66. AttributeError: first
  67. >>> n2.number
  68. 0
  69. >>> n3 = noddy2.Noddy('jim', 'fulton', 'waaa')
  70. Traceback (most recent call last):
  71. File "<stdin>", line 1, in ?
  72. TypeError: an integer is required
  73. >>> del n1
  74. >>> del n2
  75. Noddy 3
  76. >>> import noddy3
  77. >>> n1 = noddy3.Noddy('jim', 'fulton', 42)
  78. >>> n1 = noddy3.Noddy('jim', 'fulton', 42)
  79. >>> n1.name()
  80. 'jim fulton'
  81. >>> del n1.first
  82. Traceback (most recent call last):
  83. File "<stdin>", line 1, in ?
  84. TypeError: Cannot delete the first attribute
  85. >>> n1.first = 42
  86. Traceback (most recent call last):
  87. File "<stdin>", line 1, in ?
  88. TypeError: The first attribute value must be a string
  89. >>> n1.first = 'will'
  90. >>> n1.name()
  91. 'will fulton'
  92. >>> n2 = noddy3.Noddy()
  93. >>> n2 = noddy3.Noddy()
  94. >>> n2 = noddy3.Noddy()
  95. >>> n3 = noddy3.Noddy('jim', 'fulton', 'waaa')
  96. Traceback (most recent call last):
  97. File "<stdin>", line 1, in ?
  98. TypeError: an integer is required
  99. >>> del n1
  100. >>> del n2
  101. Noddy 4
  102. >>> import noddy4
  103. >>> n1 = noddy4.Noddy('jim', 'fulton', 42)
  104. >>> n1.first
  105. 'jim'
  106. >>> n1.last
  107. 'fulton'
  108. >>> n1.number
  109. 42
  110. >>> n1.name()
  111. 'jim fulton'
  112. >>> n1.first = 'will'
  113. >>> n1.name()
  114. 'will fulton'
  115. >>> n1.last = 'tell'
  116. >>> n1.name()
  117. 'will tell'
  118. >>> del n1.first
  119. >>> n1.name()
  120. Traceback (most recent call last):
  121. ...
  122. AttributeError: first
  123. >>> n1.first
  124. Traceback (most recent call last):
  125. ...
  126. AttributeError: first
  127. >>> n1.first = 'drew'
  128. >>> n1.first
  129. 'drew'
  130. >>> del n1.number
  131. Traceback (most recent call last):
  132. ...
  133. TypeError: can't delete numeric/char attribute
  134. >>> n1.number=2
  135. >>> n1.number
  136. 2
  137. >>> n1.first = 42
  138. >>> n1.name()
  139. '42 tell'
  140. >>> n2 = noddy4.Noddy()
  141. >>> n2 = noddy4.Noddy()
  142. >>> n2 = noddy4.Noddy()
  143. >>> n2 = noddy4.Noddy()
  144. >>> n2.name()
  145. ' '
  146. >>> n2.first
  147. ''
  148. >>> n2.last
  149. ''
  150. >>> del n2.first
  151. >>> n2.first
  152. Traceback (most recent call last):
  153. ...
  154. AttributeError: first
  155. >>> n2.first
  156. Traceback (most recent call last):
  157. ...
  158. AttributeError: first
  159. >>> n2.name()
  160. Traceback (most recent call last):
  161. File "<stdin>", line 1, in ?
  162. AttributeError: first
  163. >>> n2.number
  164. 0
  165. >>> n3 = noddy4.Noddy('jim', 'fulton', 'waaa')
  166. Traceback (most recent call last):
  167. File "<stdin>", line 1, in ?
  168. TypeError: an integer is required
  169. Test cyclic gc(?)
  170. >>> import gc
  171. >>> gc.disable()
  172. >>> x = []
  173. >>> l = [x]
  174. >>> n2.first = l
  175. >>> n2.first
  176. [[]]
  177. >>> l.append(n2)
  178. >>> del l
  179. >>> del n1
  180. >>> del n2
  181. >>> sys.getrefcount(x)
  182. 3
  183. >>> ignore = gc.collect()
  184. >>> sys.getrefcount(x)
  185. 2
  186. >>> gc.enable()
  187. """
  188. import os
  189. import sys
  190. from distutils.util import get_platform
  191. PLAT_SPEC = "%s-%s" % (get_platform(), sys.version[0:3])
  192. src = os.path.join("build", "lib.%s" % PLAT_SPEC)
  193. sys.path.append(src)
  194. if __name__ == "__main__":
  195. import doctest, __main__
  196. doctest.testmod(__main__)