Skip to content

Commit

Permalink
Merge pull request #756 from hhatto/refactor-unit-test
Browse files Browse the repository at this point in the history
Refactor unit test
  • Loading branch information
hhatto committed Jun 23, 2024
2 parents 24cea97 + a8126d5 commit 406f9ac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
6 changes: 3 additions & 3 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ def get_index_offset_contents(result, source):

def get_fixed_long_line(target, previous_line, original,
indent_word=' ', max_line_length=79,
aggressive=False, experimental=False, verbose=False):
aggressive=0, experimental=False, verbose=False):
"""Break up long line and return result.
Do this by generating multiple reformatted candidates and then
Expand Down Expand Up @@ -1896,7 +1896,7 @@ def _priority_key(pep8_result):


def shorten_line(tokens, source, indentation, indent_word, max_line_length,
aggressive=False, experimental=False, previous_line=''):
aggressive=0, experimental=False, previous_line=''):
"""Separate line at OPERATOR.
Multiple candidates will be yielded.
Expand Down Expand Up @@ -1934,7 +1934,7 @@ def shorten_line(tokens, source, indentation, indent_word, max_line_length,


def _shorten_line(tokens, source, indentation, indent_word,
aggressive=False, previous_line=''):
aggressive=0, previous_line=''):
"""Separate line at OPERATOR.
The input is expected to be free of newlines except for inside multiline
Expand Down
79 changes: 50 additions & 29 deletions test/test_autopep8.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
#!/usr/bin/env python
# coding: utf-8

"""Test suite for autopep8.
Unit tests go in "UnitTests". System tests go in "SystemTests".
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import re
import sys
Expand Down Expand Up @@ -54,8 +46,6 @@

class UnitTests(unittest.TestCase):

maxDiff = None

def test_compile_value_error(self):
source = '"\\xhh" \\'
self.assertFalse(autopep8.check_syntax(source))
Expand Down Expand Up @@ -275,19 +265,23 @@ def test_format_block_comments_should_only_touch_real_comments(self):
fix_e266(commented_out_code))

def test_fix_file(self):
self.assertIn(
'import ',
autopep8.fix_file(
filename=os.path.join(ROOT_DIR, 'test', 'example.py')))
ret = autopep8.fix_file(
filename=os.path.join(ROOT_DIR, 'test', 'example.py')
)
self.assertNotEqual(None, ret)
if ret is not None:
self.assertIn('import ', ret)

def test_fix_file_with_diff(self):
filename = os.path.join(ROOT_DIR, 'test', 'example.py')

self.assertIn(
'@@',
autopep8.fix_file(
filename=filename,
options=autopep8.parse_args(['--diff', filename])))
ret = autopep8.fix_file(
filename=filename,
options=autopep8.parse_args(['--diff', filename])
)
self.assertNotEqual(None, ret)
if ret is not None:
self.assertIn('@@', ret)

