我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用doctest.OutputChecker()。
def check_output(self, want, got, optionflags): alt_self = getattr(self, '_temp_override_self', None) if alt_self is not None: super_method = self._temp_call_super_check_output self = alt_self else: super_method = OutputChecker.check_output parser = self.get_parser(want, got, optionflags) if not parser: return super_method( self, want, got, optionflags) try: want_doc = parser(want) except etree.XMLSyntaxError: return False try: got_doc = parser(got) except etree.XMLSyntaxError: return False return self.compare_docs(want_doc, got_doc)
def check_output(self, want, got, optionflags): ret = doctest.OutputChecker.check_output(self, want, got, optionflags) if not ret: if "#random" in want: return True # it would be useful to normalize endianness so that # bigendian machines don't fail all the tests (and there are # actually some bigendian examples in the doctests). Let's try # making them all little endian got = got.replace("'>", "'<") want = want.replace("'>", "'<") # try to normalize out 32 and 64 bit default int sizes for sz in [4, 8]: got = got.replace("'<i%d'" % sz, "int") want = want.replace("'<i%d'" % sz, "int") ret = doctest.OutputChecker.check_output(self, want, got, optionflags) return ret # Subclass nose.plugins.doctests.DocTestCase to work around a bug in # its constructor that blocks non-default arguments from being passed # down into doctest.DocTestCase
def output_difference(self, example, got, optionflags): want = example.want parser = self.get_parser(want, got, optionflags) errors = [] if parser is not None: try: want_doc = parser(want) except etree.XMLSyntaxError: e = sys.exc_info()[1] errors.append('In example: %s' % e) try: got_doc = parser(got) except etree.XMLSyntaxError: e = sys.exc_info()[1] errors.append('In actual output: %s' % e) if parser is None or errors: value = OutputChecker.output_difference( self, example, got, optionflags) if errors: errors.append(value) return '\n'.join(errors) else: return value html = parser is html_fromstring diff_parts = [] diff_parts.append('Expected:') diff_parts.append(self.format_doc(want_doc, html, 2)) diff_parts.append('Got:') diff_parts.append(self.format_doc(got_doc, html, 2)) diff_parts.append('Diff:') diff_parts.append(self.collect_diff(want_doc, got_doc, html, 2)) return '\n'.join(diff_parts)
def install(html=False): """ Install doctestcompare for all future doctests. If html is true, then by default the HTML parser will be used; otherwise the XML parser is used. """ if html: doctest.OutputChecker = LHTMLOutputChecker else: doctest.OutputChecker = LXMLOutputChecker
def check_output(self, want, got, optionflags): """Check output, accepting special markers embedded in the output. If the output didn't pass the default validation but the special string '#random' is included, we accept it.""" # Let the original tester verify first, in case people have valid tests # that happen to have a comment saying '#random' embedded in. ret = doctest.OutputChecker.check_output(self, want, got, optionflags) if not ret and self.random_re.search(want): #print >> sys.stderr, 'RANDOM OK:',want # dbg return True return ret
def check_output(self, want, got, optionflags): res = doctest.OutputChecker.check_output(self, want, got, optionflags) if res: return True if not (optionflags & ALLOW_UNICODE): return False # ALLOW_UNICODE is active and want != got cleaned_want = self._remove_u_prefixes(want) cleaned_got = self._remove_u_prefixes(got) res = doctest.OutputChecker.check_output(self, cleaned_want, cleaned_got, optionflags) return res
def check_output(self, want, got, optionflags): if not PY2: #Differences between unicode strings representations : u"foo" -> "foo" want = re.sub("u'(.*?)'", "'\\1'", want) want = re.sub('u"(.*?)"', '"\\1"', want) #NameError message has changed want = want.replace('NameError: global name', 'NameError: name') else: want = re.sub("^b'(.*?)'", "'\\1'", want) want = re.sub('^b"(.*?)"', '"\\1"', want) return doctest.OutputChecker.check_output(self, want, got, optionflags)
def check_output(self, want, got, optionflags): if six.PY2: if want == "12\n": # write() doesn't return number of bytes on PY2 want = "" want = re.sub("PermissionError: (.*)", "OSError: \\1", want) want = re.sub("'0o(.+)'\n", "'0\\1'\n", want) want = re.sub("b'(.*?)'", "'\\1'", want) got = re.sub("u'(.*?)'", "'\\1'", got) return OutputChecker.check_output(self, want, got, optionflags)
def check_output(self, want, got, optionflags): # u'...' -> '...'; u"..." -> "..." got = re.sub(r'''\bu('[^']*'|"[^"]*")''', r'\1', got) return doctest.OutputChecker.check_output(self, want, got, optionflags)