def test_find_context_multiple_times(diff_processor): context = [ ('unmod', 'line04\n'), ('unmod', 'line05\n'), ('unmod', 'line06\n'), ('add', 'line06a add line\n'), ('unmod', 'line07\n'), ('unmod', 'line08\n'), ('unmod', 'line09\n'), ] found_line = diff_processor.find_context('file.txt', context, offset=3) assert found_line == [ diffs.DiffLineNumber(old=None, new=7), diffs.DiffLineNumber(old=None, new=49), ]
def test_find_context_end_of_chunk(diff_processor): context_of_last_line = [ ('unmod', 'line10\n'), ('unmod', 'line11\n'), ('unmod', 'line12\n'), ('unmod', 'line13\n'), ] found_line = diff_processor.find_context( 'file.txt', context_of_last_line, offset=3) assert found_line == [diffs.DiffLineNumber(old=13, new=14)]
def test_find_context_beginning_of_chunk(diff_processor): context_of_first_line = [ ('unmod', 'line02\n'), ('unmod', 'line03\n'), ('unmod', 'line04\n'), ('unmod', 'line05\n'), ] found_line = diff_processor.find_context( 'file.txt', context_of_first_line, offset=0) assert found_line == [diffs.DiffLineNumber(old=2, new=2)]
def test_context_of_an_incomplete_hunk_in_the_beginning(diff_processor): context = diff_processor.get_context_of_line( path='file.txt', diff_line=diffs.DiffLineNumber(old=None, new=2)) expected_context = [ ('unmod', 'line01\n'), ('add', 'line01a Add line after line01\n'), ('unmod', 'line02\n'), ('unmod', 'line03\n'), ('unmod', 'line04\n'), ] assert context == expected_context
def test_context_of_an_incomplete_hunk_in_the_end(diff_processor): context = diff_processor.get_context_of_line( path='file.txt', diff_line=diffs.DiffLineNumber(old=None, new=80)) expected_context = [ ('unmod', 'line36\n'), ('unmod', 'line37\n'), ('unmod', 'line38\n'), ('add', 'line38a Add line after line38\n'), ('unmod', 'line39\n'), ] assert context == expected_context
def _parse_comment_line_number(line_no): """ Parses line numbers of the form "(o|n)\d+" and returns them in a tuple. """ old_line = None new_line = None if line_no.startswith('o'): old_line = int(line_no[1:]) elif line_no.startswith('n'): new_line = int(line_no[1:]) else: raise ValueError("Comment lines have to start with either 'o' or 'n'.") return diffs.DiffLineNumber(old_line, new_line)
def test_context_of_an_invisible_line_end_of_hunk(diff_processor): # Note: The caller has to pass in a diff which is suitable to satisfy # its requirements. This test just ensures that we see a sane behavior. context = diff_processor.get_context_of_line( path='file.txt', diff_line=diffs.DiffLineNumber(old=12, new=None)) expected_context = [ ('unmod', 'line09\n'), ('unmod', 'line10\n'), ('unmod', 'line11\n'), ('unmod', 'line12\n'), ('unmod', 'line13\n'), ] assert context == expected_context
def test_context_of_a_new_line_number(diff_processor): context = diff_processor.get_context_of_line( path='file.txt', diff_line=diffs.DiffLineNumber(old=None, new=8)) expected_context = [ ('unmod', 'line05\n'), ('unmod', 'line06\n'), ('unmod', 'line07\n'), ('add', 'line07a Add after line07\n'), ('unmod', 'line08\n'), ('unmod', 'line09\n'), ('unmod', 'line10\n'), ] assert context == expected_context
def test_find_context_beginning_of_file(diff_processor): context_of_first_line = [ ('add', 'line01a Add line after line01\n'), ('unmod', 'line02\n'), ('unmod', 'line03\n'), ('unmod', 'line04\n'), ('unmod', 'line05\n'), ('unmod', 'line06\n'), ('unmod', 'line07\n'), ] found_line = diff_processor.find_context( 'file.txt', context_of_first_line, offset=3) assert found_line == [diffs.DiffLineNumber(old=4, new=5)]
def test_find_context_with_full_context(diff_processor): context_of_line_7 = [ ('unmod', 'line05\n'), ('unmod', 'line06\n'), ('unmod', 'line07\n'), ('add', 'line07a Add after line07\n'), ('unmod', 'line08\n'), ('unmod', 'line09\n'), ('unmod', 'line10\n'), ] found_line = diff_processor.find_context( 'file.txt', context_of_line_7, offset=3) assert found_line == [diffs.DiffLineNumber(old=None, new=8)]
def test_context_of_new_and_old_line_number_raises(diff_processor): with pytest.raises(ValueError): diff_processor.get_context_of_line( path='file.txt', diff_line=diffs.DiffLineNumber(old=7, new=7))
def test_context_of_a_missing_file_raises(diff_processor): with pytest.raises(diffs.FileNotInDiffException): diff_processor.get_context_of_line( path='not_existing_file.txt', diff_line=diffs.DiffLineNumber(old=None, new=8))
def test_context_of_a_missing_line_raises(diff_processor): missing_line = 20 with pytest.raises(diffs.LineNotInDiffException): diff_processor.get_context_of_line( path='file.txt', diff_line=diffs.DiffLineNumber(old=None, new=missing_line))
def test_appends_newline_for_each_context_line(diff_processor): context = diff_processor.get_context_of_line( path='file_b', diff_line=diffs.DiffLineNumber(old=None, new=1)) assert context == [('add', 'test_content\n')]