Skip to content

Commit

Permalink
error correction
Browse files Browse the repository at this point in the history
  • Loading branch information
DSungatulin committed Feb 23, 2024
1 parent 22b72fd commit 1b2d64e
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
18 changes: 13 additions & 5 deletions gendiff/formatters/format_determinant.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
def get_data_format(value):
def get_data_format_stylish(value):
if isinstance(value, str):
return value
elif value is None:
return 'null'
return str(value).lower()


def get_data_format_plain(value):
if isinstance(value, dict):
return '[complex value]'
if isinstance(value, bool):
elif isinstance(value, bool):
return str(value).lower()
if isinstance(value, str):
return value
if value is None:
elif isinstance(value, str):
return f"'{value}'"
elif value is None:
return 'null'
return str(value)
19 changes: 15 additions & 4 deletions gendiff/formatters/plain.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
from gendiff.formatters.format_determinant import get_data_format
from gendiff.formatters.format_determinant import get_data_format_plain


def get_data_format(value):
if isinstance(value, dict):
return '[complex value]'
if isinstance(value, bool):
return str(value).lower()
if isinstance(value, str):
return f"'{str(value)}'"
if value is None:
return 'null'
return str(value)

def get_plain_view(path, action, old_value, new_value):
beginning = f"Property '{path}' was"
if action == 'ADDED':
return f"{beginning} added with value: '{get_data_format(new_value)}'"
return f"{beginning} added with value: {get_data_format_plain(new_value)}"
if action == 'REMOVED':
return f"{beginning} removed"
if action == 'CHANGED':
return (
f"{beginning} updated. From '{get_data_format(old_value)}' to '{get_data_format(new_value)}'"
f"{beginning} updated. From {get_data_format_plain(old_value)} to {get_data_format_plain(new_value)}"
)


Expand All @@ -35,4 +46,4 @@ def walk(diff, path, output):
))
return output

return '\n'.join(walk(diff, [], [])).replace("'[complex value]'", "[complex value]")
return '\n'.join(walk(diff, [], []))
6 changes: 3 additions & 3 deletions gendiff/formatters/stylish.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gendiff.formatters.format_determinant import get_data_format
from gendiff.formatters.format_determinant import get_data_format_stylish


INDENT_CHAR = ' '
Expand All @@ -16,15 +16,15 @@ def walk(dict_, output, depth, indent):
beginning + walk(value, [], depth + 1, indent + (INDENT_CHAR * INDENT_NUMBER))
)
else:
output.append(beginning + get_data_format(value))
output.append(beginning + get_data_format_stylish(value))
return (
'{\n' + '\n'.join(output) + f'\n{indent + (INDENT_CHAR * INDENT_NUMBER)[0:-LEFTWARD_SHIFT]}' + '}'
)

if isinstance(value, dict):
formatted = walk(value, [], depth + 1, indent)
else:
formatted = get_data_format(value)
formatted = get_data_format_stylish(value)

return f'{indent + special_char + key}: {formatted}'

Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/comparison_results/plain_format_comparison.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Property 'common.follow' was added with value: false
Property 'common.setting2' was removed
Property 'common.setting3' was updated. From true to null
Property 'common.setting4' was added with value: 'blah blah'
Property 'common.setting5' was added with value: [complex value]
Property 'common.setting6.doge.wow' was updated. From '' to 'so much'
Property 'common.setting6.ops' was added with value: 'vops'
Property 'group1.baz' was updated. From 'bas' to 'bars'
Property 'group1.nest' was updated. From [complex value] to 'str'
Property 'group2' was removed
Property 'group3' was added with value: [complex value]
26 changes: 18 additions & 8 deletions tests/test_format_determinant.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from gendiff.formatters.format_determinant import get_data_format

def test_get_data_format():
assert get_data_format(10) == "10"
assert get_data_format(True) == "true"
assert get_data_format("hello") == "hello"
assert get_data_format(None) == "null"
assert get_data_format({1: [2, 3]}) == "[complex value]"
from gendiff.formatters.format_determinant import get_data_format_plain

def test_get_data_format_plain_dict():
assert get_data_format_plain({"key": "value"}) == '[complex value]'

def test_get_data_format_plain_bool():
assert get_data_format_plain(True) == 'true'
assert get_data_format_plain(False) == 'false'

def test_get_data_format_plain_string():
assert get_data_format_plain("hello") == "'hello'"

def test_get_data_format_plain_none():
assert get_data_format_plain(None) == 'null'

def test_get_data_format_plain_other_types():
assert get_data_format_plain(123) == '123'
assert get_data_format_plain(12.3) == '12.3'
8 changes: 8 additions & 0 deletions tests/test_generate_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,11 @@ def test_generate_diff():
'tests/fixtures/comparison_results/correct_nested_comparison.txt', 'r'
)
assert file_differences == correct_result.read()

first_file = 'tests/fixtures/nested_files/file1.yml'
second_file = 'tests/fixtures/nested_files/file2.yml'
file_differences = generate_diff(first_file, second_file, 'plain')
correct_result = open(
'tests/fixtures/comparison_results/plain_format_comparison.txt', 'r'
)
assert file_differences == correct_result.read()

0 comments on commit 1b2d64e

Please sign in to comment.