MCPcopy
hub / github.com/pytest-dev/pytest / LiteralsOutputChecker

Class LiteralsOutputChecker

src/_pytest/doctest.py:578–657  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

576 import doctest
577
578 class LiteralsOutputChecker(doctest.OutputChecker):
579 # Based on doctest_nose_plugin.py from the nltk project
580 # (https://github.com/nltk/nltk) and on the "numtest" doctest extension
581 # by Sebastien Boisgerault (https://github.com/boisgera/numtest).
582
583 _unicode_literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)
584 _bytes_literal_re = re.compile(r"(\W|^)[bB]([rR]?[\'\"])", re.UNICODE)
585 _number_re = re.compile(
586 r"""
587 (?P<number>
588 (?P<mantissa>
589 (?P<integer1> [+-]?\d*)\.(?P<fraction>\d+)
590 |
591 (?P<integer2> [+-]?\d+)\.
592 )
593 (?:
594 [Ee]
595 (?P<exponent1> [+-]?\d+)
596 )?
597 |
598 (?P<integer3> [+-]?\d+)
599 (?:
600 [Ee]
601 (?P<exponent2> [+-]?\d+)
602 )
603 )
604 """,
605 re.VERBOSE,
606 )
607
608 def check_output(self, want: str, got: str, optionflags: int) -> bool:
609 if super().check_output(want, got, optionflags):
610 return True
611
612 allow_unicode = optionflags & _get_allow_unicode_flag()
613 allow_bytes = optionflags & _get_allow_bytes_flag()
614 allow_number = optionflags & _get_number_flag()
615
616 if not allow_unicode and not allow_bytes and not allow_number:
617 return False
618
619 def remove_prefixes(regex: re.Pattern[str], txt: str) -> str:
620 return re.sub(regex, r"\1\2", txt)
621
622 if allow_unicode:
623 want = remove_prefixes(self._unicode_literal_re, want)
624 got = remove_prefixes(self._unicode_literal_re, got)
625
626 if allow_bytes:
627 want = remove_prefixes(self._bytes_literal_re, want)
628 got = remove_prefixes(self._bytes_literal_re, got)
629
630 if allow_number:
631 got = self._remove_unwanted_precision(want, got)
632
633 return super().check_output(want, got, optionflags)
634
635 def _remove_unwanted_precision(self, want: str, got: str) -> str:

Callers

nothing calls this directly

Calls 1

compileMethod · 0.80

Tested by

no test coverage detected