<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

# this module is a trivial class with doctests to test trial's doctest
# support.


class Counter:
    """a simple counter object for testing trial's doctest support

    &gt;&gt;&gt; c = Counter()
    &gt;&gt;&gt; c.value()
    0
    &gt;&gt;&gt; c += 3
    &gt;&gt;&gt; c.value()
    3
    &gt;&gt;&gt; c.incr()
    &gt;&gt;&gt; c.value() == 4
    True
    &gt;&gt;&gt; c == 4
    True
    &gt;&gt;&gt; c != 9
    True

    """

    _count = 0

    def __init__(self, initialValue=0, maxval=None):
        self._count = initialValue
        self.maxval = maxval

    def __iadd__(self, other):
        """add other to my value and return self

        &gt;&gt;&gt; c = Counter(100)
        &gt;&gt;&gt; c += 333
        &gt;&gt;&gt; c == 433
        True
        """
        if self.maxval is not None and ((self._count + other) &gt; self.maxval):
            raise ValueError("sorry, counter got too big")
        else:
            self._count += other
        return self

    def __eq__(self, other: object) -&gt; bool:
        """equality operator, compare other to my value()

        &gt;&gt;&gt; c = Counter()
        &gt;&gt;&gt; c == 0
        True
        &gt;&gt;&gt; c += 10
        &gt;&gt;&gt; c.incr()
        &gt;&gt;&gt; c == 10   # fail this test on purpose
        True

        """
        return self._count == other

    def __ne__(self, other: object) -&gt; bool:
        """inequality operator

        &gt;&gt;&gt; c = Counter()
        &gt;&gt;&gt; c != 10
        True
        """
        return not self.__eq__(other)

    def incr(self):
        """increment my value by 1

        &gt;&gt;&gt; from twisted.trial.test.mockdoctest import Counter
        &gt;&gt;&gt; c = Counter(10, 11)
        &gt;&gt;&gt; c.incr()
        &gt;&gt;&gt; c.value() == 11
        True
        &gt;&gt;&gt; c.incr()
        Traceback (most recent call last):
          File "&lt;stdin&gt;", line 1, in ?
          File "twisted/trial/test/mockdoctest.py", line 51, in incr
            self.__iadd__(1)
          File "twisted/trial/test/mockdoctest.py", line 39, in __iadd__
            raise ValueError, "sorry, counter got too big"
        ValueError: sorry, counter got too big
        """
        self.__iadd__(1)

    def value(self):
        """return this counter's value

        &gt;&gt;&gt; c = Counter(555)
        &gt;&gt;&gt; c.value() == 555
        True
        """
        return self._count

    def unexpectedException(self):
        """i will raise an unexpected exception...
        ... *CAUSE THAT'S THE KINDA GUY I AM*

              &gt;&gt;&gt; 1/0
        """
</pre></body></html>