Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions deepdiff/colored_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ def _colorize_json(self, obj: Any, path: str = 'root', indent: int = 0) -> str:
return '{...}' if isinstance(obj, dict) else '[...]'

if isinstance(obj, dict):
if not obj:
removed_map = self._get_path_removed(path)
if not obj and not removed_map:
return '{}'
items = []
for key, value in obj.items():
Expand All @@ -103,7 +104,7 @@ def _colorize_json(self, obj: Any, path: str = 'root', indent: int = 0) -> str:
items.append(f'{next_indent}{GREEN}"{key}": {self._colorize_json(value, new_path, indent + 1)}{RESET}')
else:
items.append(f'{next_indent}"{key}": {self._colorize_json(value, new_path, indent + 1)}')
for key, value in self._get_path_removed(path).items():
for key, value in removed_map.items():
new_path = f"{path}['{key}']" if isinstance(key, str) else f"{path}[{key}]"
items.append(f'{next_indent}{RED}"{key}": {self._colorize_json(value, new_path, indent + 1)}{RESET}')
return '{\n' + ',\n'.join(items) + f'\n{current_indent}' + '}'
Expand Down
62 changes: 62 additions & 0 deletions tests/test_colored_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,68 @@ def test_colored_compact_view_list_all_items_removed():
assert result == expected


def test_colored_view_dict_all_keys_removed():
"""Test that removed keys are displayed when a dict becomes empty."""
t1 = {'a': 1}
t2 = {}

diff = DeepDiff(t1, t2, view=COLORED_VIEW)
result = str(diff)

expected = f'''{{
{RED}"a": {RED}1{RESET}{RESET}
}}'''
assert result == expected


def test_colored_view_nested_dict_all_keys_removed():
"""Test that removed keys are displayed when a nested dict becomes empty."""
t1 = {'x': {'a': 1}}
t2 = {'x': {}}

diff = DeepDiff(t1, t2, view=COLORED_VIEW)
result = str(diff)

expected = f'''{{
"x": {{
{RED}"a": {RED}1{RESET}{RESET}
}}
}}'''
assert result == expected


def test_colored_compact_view_nested_dict_all_keys_removed():
"""Test that removed keys are displayed when a nested dict becomes empty with compact view."""
t1 = {'x': {'a': 1}, 'y': {'z': 3}}
t2 = {'x': {}, 'y': {'z': 3}}

diff = DeepDiff(t1, t2, view=COLORED_COMPACT_VIEW)
result = str(diff)

expected = f'''{{
"x": {{
{RED}"a": {RED}1{RESET}{RESET}
}},
"y": {{...}}
}}'''
assert result == expected


def test_colored_view_empty_dict_without_removals():
"""An empty dict with no removed keys under it is still displayed as {}."""
t1 = {'x': {}, 'y': 1}
t2 = {'x': {}, 'y': 2}

diff = DeepDiff(t1, t2, view=COLORED_VIEW)
result = str(diff)

expected = f'''{{
"x": {{}},
"y": {RED}1{RESET} -> {GREEN}2{RESET}
}}'''
assert result == expected


def test_colored_view_list_changes_deletions():
t1 = [1, 5, 7, 3, 6]
t2 = [1, 2, 3, 4]
Expand Down