Repository: coala/coala

Issue:
Diff: Remove has_changes field
The better alternative to `has_changes` is using `__bool__`.
Task is to remove `has_changes` again by reverting commit https://github.com/coala/coala/commit/671e6a2e9fe1f5ab8bab5370c1848f00b3472030.

Sorry @lshobith, but your work is not in vain, now we know also thanks to you how to do it better ;)

See also https://github.com/coala/coala/issues/3790


Discussion:
@Makman2 are you sure, have a look at this:
```py
In [1]: from coalib.results.Diff import Diff

In [2]: diff = Diff([1,2,3])

In [3]: diff.delete_line(3)

In [4]: diff.add_line(3, 3)

In [5]: diff.has_changes
Out[5]: False

In [6]: bool(diff)
Out[6]: True
```
If you look at `has_changes` and `bool` they are quite different
From the docstring of `__bool__`:

> False if the patch has no effect at all when applied.

Then `__bool__` has actually a bug imo^^
The bug is that `self._changes`, which is queried in` __bool__` does still include the changes, even if they negate each other.

We could just incorporate the computation of `self.has_changes` to `__bool__ `instead of looking at the `self._changes` property
yeah this is safer :)
in `__bool__` , `self.rename is not False or ...` looks suspiciously like it will return `True` way too often.
@jayvdb `rename` is either a string labelling the new file name or it's `False`, meaning no rename shall happen. So imo it's alright^^

Resolving patch:
diff --git a/coalib/results/Diff.py b/coalib/results/Diff.py
--- a/coalib/results/Diff.py
+++ b/coalib/results/Diff.py
@@ -26,7 +26,7 @@ def __init__(self, file_list, rename=False, delete=False):
         :param delete:    True if file is set to be deleted.
         """
         self._changes = {}
-        self._file = file_list
+        self._file = list(file_list)
         self.rename = rename
         self.delete = delete
 
@@ -277,14 +277,6 @@ def modified(self):
 
         return result
 
-    @property
-    def has_changes(self):
-        """
-        True if the modified file is different than the original file,
-        else False.
-        """
-        return self.modified != self._file
-
     @property
     def unified_diff(self):
         """
@@ -440,7 +432,7 @@ def __bool__(self):
         """
         return (self.rename is not False or
                 self.delete is True or
-                len(self._changes) > 0)
+                self.modified != self._file)
 
     def delete_line(self, line_nr):
         """


Test patch:
diff --git a/tests/results/DiffTest.py b/tests/results/DiffTest.py
--- a/tests/results/DiffTest.py
+++ b/tests/results/DiffTest.py
@@ -148,12 +148,21 @@ def test_modified(self):
         del result_file[2]
         self.assertEqual(self.uut.modified, result_file)
 
-    def test_has_changes(self):
-        self.assertFalse(self.uut.has_changes)
+    def test_bool(self):
+        self.assertFalse(self.uut)
         self.uut.add_line(4, '4')
-        self.assertTrue(self.uut.has_changes)
+        self.assertTrue(self.uut)
         self.uut.delete_line(4)
-        self.assertFalse(self.uut.has_changes)
+        self.assertFalse(self.uut)
+
+        # test if it works with tuples.
+        uutuple = Diff(('1', '2', '3', '4'))
+
+        self.assertFalse(uutuple)
+        uutuple.add_line(4, '4')
+        self.assertTrue(uutuple)
+        uutuple.delete_line(4)
+        self.assertFalse(uutuple)
 
     def test_addition(self):
         self.assertRaises(TypeError, self.uut.__add__, 5)
@@ -560,8 +569,16 @@ def test_equality(self):
         b = ['first', 'third']
         diff_1 = Diff.from_string_arrays(a, b)
 
+        c = ['first', 'second', 'third']
+        d = ['first', 'third']
+
+        diff_2 = Diff.from_string_arrays(c, d)
+
+        self.assertEqual(diff_1, diff_2)
+
+        # changing the original array should not influence
+        # the diff
         a[1] = 'else'
-        diff_2 = Diff.from_string_arrays(a, b)
         self.assertEqual(diff_1, diff_2)
 
         diff_1.rename = 'abcd'