Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mockdoctest.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. # this module is a trivial class with doctests to test trial's doctest
  4. # support.
  5. class Counter:
  6. """a simple counter object for testing trial's doctest support
  7. >>> c = Counter()
  8. >>> c.value()
  9. 0
  10. >>> c += 3
  11. >>> c.value()
  12. 3
  13. >>> c.incr()
  14. >>> c.value() == 4
  15. True
  16. >>> c == 4
  17. True
  18. >>> c != 9
  19. True
  20. """
  21. _count = 0
  22. def __init__(self, initialValue=0, maxval=None):
  23. self._count = initialValue
  24. self.maxval = maxval
  25. def __iadd__(self, other):
  26. """add other to my value and return self
  27. >>> c = Counter(100)
  28. >>> c += 333
  29. >>> c == 433
  30. True
  31. """
  32. if self.maxval is not None and ((self._count + other) > self.maxval):
  33. raise ValueError("sorry, counter got too big")
  34. else:
  35. self._count += other
  36. return self
  37. def __eq__(self, other: object) -> bool:
  38. """equality operator, compare other to my value()
  39. >>> c = Counter()
  40. >>> c == 0
  41. True
  42. >>> c += 10
  43. >>> c.incr()
  44. >>> c == 10 # fail this test on purpose
  45. True
  46. """
  47. return self._count == other
  48. def __ne__(self, other: object) -> bool:
  49. """inequality operator
  50. >>> c = Counter()
  51. >>> c != 10
  52. True
  53. """
  54. return not self.__eq__(other)
  55. def incr(self):
  56. """increment my value by 1
  57. >>> from twisted.trial.test.mockdoctest import Counter
  58. >>> c = Counter(10, 11)
  59. >>> c.incr()
  60. >>> c.value() == 11
  61. True
  62. >>> c.incr()
  63. Traceback (most recent call last):
  64. File "<stdin>", line 1, in ?
  65. File "twisted/trial/test/mockdoctest.py", line 51, in incr
  66. self.__iadd__(1)
  67. File "twisted/trial/test/mockdoctest.py", line 39, in __iadd__
  68. raise ValueError, "sorry, counter got too big"
  69. ValueError: sorry, counter got too big
  70. """
  71. self.__iadd__(1)
  72. def value(self):
  73. """return this counter's value
  74. >>> c = Counter(555)
  75. >>> c.value() == 555
  76. True
  77. """
  78. return self._count
  79. def unexpectedException(self):
  80. """i will raise an unexpected exception...
  81. ... *CAUSE THAT'S THE KINDA GUY I AM*
  82. >>> 1/0
  83. """