Variant of `Responder` which is capable of detecting incorrect responses. This class adds a ``sentinel`` parameter to ``__init__``, and its ``submit`` will raise `.ResponseNotAccepted` if it detects that sentinel value in the stream. .. versionadded:: 1.0
| 111 | |
| 112 | |
| 113 | class FailingResponder(Responder): |
| 114 | """ |
| 115 | Variant of `Responder` which is capable of detecting incorrect responses. |
| 116 | |
| 117 | This class adds a ``sentinel`` parameter to ``__init__``, and its |
| 118 | ``submit`` will raise `.ResponseNotAccepted` if it detects that sentinel |
| 119 | value in the stream. |
| 120 | |
| 121 | .. versionadded:: 1.0 |
| 122 | """ |
| 123 | |
| 124 | def __init__(self, pattern: str, response: str, sentinel: str) -> None: |
| 125 | super().__init__(pattern, response) |
| 126 | self.sentinel = sentinel |
| 127 | self.failure_index = 0 |
| 128 | self.tried = False |
| 129 | |
| 130 | def submit(self, stream: str) -> Generator[str, None, None]: |
| 131 | # Behave like regular Responder initially |
| 132 | response = super().submit(stream) |
| 133 | # Also check stream for our failure sentinel |
| 134 | failed = self.pattern_matches(stream, self.sentinel, "failure_index") |
| 135 | # Error out if we seem to have failed after a previous response. |
| 136 | if self.tried and failed: |
| 137 | err = 'Auto-response to r"{}" failed with {!r}!'.format( |
| 138 | self.pattern, self.sentinel |
| 139 | ) |
| 140 | raise ResponseNotAccepted(err) |
| 141 | # Once we see that we had a response, take note |
| 142 | if response: |
| 143 | self.tried = True |
| 144 | # Again, behave regularly by default. |
| 145 | return response |
no outgoing calls
no test coverage detected
searching dependent graphs…