diff --git a/gendiff/formatters/format_determinant.py b/gendiff/formatters/format_determinant.py index 04fc876..31f6111 100644 --- a/gendiff/formatters/format_determinant.py +++ b/gendiff/formatters/format_determinant.py @@ -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) diff --git a/gendiff/formatters/plain.py b/gendiff/formatters/plain.py index af4c1f8..ceea13e 100644 --- a/gendiff/formatters/plain.py +++ b/gendiff/formatters/plain.py @@ -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)}" ) @@ -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, [], [])) diff --git a/gendiff/formatters/stylish.py b/gendiff/formatters/stylish.py index 53f2bb7..5e6f788 100644 --- a/gendiff/formatters/stylish.py +++ b/gendiff/formatters/stylish.py @@ -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 = ' ' @@ -16,7 +16,7 @@ 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]}' + '}' ) @@ -24,7 +24,7 @@ def walk(dict_, output, depth, indent): 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}' diff --git a/tests/fixtures/comparison_results/plain_format_comparison.txt b/tests/fixtures/comparison_results/plain_format_comparison.txt new file mode 100644 index 0000000..bf58a17 --- /dev/null +++ b/tests/fixtures/comparison_results/plain_format_comparison.txt @@ -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] \ No newline at end of file diff --git a/tests/test_format_determinant.py b/tests/test_format_determinant.py index 58b70a0..8491eaa 100644 --- a/tests/test_format_determinant.py +++ b/tests/test_format_determinant.py @@ -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]" \ No newline at end of file +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' \ No newline at end of file diff --git a/tests/test_generate_diff.py b/tests/test_generate_diff.py index 789a3be..18e80d0 100644 --- a/tests/test_generate_diff.py +++ b/tests/test_generate_diff.py @@ -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()