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