def test_fix_lines(self):
self.assertEqual(
Expand Down Expand Up @@ -814,9 +808,7 @@ def test_get_fixed_long_line_empty(self):
self.assertEqual(line, autopep8.get_fixed_long_line(line, line, line))


class SystemTests(unittest.TestCase):

maxDiff = None
class SystemTestsE1(unittest.TestCase):

def test_e101(self):
line = """\
Expand Down Expand Up @@ -1789,6 +1781,9 @@ def test_e131_invalid_indent_with_select_option(self):
with autopep8_context(line, options=['--select=E131']) as result:
self.assertEqual(fixed, result)


class SystemTestsE2(unittest.TestCase):

def test_e201(self):
line = '( 1)\n'
fixed = '(1)\n'
Expand Down Expand Up @@ -2292,6 +2287,9 @@ def test_e274(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)


class SystemTestsE3(unittest.TestCase):

def test_e306(self):
line = """
def test_descriptors(self):
Expand Down Expand Up @@ -2444,6 +2442,9 @@ def test_e305(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)


class SystemTestsE4(unittest.TestCase):

def test_e401(self):
line = 'import os, sys\n'
fixed = 'import os\nimport sys\n'
Expand Down Expand Up @@ -2626,6 +2627,9 @@ def f():
with autopep8_context(line) as result:
self.assertEqual(fixed, result)


class SystemTestsE5(unittest.TestCase):

def test_e501_basic(self):
line = """\
Expand Down Expand Up @@ -3967,6 +3971,9 @@ def test_e502(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)


class SystemTestsE7(unittest.TestCase):

def test_e701(self):
line = 'if True: print(True)\n'
fixed = 'if True:\n print(True)\n'
Expand Down Expand Up @@ -4477,6 +4484,9 @@ def test_e731_with_default_arguments(self):
with autopep8_context(line, options=['--select=E731']) as result:
self.assertEqual(fixed, result)


class SystemTestsE9(unittest.TestCase):

@unittest.skipIf(sys.version_info >= (3, 12), 'not detect in Python3.12+')
def test_e901_should_cause_indentation_screw_up(self):
line = """\
Expand All @@ -4502,6 +4512,9 @@ def test_should_preserve_vertical_tab(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)


class SystemTestsW1(unittest.TestCase):

def test_w191_should_ignore_multiline_strings(self):
line = """\
print(3 != 4, '''
Expand Down Expand Up @@ -4552,6 +4565,9 @@ def test_w191_should_ignore_tabs_in_strings(self):
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)


class SystemTestsW2(unittest.TestCase):

def test_w291(self):
line = "print('a b ')\t \n"
fixed = "print('a b ')\n"
Expand Down Expand Up @@ -4583,6 +4599,9 @@ def test_w293(self):
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)


class SystemTestsW3(unittest.TestCase):

def test_w391(self):
line = ' \n'
fixed = ''
Expand All @@ -4595,6 +4614,9 @@ def test_w391_more_complex(self):
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)


class SystemTestsW5(unittest.TestCase):

def test_w503(self):
line = '(width == 0\n + height == 0)\n'
fixed = '(width == 0 +\n height == 0)\n'
Expand Down Expand Up @@ -4908,6 +4930,9 @@ def test_w503_and_w504_conflict(self):
with autopep8_context(line, options=['-aa', '--select=E,W50']) as result:
self.assertEqual(fixed, result)


class SystemTestsW6(unittest.TestCase):

def test_w605_simple(self):
line = "escape = '\\.jpg'\n"
fixed = "escape = '\\\\.jpg'\n"
Expand Down Expand Up @@ -5424,8 +5449,6 @@ def test_get_module_imports_case_of_autopep8(self):

class CommandLineTests(unittest.TestCase):

maxDiff = None

def test_e122_and_e302_with_backslash(self):
line = """\
import sys
Expand Down Expand Up @@ -5490,7 +5513,7 @@ def test_diff_with_standard_in(self):

def test_indent_size_is_zero(self):
line = "'abc'\n"
with autopep8_subprocess(line, ['--indent-size=0']) as (result, retcode):
with autopep8_subprocess(line, ['--indent-size=0']) as (_, retcode):
self.assertEqual(retcode, autopep8.EXIT_CODE_ARGPARSE_ERROR)

def test_exit_code_with_io_error(self):
Expand Down Expand Up @@ -5650,7 +5673,7 @@ def test_parallel_jobs_with_diff_option(self):

with temporary_file_context(line) as filename_a:
with temporary_file_context(line) as filename_b:
files = list(set([filename_a, filename_b]))
files = list({filename_a, filename_b})
p = Popen(list(AUTOPEP8_CMD_TUPLE) + files +
['--jobs=3', '--diff'], stdout=PIPE)
p.wait()
Expand Down Expand Up @@ -6116,8 +6139,6 @@ def test_setupcfg_with_pycodestyle_config(self):

class ExperimentalSystemTests(unittest.TestCase):

maxDiff = None

def test_e501_experimental_basic(self):
line = """\
print(111, 111, 111, 111, 222, 222, 222, 222, 222, 222, 222, 222, 222, 333, 333, 333, 333)
Expand Down Expand Up @@ -7411,7 +7432,7 @@ def autopep8_subprocess(line, options, timeout=None):
p.kill()
raise Exception("subprocess is timed out")
_stdout, _ = p.communicate()
yield (_stdout.decode('utf-8'), p.returncode)
yield _stdout.decode('utf-8'), p.returncode


@contextlib.contextmanager
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist=py37,py38,py39,py310,py311,py312
envlist=py38,py39,py310,py311,py312
skip_missing_interpreters=True

[testenv]
Expand All @@ -8,5 +8,5 @@ commands=
python test/acid.py --aggressive test/example.py
python test/acid.py --compare-bytecode test/example.py
deps=
pycodestyle>=2.9.1
pycodestyle>=2.12.0
pydiff>=0.1.2

0 comments on commit 406f9ac

Please sign in to comment.