def test_append_deleted_line(self): hunk = Hunk(src_len=1, tgt_len=0) hunk.append_deleted_line(self.sample_line) self.assertTrue(hunk.is_valid()) self.assertEqual(len(hunk.source_lines), 1) self.assertEqual(hunk.target_lines, []) self.assertIn(self.sample_line, hunk.source_lines)
def _parse_hunk(diff, source_start, source_len, target_start, target_len, section_header): """Parse a diff hunk details.""" hunk = Hunk(source_start, source_len, target_start, target_len, section_header) modified = 0 deleting = 0 for line in diff: valid_line = RE_HUNK_BODY_LINE.match(line) if not valid_line: raise UnidiffParseException("Hunk diff data expected") action = valid_line.group(0) original_line = line[1:] if action == LINE_TYPE_ADD: hunk.append_added_line(original_line) # modified lines == deleted immediately followed by added if deleting > 0: modified += 1 deleting -= 1 elif action == LINE_TYPE_DELETE: hunk.append_deleted_line(original_line) deleting += 1 elif action == LINE_TYPE_CONTEXT: hunk.append_context_line(original_line) hunk.add_to_modified_counter(modified) # reset modified auxiliar variables deleting = 0 modified = 0 # check hunk len(old_lines) and len(new_lines) are ok if hunk.is_valid(): break return hunk
def test_append_deleted_line(self): hunk = Hunk(src_len=1, tgt_len=0) hunk.append(self.removed_line) self.assertTrue(hunk.is_valid()) self.assertEqual(len(hunk.source), 1) self.assertEqual(hunk.target, []) self.assertIn(str(self.removed_line), hunk.source) source_lines = list(hunk.source_lines()) self.assertEqual(source_lines, [self.removed_line])
def test_append_added_line(self): hunk = Hunk(src_len=0, tgt_len=1) hunk.append(self.added_line) self.assertTrue(hunk.is_valid()) self.assertEqual(len(hunk.target), 1) self.assertEqual(hunk.source, []) self.assertIn(str(self.added_line), hunk.target) target_lines = list(hunk.target_lines()) self.assertEqual(target_lines, [self.added_line])
def test_append_added_line(self): hunk = Hunk(src_len=0, tgt_len=1) hunk.append_added_line(self.sample_line) self.assertTrue(hunk.is_valid()) self.assertEqual(len(hunk.target_lines), 1) self.assertEqual(hunk.source_lines, []) self.assertIn(self.sample_line, hunk.target_lines) self.assertEqual(hunk.source_types, []) self.assertEqual(len(hunk.target_types), 1) self.assertEqual(hunk.target_types[0], LINE_TYPE_ADD)
def test_append_context(self): hunk = Hunk(src_len=1, tgt_len=1) hunk.append_context_line(self.sample_line) self.assertTrue(hunk.is_valid()) self.assertEqual(len(hunk.source_lines), 1) self.assertEqual(hunk.target_lines, hunk.source_lines) self.assertIn(self.sample_line, hunk.source_lines) self.assertEqual(len(hunk.source_types), 1) self.assertEqual(hunk.target_types, hunk.source_types) self.assertEqual(hunk.source_types[0], LINE_TYPE_CONTEXT) self.assertEqual(hunk.target_types[0], LINE_TYPE_CONTEXT)
def test_modified_counter(self): hunk = Hunk(src_len=1, tgt_len=1) hunk.append_deleted_line(self.sample_line) hunk.append_added_line(self.sample_line) hunk.add_to_modified_counter(1) self.assertTrue(hunk.is_valid()) self.assertEqual(hunk.modified, 1) self.assertEqual(hunk.added, 0) self.assertEqual(hunk.deleted, 0)
def test_append_context(self): hunk = Hunk(src_len=1, tgt_len=1) hunk.append(self.context_line) self.assertTrue(hunk.is_valid()) self.assertEqual(len(hunk.source), 1) self.assertEqual(hunk.target, hunk.source) self.assertIn(str(self.context_line), hunk.source) source_lines = list(hunk.source_lines()) target_lines = list(hunk.target_lines()) self.assertEqual(target_lines, source_lines) self.assertEqual(target_lines, [self.context_line])
def test_default_is_valid(self): hunk = Hunk() self.assertTrue(hunk.is_valid())
def test_missing_length(self): hunk = Hunk(src_len=None, tgt_len=None) hunk.append_context_line(self.sample_line) self.assertTrue(hunk.is_valid())
def test_missing_data_is_not_valid(self): hunk = Hunk(src_len=1, tgt_len=1) self.assertFalse(hunk.is_valid())
def test_is_modified_file(self): hunk = Hunk(src_start=1, src_len=10, tgt_start=1, tgt_len=8) self.patched_file.append(hunk) self.assertTrue(self.patched_file.is_modified_file)
def test_is_removed_file(self): hunk = Hunk(src_start=1, src_len=10, tgt_start=0, tgt_len=0) self.patched_file.append(hunk) self.assertTrue(self.patched_file.is_removed_file)
def test_missing_length(self): hunk = Hunk(src_len=None, tgt_len=None) hunk.append(self.context_line) self.assertTrue(hunk.is_valid())