Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kotano committed Aug 18, 2020
1 parent 4360372 commit e5a6523
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 123 deletions.
52 changes: 31 additions & 21 deletions gendiff/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
"""

# STATUSES
COMMON, NEW, REMOVED, CHANGED = ' ', '+', '-', 'c'
COMMON, NEW, REMOVED, MODIFIED = ' ', '+', '-', 'm'


class Change:
"""`Change` class provides info about changed attributes."""

changedfrom = None
"""Link to previous state of diff."""
"""`Change` class provides info about `Diff`'s changed attributes."""

def __init__(self, status=COMMON, key='', value=None, *, parent=''):
"""Create `Change` class instance.
Expand All @@ -29,6 +26,9 @@ def __init__(self, status=COMMON, key='', value=None, *, parent=''):
self.level = parent.level + 1 if parent else 1
self.value = self._normalize(value)

self.changedfrom = None
"""Link to previous state of diff."""

def _normalize(self, value):
res = value
if isinstance(value, dict):
Expand Down Expand Up @@ -56,7 +56,11 @@ def to_dict(self):
res = dict(self.__dict__)
# Link to parent object leads to loop while serializing,
# so it is better to simply leave parent's key in the list.
res['parent'] = [x.key for x in self.get_parents()]
parents = self.get_parents()
parent = parents[-1].key if parents else ''
# res['parent'] = [x.key for x in self.get_parents()]
# res['parent'] = self.get_parents()[-1].key
res['parent'] = parent
return res

# VISUAL
Expand All @@ -65,7 +69,7 @@ def __str__(self):
indent = self.level * 2 * ' '
status = self.status

if self.status == CHANGED:
if self.status == MODIFIED:
# Add previous diff on top of current
template = '{}\n{}'.format(str(self.changedfrom), template)
status = NEW
Expand All @@ -82,7 +86,7 @@ def __str__(self):

class Diff:
"""`Diff` class gathers information about differences
between two dictionaries and keeps this info as `Diffs` in `contents`.
between two dictionaries and keeps this info as `Changes` in `contents`.
Attributes:
contents (list): list with `Change` objects.
Expand All @@ -101,8 +105,6 @@ class Diff:
brackets: str = '{}'
"""A pair of characters to use as trailing and closing parentheses
for rendering."""
parent = ''
"""Parent object. Usually leads to `Change` object or empty string."""

def __init__(self, before={}, after={}, *, level=0):
"""Create an instance of `Diff` class.
Expand All @@ -127,6 +129,7 @@ def __init__(self, before={}, after={}, *, level=0):
self.new_keys = []
self.removed_keys = []
self.contents = []
self.parent = ''

self.find_difference(before, after)

Expand All @@ -149,7 +152,7 @@ def find_difference(self, before: dict, after: dict):
"""
difs = []

common = self.common_keys = list(before.keys() & after.keys())
common = self.common_keys = sorted(before.keys() & after.keys())
new = self.get_diffs(NEW, after, before)
removed = self.get_diffs(REMOVED, before, after)

Expand All @@ -168,14 +171,15 @@ def find_difference(self, before: dict, after: dict):
# Then there is changed values.
# Show which values have changed.
r = Change(REMOVED, k, before[k], parent=self)
c = Change(CHANGED, k, after[k], parent=self)
c = Change(MODIFIED, k, after[k], parent=self)
# Remember previous value
c.changedfrom = r
difs.append(c)

# Update Diff attributes.
self.new_keys = [x.key for x in new]
self.removed_keys = [x.key for x in removed]
# Sort everything to make tests pass.
self.new_keys = sorted([x.key for x in new])
self.removed_keys = sorted([x.key for x in removed])
self.contents = sorted(difs, key=lambda x: x.key)

return self.contents
Expand All @@ -198,10 +202,16 @@ def _render_contents(self, difs):

def __str__(self):
# Indentation for closing bracket.
inc = self.level + 1 if self.parent else self.level
indent = inc * 2 * ' '
rep = f'''\
{self.brackets[0]}
{self._render_contents(self.contents)}
{indent}{self.brackets[1]}'''
return rep
depth = self.level + 1 if self.parent else self.level
indent = depth * 2 * ' '
template = (
'{br1}\n'
'{contents}\n'
'{indent}''{br2}'
)
res = template.format(
br1=self.brackets[0],
contents=self._render_contents(self.contents),
indent=indent, br2=self.brackets[1]
)
return res
4 changes: 2 additions & 2 deletions gendiff/views/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


from gendiff.diff import Diff
from gendiff.diff import COMMON, NEW, REMOVED, CHANGED
from gendiff.diff import COMMON, NEW, REMOVED, MODIFIED


def to_plain(change) -> str:
Expand All @@ -36,7 +36,7 @@ def to_plain(change) -> str:
elif change.status == REMOVED:
return template.format('removed')

elif change.status == CHANGED:
elif change.status == MODIFIED:
value = "changed. From '{}' to '{}'".format(
change.changedfrom.value, change.value)
return template.format(value)
Expand Down
Loading

0 comments on commit e5a6523

Please sign in to comment.