def test_tableiter(self): bindata = [] text = 'IŋƫƐrnætiønæl' text_a = bytestr(text) + b'\0\1\2\n' text_b = bytestr(r'\x41\u0042\U000043') for i in range(256): fragments = [bytestr('%0x %d ' % (i, i))] h = int(i / 256) l = i % 256 if h: fragments.append(whatstyle.int2byte(h)) fragments.append(whatstyle.int2byte(l)) data = b''.join(fragments) udata = data.decode('utf-8', 'replace') ulen = whatstyle.unilen(udata) data = b' ' * (16 - ulen) + data + b'\n' bindata.append(data) text_b += b''.join(bindata) try: bodies = [] for tablestart, tbody, tableend in whatstyle.table_iter( [(text_a, text_b)], 1, fromdesc='', todesc=''): bodies.append(tbody) html = '\n'.join(bodies) buf = BytesIO() html = '<html><body><table>' + html + '</table></body></html>' whatstyle.htmldiff2ansi(html, enc='utf-8', fp=buf) ansi = buf.getvalue() # whatstyle.outline(ansi) self.assertEqual(len(re.findall(bytestr(text) + b'\0\1\2', ansi)), 1) except whatstyle.ETree.ParseError: raise
def test_builtin_diff(self): source = "a\nb\nc\n" content2 = bytestr("a\ne\nc\n") bn = "whatstyle_diff_" filename = tmpfile_with_data(source, suffix=bn) try: code, diff = whatstyle.compute_unified_diff(filename, content2) diffdata = ''.join(diff) diffdata = bytestr(diffdata) metric = whatstyle.metric_for_mindiff(diffdata) self.assertEqual(metric, 4) finally: os.remove(filename)
def test_quoting(self): a = bytestr(r'\x41\u0042\U000043') b = whatstyle.surrdecode(a) b = whatstyle.escape_illegal_xmlchars(b) b = whatstyle.unescape_illegal_xmlchars(b) b = whatstyle.surrencode(b) self.assertEqual(a, b)
def files_equal(refdir, testfiles): diffs = [] for f in testfiles: base = basename(f) reffile = join(refdir, base) if not os.path.exists(reffile): diffs.append("# Reference file %s does not exist" % reffile) continue if not os.path.exists(f): diffs.append("# Result file %s does not exist" % reffile) continue with open(f, 'rb') as fp: data = fp.read() exit_code, diff = whatstyle.compute_unified_diff(reffile, data, fromfile=reffile, tofile=f, n=2) diff = list(diff) if not diff: continue metricfile = whatstyle.metricfile(reffile) if metricfile == reffile: with open(reffile, 'rb') as fp: data = bytestr(data) refdata = fp.read() commentpos = refdata.find(b'#') if commentpos >= 0 and data[:commentpos] == refdata[:commentpos]: # Only the formatter name or version is different and that # is no reason to fail the test. continue diffs.extend(diff) return diffs
def test_convert(self): lines = self.source.splitlines() data = os.linesep.join(lines) + os.linesep filename = tmpfile_with_data(data, suffix='.py', prefix='whatstyle_') try: ret, outtxt, errtxt = run_args( ['--quiet', '--color=off', '--stderr', filename]) self.assertEqual(ret, 0) self.assertEqual(outtxt, bytestr(self.expected)) finally: os.remove(filename)
def test_pathological_diffs(self): """Assure that a diff of a file with a different indentation or a few added lines has a better diff metric than a file that is basically empty. """ difftool = whatstyle.BUILTIN_DIFF origlines = [] newlines = [] # The original file has 200 lines with numbers and a 4 space indent. for i in range(1, 201): origlines.append(' %d' % i) # The file to compare with has a 2 space indent. newlines.append(' %d' % i) source = '\n'.join(origlines) + '\n' mod1 = '\n'.join(newlines) + '\n' mod2 = mod1 + '\n\n\n\n\n\n\n\n' mod3 = '' mod4 = '\n' fd, tmpfilename = tempfile.mkstemp(suffix='.c', prefix='whatstyle_test_') os.write(fd, bytestr(source)) os.close(fd) try: dists = whatstyle.distances_from_diffs(difftool, [(tmpfilename, mod1), (tmpfilename, mod2), (tmpfilename, mod3), (tmpfilename, mod4)]) dists = list(dists) self.assertLess(dists[0], dists[2]) self.assertLess(dists[0], dists[3]) self.assertLess(dists[1], dists[2]) self.assertLess(dists[1], dists[3]) finally: os.remove(tmpfilename)
def tmpfile_with_data(data, **kwargs): fd, filename = tempfile.mkstemp(**kwargs) os.write(fd, bytestr(data)) os.close(fd) return filename