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 5c239eb commit e31fed8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
19 changes: 9 additions & 10 deletions gendiff/formatters/format_determinant.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
def get_data_format(value):
if not isinstance(value, dict):
if value is None:
data_format = 'null'
elif isinstance(value, str):
data_format = f"'{str(value)}'"
else:
data_format = str(value).lower()
else:
data_format = '[complex value]'
return data_format
if isinstance(value, dict):
return '[complex value]'
if isinstance(value, bool):
return str(value).lower()
if isinstance(value, str):
return value
if value is None:
return 'null'
return str(value)
6 changes: 3 additions & 3 deletions gendiff/formatters/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
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(new_value)}'"
if action == 'REMOVED':
return f'{beginning} 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(old_value)}' to '{get_data_format(new_value)}'"
)


Expand Down
2 changes: 1 addition & 1 deletion gendiff/formatters/stylish.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def walk(dict_, output, depth, indent):
else:
output.append(beginning + get_data_format(value))
return (
'{\n' + '\n'.join(output) + f'\n{indent + (INDENT_CHAR*INDENT_NUMBER)[0:-LEFTWARD_SHIFT]}' + '}' # noqa: E226
'{\n' + '\n'.join(output) + f'\n{indent + (INDENT_CHAR * INDENT_NUMBER)[0:-LEFTWARD_SHIFT]}' + '}'
)

if isinstance(value, dict):
Expand Down
2 changes: 1 addition & 1 deletion gendiff/gendiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def generate_diff(filepath1, filepath2, output_format='stylish'):
+ 'Available formats are: "stylish", "plain", "json".'
)

return apply_format(get_diff(config1, config2)).replace("'", "")
return apply_format(get_diff(config1, config2))
2 changes: 1 addition & 1 deletion tests/test_format_determinant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
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("hello") == "hello"
assert get_data_format(None) == "null"
assert get_data_format({1: [2, 3]}) == "[complex value]"
20 changes: 10 additions & 10 deletions tests/test_stylish.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@


def test_get_stylish_view():
assert get_stylish_view('key', 'value', '+ ', 1, ' ') == " + key: 'value'"
assert get_stylish_view('key', {'nested_key': 'value'}, ' ', 1, ' ') == " key: {\n nested_key: 'value'\n }"
assert get_stylish_view('key', 'value', '+ ', 1, ' ') == " + key: value"
assert get_stylish_view('key', {'nested_key': 'value'}, ' ', 1, ' ') == " key: {\n nested_key: value\n }"

def test_form_string():
assert form_string('key', 'ADDED', None, 'new_value', 1, ' ') == " + key: 'new_value'"
assert form_string('key', 'REMOVED', 'old_value', None, 1, ' ') == " - key: 'old_value'"
assert form_string('key', 'CHANGED', 'old_value', 'new_value', 1, ' ') == " - key: 'old_value'\n + key: 'new_value'"
assert form_string('key', 'UNCHANGED', 'old_value', 'old_value', 1, ' ') == " key: 'old_value'"
assert form_string('key', 'ADDED', None, 'new_value', 1, ' ') == " + key: new_value"
assert form_string('key', 'REMOVED', 'old_value', None, 1, ' ') == " - key: old_value"
assert form_string('key', 'CHANGED', 'old_value', 'new_value', 1, ' ') == " - key: old_value\n + key: new_value"
assert form_string('key', 'UNCHANGED', 'old_value', 'old_value', 1, ' ') == " key: old_value"

def test_get_stylished():
diff = [
Expand Down Expand Up @@ -38,10 +38,10 @@ def test_get_stylished():
]
expected_output = "\n".join([
"{",
" + property: 'new_value'",
" - another_property: 'old_value'",
" - yet_another_property: 'old_value'",
" + yet_another_property: 'new_value'",
" + property: new_value",
" - another_property: old_value",
" - yet_another_property: old_value",
" + yet_another_property: new_value",
"}"
])
assert get_stylished(diff) == expected_output

0 comments on commit e31fed8

Please sign in to comment.