A single doctest example, consisting of source code and expected output. `Example` defines the following attributes: - source: A single Python statement, always ending with a newline. The constructor adds a newline if needed. - want: The expected output from running t
| 456 | ## includes information about where the string was extracted from. |
| 457 | |
| 458 | class Example: |
| 459 | """ |
| 460 | A single doctest example, consisting of source code and expected |
| 461 | output. `Example` defines the following attributes: |
| 462 | |
| 463 | - source: A single Python statement, always ending with a newline. |
| 464 | The constructor adds a newline if needed. |
| 465 | |
| 466 | - want: The expected output from running the source code (either |
| 467 | from stdout, or a traceback in case of exception). `want` ends |
| 468 | with a newline unless it's empty, in which case it's an empty |
| 469 | string. The constructor adds a newline if needed. |
| 470 | |
| 471 | - exc_msg: The exception message generated by the example, if |
| 472 | the example is expected to generate an exception; or `None` if |
| 473 | it is not expected to generate an exception. This exception |
| 474 | message is compared against the return value of |
| 475 | `traceback.format_exception_only()`. `exc_msg` ends with a |
| 476 | newline unless it's `None`. The constructor adds a newline |
| 477 | if needed. |
| 478 | |
| 479 | - lineno: The line number within the DocTest string containing |
| 480 | this Example where the Example begins. This line number is |
| 481 | zero-based, with respect to the beginning of the DocTest. |
| 482 | |
| 483 | - indent: The example's indentation in the DocTest string. |
| 484 | I.e., the number of space characters that precede the |
| 485 | example's first prompt. |
| 486 | |
| 487 | - options: A dictionary mapping from option flags to True or |
| 488 | False, which is used to override default options for this |
| 489 | example. Any option flags not contained in this dictionary |
| 490 | are left at their default value (as specified by the |
| 491 | DocTestRunner's optionflags). By default, no options are set. |
| 492 | """ |
| 493 | def __init__(self, source, want, exc_msg=None, lineno=0, indent=0, |
| 494 | options=None): |
| 495 | # Normalize inputs. |
| 496 | if not source.endswith('\n'): |
| 497 | source += '\n' |
| 498 | if want and not want.endswith('\n'): |
| 499 | want += '\n' |
| 500 | if exc_msg is not None and not exc_msg.endswith('\n'): |
| 501 | exc_msg += '\n' |
| 502 | # Store properties. |
| 503 | self.source = source |
| 504 | self.want = want |
| 505 | self.lineno = lineno |
| 506 | self.indent = indent |
| 507 | if options is None: options = {} |
| 508 | self.options = options |
| 509 | self.exc_msg = exc_msg |
| 510 | |
| 511 | def __eq__(self, other): |
| 512 | if type(self) is not type(other): |
| 513 | return NotImplemented |
| 514 | |
| 515 | return self.source == other.source and \ |