How to use get_line_changed_regions method in Testify

Best Python code snippet using Testify_python

utils.py

Source:utils.py Github

copy

Full Screen

...9LEFT_HIGHLIGHT_CHARACTER = '<'10RIGHT_HIGHLIGHT_CHARACTER = '>'11# Borrowed from12# https://github.com/reviewboard/reviewboard/blob/master/reviewboard/diffviewer/diffutils.py13def get_line_changed_regions(oldline, newline):14 if oldline is None or newline is None:15 return (None, None)16 # Use the SequenceMatcher directly. It seems to give us better results17 # for this. We should investigate steps to move to the new differ.18 differ = SequenceMatcher(None, oldline, newline)19 # This thresholds our results -- we don't want to show inter-line diffs if20 # most of the line has changed, unless those lines are very short.21 # FIXME: just a plain, linear threshold is pretty crummy here. Short22 # changes in a short line get lost. I haven't yet thought of a fancy23 # nonlinear test.24 if differ.ratio() < 0.6:25 return (None, None)26 oldchanges = []27 newchanges = []28 back = (0, 0)29 for tag, i1, i2, j1, j2 in differ.get_opcodes():30 if tag == "equal":31 if (i2 - i1 < 3) or (j2 - j1 < 3):32 back = (j2 - j1, i2 - i1)33 continue34 oldstart, oldend = i1 - back[0], i235 newstart, newend = j1 - back[1], j236 if oldchanges != [] and oldstart <= oldchanges[-1][1] < oldend:37 oldchanges[-1] = (oldchanges[-1][0], oldend)38 elif not oldline[oldstart:oldend].isspace():39 oldchanges.append((oldstart, oldend))40 if newchanges != [] and newstart <= newchanges[-1][1] < newend:41 newchanges[-1] = (newchanges[-1][0], newend)42 elif not newline[newstart:newend].isspace():43 newchanges.append((newstart, newend))44 back = (0, 0)45 return (oldchanges, newchanges)46def highlight_regions(string, regions):47 """Given `string` and `regions` (a list of (beginning index, end index)48 tuples), return `string` marked up to highlight those regions.49 >>> highlight_regions('This is a string.', [(0, 3), (8, 9)])50 '<Thi>s is <a> string.'51 """52 string = list(string)53 # Inserting into the middle of a list shifts all the elements over by one.54 # Each time a markup element is added, increase a result string's insertion55 # offset.56 offset = 057 for beginning, end in sorted(regions or []):58 string.insert(offset + beginning, LEFT_HIGHLIGHT_CHARACTER)59 offset +=160 string.insert(offset + end, RIGHT_HIGHLIGHT_CHARACTER)61 offset +=162 return ''.join(string)63# no namedtuple in Python 2.5; here is a simple imitation64# HighlightedDiff = collections.namedtuple('HighlightedDiff', 'old new')65class HighlightedDiff(tuple):66 def __new__(cls, old, new):67 return tuple.__new__(cls, (old, new))68 __slots__ = () # no attributes allowed69 @property70 def old(self):71 return self[0]72 @property73 def new(self):74 return self[1]75 def __repr__(self):76 return '%s(%r, %r)' % (self.__class__.__name__, self.old, self.new)77def highlight(old, new):78 """Given two strings, return a `HighlightedDiff` containing the strings79 with markup identifying the parts that changed.80 >>> highlight('Testify is great.', 'testify is gr8')81 HighlightedDiff(old='<T>estify is gr<eat.>', new='<t>estify is gr<8>')82 """83 oldchanges, newchanges = get_line_changed_regions(old, new)84 return HighlightedDiff(highlight_regions(old, oldchanges),...

Full Screen

Full Screen

stringdiffer.py

Source:stringdiffer.py Github

copy

Full Screen

