Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
myint committed May 16, 2013
1 parent 9da2ce0 commit 263bf20
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 45 deletions.
8 changes: 4 additions & 4 deletions docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ def format_docstring(indentation, docstring,
{description}{post_description}
{indentation}"""\
'''.format(pre_summary=('\n' + indentation if pre_summary_newline
else unicode()),
else ''),
summary=wrap_summary(normalize_summary(summary),
wrap_length=summary_wrap_length,
initial_indent=initial_indent,
subsequent_indent=indentation).lstrip(),
description='\n'.join([indent_non_indented(l, indentation).rstrip()
for l in description.splitlines()]),
post_description=('\n' if post_description_blank else unicode()),
post_description=('\n' if post_description_blank else ''),
indentation=indentation)
else:
return wrap_summary('"""' + normalize_summary(contents) + '"""',
Expand Down Expand Up @@ -219,7 +219,7 @@ def split_summary_and_description(contents):
if len(split) == 2:
return (split[0].strip(), (token + split[1]).strip())

return (split[0].strip(), unicode())
return (split[0].strip(), '')


def strip_docstring(docstring):
Expand Down Expand Up @@ -303,7 +303,7 @@ def format_file(filename, args, standard_out):
'before/' + filename,
'after/' + filename,
lineterm='')
standard_out.write(unicode('\n').join(list(diff) + ['']))
standard_out.write('\n'.join(list(diff) + ['']))


def main(argv, standard_out, standard_error):
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python

"""Setup for docformatter."""

from __future__ import unicode_literals

import ast
from distutils import core

Expand Down
78 changes: 37 additions & 41 deletions test_docformatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

"""Test suite for docformatter."""

from __future__ import unicode_literals

import contextlib
import io
import tempfile
Expand All @@ -15,12 +17,6 @@
import docformatter


try:
unicode
except NameError:
unicode = str


class TestUnits(unittest.TestCase):

def test_strip_docstring(self):
Expand Down Expand Up @@ -193,12 +189,12 @@ def foo():
"""Hello foo."""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
"""
Hello foo.
"""
''')))
'''))

def test_format_code_with_empty_string(self):
self.assertEqual(
Expand All @@ -214,14 +210,14 @@ def foo():
\t\tx = 1
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
\t"""
\tHello foo.
\t"""
\tif True:
\t\tx = 1
''')))
'''))

def test_format_code_with_mixed_tabs(self):
self.assertEqual(
Expand All @@ -232,14 +228,14 @@ def foo():
\t x = 1
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
\t"""
\tHello foo.
\t"""
\tif True:
\t x = 1
''')))
'''))

def test_format_code_with_escaped_newlines(self):
self.assertEqual(
Expand All @@ -249,13 +245,13 @@ def test_format_code_with_escaped_newlines(self):
1
''',
docformatter.format_code(
unicode(r'''def foo():
r'''def foo():
"""
Hello foo.
"""
x = \
1
''')))
'''))

def test_format_code_with_comments(self):
self.assertEqual(
Expand All @@ -267,15 +263,15 @@ def foo():
123
'''.lstrip(),
docformatter.format_code(
unicode(r'''
r'''
def foo():
"""
Hello foo.
"""
# My comment
# My comment with escape \
123
'''.lstrip())))
'''.lstrip()))

def test_format_code_skip_complex(self):
"""We do not handle r/u/b prefixed strings."""
Expand All @@ -287,12 +283,12 @@ def foo():
"""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
r"""
Hello foo.
"""
''')))
'''))

def test_format_code_skip_complex_single(self):
"""We do not handle r/u/b prefixed strings."""
Expand All @@ -304,19 +300,19 @@ def foo():
'''
""",
docformatter.format_code(
unicode("""\
"""\
def foo():
r'''
Hello foo.
'''
""")))
"""))

def test_format_code_skip_nested(self):
code = unicode("""\
code = """\
def foo():
'''Hello foo. \"\"\"abc\"\"\"
'''
""")
"""
self.assertEqual(code, docformatter.format_code(code))

def test_format_code_with_multiple_sentences(self):
Expand All @@ -330,13 +326,13 @@ def foo():
"""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
"""
Hello foo.
This is a docstring.
"""
''')))
'''))

def test_format_code_with_multiple_sentences_same_line(self):
self.assertEqual(
Expand All @@ -349,12 +345,12 @@ def foo():
"""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
"""
Hello foo. This is a docstring.
"""
''')))
'''))

def test_format_code_with_multiple_sentences_multiline_summary(self):
self.assertEqual(
Expand All @@ -367,13 +363,13 @@ def foo():
"""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
"""
Hello
foo. This is a docstring.
"""
''')))
'''))

def test_format_code_with_empty_lines(self):
self.assertEqual(
Expand All @@ -388,15 +384,15 @@ def foo():
"""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
"""
Hello
foo. This is a docstring.
More stuff.
"""
''')))
'''))

def test_format_code_with_trailing_whitespace(self):
self.assertEqual(
Expand All @@ -411,21 +407,21 @@ def foo():
"""
''',
docformatter.format_code(
(unicode('''\
('''\
def foo():
"""
Hello
foo. This is a docstring.\t
More stuff.\t
"""
'''))))
''')))

def test_format_code_with_no_docstring(self):
line = unicode('''\
line = '''\
def foo():
"Just a regular string"
''')
'''
self.assertEqual(line, docformatter.format_code(line))

def test_format_code_with_assignment_on_first_line(self):
Expand All @@ -435,10 +431,10 @@ def foo():
x = """Just a regular string. Alpha."""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
x = """Just a regular string. Alpha."""
''')))
'''))

def test_format_code_with_regular_strings_too(self):
self.assertEqual(
Expand All @@ -459,7 +455,7 @@ def foo():
touched\t"""
''',
docformatter.format_code(
unicode('''\
'''\
def foo():
"""
Hello
Expand All @@ -473,11 +469,11 @@ def foo():
"""More stuff
that should not be
touched\t"""
''')))
'''))

def test_format_code_with_syntax_error(self):
self.assertEqual('"""\n',
docformatter.format_code(unicode('"""\n')))
docformatter.format_code('"""\n'))

def test_split_summary_and_description(self):
self.assertEqual(('This is the first.',
Expand Down Expand Up @@ -553,14 +549,14 @@ def foo():
docformatter.main(argv=['my_fake_program', filename],
standard_out=output_file,
standard_error=None)
self.assertEqual(unicode('''\
self.assertEqual('''\
@@ -1,4 +1,2 @@
def foo():
- """
- Hello world
- """
+ """Hello world."""
'''), '\n'.join(output_file.getvalue().split('\n')[2:]))
''', '\n'.join(output_file.getvalue().split('\n')[2:]))

def test_diff_with_nonexistent_file(self):
output_file = io.StringIO()
Expand Down

0 comments on commit 263bf20

Please sign in to comment.