[diffoscope] 01/02: Add a size() method to Difference and check that self._visuals is empty in get_reverse()
Ximin Luo
infinity0 at debian.org
Tue May 30 17:41:02 CEST 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch experimental
in repository diffoscope.
commit 1480faf190c1c837a097c3ee4839b5cb8bf5a04e
Author: Ximin Luo <infinity0 at debian.org>
Date: Tue May 30 17:17:30 2017 +0200
Add a size() method to Difference and check that self._visuals is empty in get_reverse()
---
diffoscope/difference.py | 40 ++++++++++++++++++++++++++++++----------
tests/test_difference.py | 14 ++++++++++++++
2 files changed, 44 insertions(+), 10 deletions(-)
diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index cef38f3..d4acd6d 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -51,6 +51,9 @@ class VisualDifference(object):
def source(self):
return self._source
+ def size(self):
+ return len(self.data_type) + len(self.content) + len(self.source)
+
class Difference(object):
def __init__(self, unified_diff, path1, path2, source=None, comment=None, has_internal_linenos=False, details=None):
@@ -81,10 +84,24 @@ class Difference(object):
self._has_internal_linenos = has_internal_linenos
self._details = details or []
self._visuals = []
+ self._size_cache = None
def __repr__(self):
return '<Difference %s -- %s %s>' % (self._source1, self._source2, self._details)
+ def get_reverse(self):
+ logger.debug('reverse orig %s %s', self.source1, self.source2)
+ if self._visuals:
+ raise NotImplementedError("reversing VisualDifference is not yet implemented")
+ difference = Difference(
+ None if self.unified_diff is None else reverse_unified_diff(self.unified_diff),
+ self.source2,
+ self.source1,
+ comment = self.comments,
+ has_internal_linenos = self.has_internal_linenos,
+ details = [d.get_reverse() for d in self._details])
+ return difference
+
def equals(self, other):
return self == other or (
self.unified_diff == other.unified_diff and
@@ -94,6 +111,16 @@ class Difference(object):
self.has_internal_linenos == other.has_internal_linenos and
all(x.equals(y) for x, y in zip(self.details, other.details)))
+ def size(self):
+ if self._size_cache is None:
+ self._size_cache = (len(self.unified_diff) +
+ len(self.source1) +
+ len(self.source2) +
+ sum(map(len, self.comments)) +
+ sum(d.size() for d in self._details) +
+ sum(v.size() for v in self._visuals))
+ return self._size_cache
+
@staticmethod
def from_feeder(feeder1, feeder2, path1, path2, source=None, comment=None, **kwargs):
try:
@@ -176,6 +203,7 @@ class Difference(object):
def add_comment(self, comment):
for line in comment.splitlines():
self._comments.append(line)
+ self._size_cache = None
@property
def source1(self):
@@ -205,21 +233,13 @@ class Difference(object):
if len([d for d in differences if type(d) is not Difference]) > 0:
raise TypeError("'differences' must contains Difference objects'")
self._details.extend(differences)
+ self._size_cache = None
def add_visuals(self, visuals):
if any([type(v) is not VisualDifference for v in visuals]):
raise TypeError("'visuals' must contain VisualDifference objects'")
self._visuals.extend(visuals)
-
- def get_reverse(self):
- if self._unified_diff is None:
- unified_diff = None
- else:
- unified_diff = reverse_unified_diff(self._unified_diff)
- logger.debug('reverse orig %s %s', self._source1, self._source2)
- difference = Difference(unified_diff, None, None, source=[self._source2, self._source1], comment=self._comments)
- difference.add_details([d.get_reverse() for d in self._details])
- return difference
+ self._size_cache = None
def make_feeder_from_text_reader(in_file, filter=lambda text_buf: text_buf):
def encoding_filter(text_buf):
diff --git a/tests/test_difference.py b/tests/test_difference.py
index f4770f2..fbf40d1 100644
--- a/tests/test_difference.py
+++ b/tests/test_difference.py
@@ -24,12 +24,17 @@ from diffoscope.config import Config
from diffoscope.difference import Difference
+def assert_algebraic_properties(d, size):
+ assert d.equals(d.get_reverse().get_reverse())
+ assert d.get_reverse().size() == d.size() == size
+
def test_too_much_input_for_diff(monkeypatch):
monkeypatch.setattr(Config(), 'max_diff_input_lines', 20)
too_long_text_a = io.StringIO("a\n" * 21)
too_long_text_b = io.StringIO("b\n" * 21)
difference = Difference.from_text_readers(too_long_text_a, too_long_text_b, 'a', 'b')
assert '[ Too much input for diff ' in difference.unified_diff
+ assert_algebraic_properties(difference, 290)
def test_too_long_diff_block_lines(monkeypatch):
monkeypatch.setattr(Config(), 'enforce_constraints', False)
@@ -38,6 +43,15 @@ def test_too_long_diff_block_lines(monkeypatch):
too_long_text_b = io.StringIO("b\n" * 21)
difference = Difference.from_text_readers(too_long_text_a, too_long_text_b, 'a', 'b')
assert '[ 11 lines removed ]' in difference.unified_diff
+ assert_algebraic_properties(difference, 124)
+
+def test_size_updates():
+ d = Difference("0123456789", "path1", "path2")
+ assert d.size() == 20
+ d.add_details([Difference("0123456789", "path1/a", "path2/a")])
+ assert d.size() == 44
+ d.add_comment("lol1")
+ assert d.size() == 48
def test_non_str_arguments_to_source1_source2():
for x in (
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list