...6LEFT_HIGHLIGHT_CHARACTER = '<'7RIGHT_HIGHLIGHT_CHARACTER = '>'8# Borrowed from9# https://github.com/reviewboard/reviewboard/blob/master/reviewboard/diffviewer/diffutils.py10def get_line_changed_regions(oldline, newline):11 if oldline is None or newline is None:12 return (None, None)13 # Use the SequenceMatcher directly. It seems to give us better results14 # for this. We should investigate steps to move to the new differ.15 differ = SequenceMatcher(None, oldline, newline)16 # This thresholds our results -- we don't want to show inter-line diffs if17 # most of the line has changed, unless those lines are very short.18 # FIXME: just a plain, linear threshold is pretty crummy here. Short19 # changes in a short line get lost. I haven't yet thought of a fancy20 # nonlinear test.21 if differ.ratio() < 0.6:22 return (None, None)23 oldchanges = []24 newchanges = []25 back = (0, 0)26 for tag, i1, i2, j1, j2 in differ.get_opcodes():27 if tag == "equal":28 if ((i2 - i1 < 3) or (j2 - j1 < 3)) and (i1, j1) != (0, 0):29 back = (j2 - j1, i2 - i1)30 continue31 oldstart, oldend = i1 - back[0], i232 newstart, newend = j1 - back[1], j233 if oldchanges != [] and oldstart <= oldchanges[-1][1] < oldend:34 oldchanges[-1] = (oldchanges[-1][0], oldend)35 elif not oldline[oldstart:oldend].isspace():36 oldchanges.append((oldstart, oldend))37 if newchanges != [] and newstart <= newchanges[-1][1] < newend:38 newchanges[-1] = (newchanges[-1][0], newend)39 elif not newline[newstart:newend].isspace():40 newchanges.append((newstart, newend))41 back = (0, 0)42 return (oldchanges, newchanges)43def highlight_regions(string, regions):44 """Given `string` and `regions` (a list of (beginning index, end index)45 tuples), return `string` marked up to highlight those regions.46 >>> highlight_regions('This is a string.', [(0, 3), (8, 9)])47 '<Thi>s is <a> string.'48 """49 string = list(string)50 # Inserting into the middle of a list shifts all the elements over by one.51 # Each time a markup element is added, increase a result string's insertion52 # offset.53 offset = 054 for beginning, end in sorted(regions or []):55 string.insert(offset + beginning, LEFT_HIGHLIGHT_CHARACTER)56 offset += 157 string.insert(offset + end, RIGHT_HIGHLIGHT_CHARACTER)58 offset += 159 return ''.join(string)60# no namedtuple in Python 2.5; here is a simple imitation61# HighlightedDiff = collections.namedtuple('HighlightedDiff', 'old new')62class HighlightedDiff(tuple):63 def __new__(cls, old, new):64 return tuple.__new__(cls, (old, new))65 __slots__ = () # no attributes allowed66 @property67 def old(self):68 return self[0]69 @property70 def new(self):71 return self[1]72 def __repr__(self):73 return '%s(old=%r, new=%r)' % (self.__class__.__name__, self.old, self.new)74def highlight(old, new):75 """Given two strings, return a `HighlightedDiff` containing the strings76 with markup identifying the parts that changed.77 >>> highlight('Testify is great.', 'testify is gr8')78 HighlightedDiff(old='<T>estify is gr<eat.>', new='<t>estify is gr<8>')79 """80 oldchanges, newchanges = get_line_changed_regions(old, new)81 return HighlightedDiff(highlight_regions(old, oldchanges),82 highlight_regions(new, newchanges))...

Full Screen

Full Screen

chunk_generators.py

Source:chunk_generators.py Github

copy

Full Screen

...5 """A chunk generator for rendered Markdown content.6 This works like a standard RawDiffChunkGenerator, but handles showing7 changes within a line for HTML-rendered Markdown.8 """9 def get_line_changed_regions(self, old_line_num, old_line,10 new_line_num, new_line):11 """Return information on changes between two lines.12 This returns the regions between lines of rendered Markdown that13 have changed.14 Only text changes are highlighted, and not formatting changes (such as15 the addition of bold text).16 """17 return \18 super(MarkdownDiffChunkGenerator, self).get_line_changed_regions(19 old_line_num,20 strip_tags(old_line),21 new_line_num,...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful