diff options
Diffstat (limited to 'yapftests/reformatter_style_config_test.py')
-rw-r--r-- | yapftests/reformatter_style_config_test.py | 133 |
1 files changed, 125 insertions, 8 deletions
diff --git a/yapftests/reformatter_style_config_test.py b/yapftests/reformatter_style_config_test.py index 5afd805..c5726cb 100644 --- a/yapftests/reformatter_style_config_test.py +++ b/yapftests/reformatter_style_config_test.py @@ -29,7 +29,7 @@ class TestsForStyleConfig(yapf_test_helper.YAPFTest): def testSetGlobalStyle(self): try: - style.SetGlobalStyle(style.CreateChromiumStyle()) + style.SetGlobalStyle(style.CreateYapfStyle()) unformatted_code = textwrap.dedent(u"""\ for i in range(5): print('bar') @@ -38,9 +38,9 @@ class TestsForStyleConfig(yapf_test_helper.YAPFTest): for i in range(5): print('bar') """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, - reformatter.Reformat(uwlines)) + reformatter.Reformat(llines)) finally: style.SetGlobalStyle(style.CreatePEP8Style()) style.DEFAULT_STYLE = self.current_style @@ -53,10 +53,10 @@ class TestsForStyleConfig(yapf_test_helper.YAPFTest): for i in range(5): print('bar') """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) - self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(uwlines)) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, reformatter.Reformat(llines)) - def testOperatorStyle(self): + def testOperatorNoSpaceStyle(self): try: sympy_style = style.CreatePEP8Style() sympy_style['NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS'] = \ @@ -64,14 +64,131 @@ class TestsForStyleConfig(yapf_test_helper.YAPFTest): style.SetGlobalStyle(sympy_style) unformatted_code = textwrap.dedent("""\ a = 1+2 * 3 - 4 / 5 + b = '0' * 1 """) expected_formatted_code = textwrap.dedent("""\ a = 1 + 2*3 - 4/5 + b = '0'*1 """) - uwlines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) self.assertCodeEqual(expected_formatted_code, - reformatter.Reformat(uwlines)) + reformatter.Reformat(llines)) + finally: + style.SetGlobalStyle(style.CreatePEP8Style()) + style.DEFAULT_STYLE = self.current_style + + def testOperatorPrecedenceStyle(self): + try: + pep8_with_precedence = style.CreatePEP8Style() + pep8_with_precedence['ARITHMETIC_PRECEDENCE_INDICATION'] = True + style.SetGlobalStyle(pep8_with_precedence) + unformatted_code = textwrap.dedent("""\ + 1+2 + (1 + 2) * (3 - (4 / 5)) + a = 1 * 2 + 3 / 4 + b = 1 / 2 - 3 * 4 + c = (1 + 2) * (3 - 4) + d = (1 - 2) / (3 + 4) + e = 1 * 2 - 3 + f = 1 + 2 + 3 + 4 + g = 1 * 2 * 3 * 4 + h = 1 + 2 - 3 + 4 + i = 1 * 2 / 3 * 4 + j = (1 * 2 - 3) + 4 + k = (1 * 2 * 3) + (4 * 5 * 6 * 7 * 8) + """) + expected_formatted_code = textwrap.dedent("""\ + 1 + 2 + (1+2) * (3 - (4/5)) + a = 1*2 + 3/4 + b = 1/2 - 3*4 + c = (1+2) * (3-4) + d = (1-2) / (3+4) + e = 1*2 - 3 + f = 1 + 2 + 3 + 4 + g = 1 * 2 * 3 * 4 + h = 1 + 2 - 3 + 4 + i = 1 * 2 / 3 * 4 + j = (1*2 - 3) + 4 + k = (1*2*3) + (4*5*6*7*8) + """) + + llines = yapf_test_helper.ParseAndUnwrap(unformatted_code) + self.assertCodeEqual(expected_formatted_code, + reformatter.Reformat(llines)) + finally: + style.SetGlobalStyle(style.CreatePEP8Style()) + style.DEFAULT_STYLE = self.current_style + + def testNoSplitBeforeFirstArgumentStyle1(self): + try: + pep8_no_split_before_first = style.CreatePEP8Style() + pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False + pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = False + style.SetGlobalStyle(pep8_no_split_before_first) + formatted_code = textwrap.dedent("""\ + # Example from in-code MustSplit comments + foo = outer_function_call(fitting_inner_function_call(inner_arg1, inner_arg2), + outer_arg1, outer_arg2) + + foo = outer_function_call( + not_fitting_inner_function_call(inner_arg1, inner_arg2), outer_arg1, + outer_arg2) + + # Examples Issue#424 + a_super_long_version_of_print(argument1, argument2, argument3, argument4, + argument5, argument6, argument7) + + CREDS_FILE = os.path.join(os.path.expanduser('~'), + 'apis/super-secret-admin-creds.json') + + # Examples Issue#556 + i_take_a_lot_of_params(arg1, param1=very_long_expression1(), + param2=very_long_expression2(), + param3=very_long_expression3(), + param4=very_long_expression4()) + + # Examples Issue#590 + plt.plot(numpy.linspace(0, 1, 10), numpy.linspace(0, 1, 10), marker="x", + color="r") + + plt.plot(veryverylongvariablename, veryverylongvariablename, marker="x", + color="r") + """) # noqa + llines = yapf_test_helper.ParseAndUnwrap(formatted_code) + self.assertCodeEqual(formatted_code, reformatter.Reformat(llines)) + finally: + style.SetGlobalStyle(style.CreatePEP8Style()) + style.DEFAULT_STYLE = self.current_style + + def testNoSplitBeforeFirstArgumentStyle2(self): + try: + pep8_no_split_before_first = style.CreatePEP8Style() + pep8_no_split_before_first['SPLIT_BEFORE_FIRST_ARGUMENT'] = False + pep8_no_split_before_first['SPLIT_BEFORE_NAMED_ASSIGNS'] = True + style.SetGlobalStyle(pep8_no_split_before_first) + formatted_code = textwrap.dedent("""\ + # Examples Issue#556 + i_take_a_lot_of_params(arg1, + param1=very_long_expression1(), + param2=very_long_expression2(), + param3=very_long_expression3(), + param4=very_long_expression4()) + + # Examples Issue#590 + plt.plot(numpy.linspace(0, 1, 10), + numpy.linspace(0, 1, 10), + marker="x", + color="r") + + plt.plot(veryverylongvariablename, + veryverylongvariablename, + marker="x", + color="r") + """) + llines = yapf_test_helper.ParseAndUnwrap(formatted_code) + self.assertCodeEqual(formatted_code, reformatter.Reformat(llines)) finally: style.SetGlobalStyle(style.CreatePEP8Style()) style.DEFAULT_STYLE = self.current_style |