From 63bb1fd1f5a56d84f2cc6f517a020b5a5a49e00a Mon Sep 17 00:00:00 2001 From: Morgan Courbet Date: Tue, 4 Jul 2017 21:51:08 +0200 Subject: [PATCH] Add unit test for mixed_line_ending --fix={cr,crlf} --- tests/mixed_line_ending_test.py | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/mixed_line_ending_test.py b/tests/mixed_line_ending_test.py index 0b33c92..3da24a7 100644 --- a/tests/mixed_line_ending_test.py +++ b/tests/mixed_line_ending_test.py @@ -48,3 +48,53 @@ def test_detect_mixed_line_ending(input_s, expected_retval, output, tmpdir): assert ret == expected_retval assert path.read() == output + + +# Input, expected return value, expected output +TESTS_FIX_FORCE_LF = ( + # only 'LF' + (b'foo\nbar\nbaz\n', 1, b'foo\nbar\nbaz\n'), + # only 'CRLF' + (b'foo\r\nbar\r\nbaz\r\n', 1, b'foo\nbar\nbaz\n'), + # mixed with majority of 'LF' + (b'foo\r\nbar\nbaz\n', 1, b'foo\nbar\nbaz\n'), + # mixed with majority of 'CRLF' + (b'foo\r\nbar\nbaz\r\n', 1, b'foo\nbar\nbaz\n'), +) + + +@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), + TESTS_FIX_FORCE_LF) +def test_mixed_line_ending_fix_force_lf(input_s, expected_retval, output, + tmpdir): + path = tmpdir.join('file.txt') + path.write(input_s) + ret = mixed_line_ending(('--fix=lf', '-vv', path.strpath)) + + assert ret == expected_retval + assert path.read() == output + + +# Input, expected return value, expected output +TESTS_FIX_FORCE_CRLF = ( + # only 'LF' + (b'foo\nbar\nbaz\n', 1, b'foo\r\nbar\r\nbaz\r\n'), + # only 'CRLF' + (b'foo\r\nbar\r\nbaz\r\n', 1, b'foo\r\nbar\r\nbaz\r\n'), + # mixed with majority of 'LF' + (b'foo\r\nbar\nbaz\n', 1, b'foo\r\nbar\r\nbaz\r\n'), + # mixed with majority of 'CRLF' + (b'foo\r\nbar\nbaz\r\n', 1, b'foo\r\nbar\r\nbaz\r\n'), +) + + +@pytest.mark.parametrize(('input_s', 'expected_retval', 'output'), + TESTS_FIX_FORCE_CRLF) +def test_mixed_line_ending_fix_force_crlf(input_s, expected_retval, output, + tmpdir): + path = tmpdir.join('file.txt') + path.write(input_s) + ret = mixed_line_ending(('--fix=crlf', '-vv', path.strpath)) + + assert ret == expected_retval + assert path.read() == output