Skip to content

Commit

Permalink
Add tests for replace_newlines_with_spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
asmeurer committed Oct 7, 2024
1 parent c6ef6ca commit fc9281e
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion mypython/tests/test_processors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pyflakes.messages import UndefinedName, UnusedVariable, DuplicateArgument

from ..processors import get_pyflakes_warnings, SyntaxErrorMessage
from ..processors import (get_pyflakes_warnings, SyntaxErrorMessage,
replace_newlines_with_spaces)

def test_get_pyflakes_warnings():
warnings = get_pyflakes_warnings("""\
Expand Down Expand Up @@ -368,3 +369,75 @@ def test_get_pyflakes_warnings_skip():
a + b
""")
assert warnings == []

def test_replace_newlines_with_spaces():
# Test that an empty string returns an empty string.
assert replace_newlines_with_spaces('', 10) == ''

# Test that a single line without newlines remains unchanged.
text = "This is a single line."
assert replace_newlines_with_spaces(text, 10) == text

# Test multiple lines with varying lengths.
text = "Line one\nLine two is longer\nShort"
column_width = 15
expected = (
# 111111111
#123456789012345678
"Line one" + ' ' * 7 + # Padding to reach next multiple of 15
"Line two is longer" + ' ' * 12 + # Padding to reach next multiple
"Short"
)
result = replace_newlines_with_spaces(text, column_width)
assert result == expected

# Test lines where length is a multiple of column width.
text = "12345\n67890"
column_width = 5
expected = "12345" + "67890"
result = replace_newlines_with_spaces(text, column_width)
assert result == expected

# Test with very long lines to ensure correct padding.
text = "A" * 23 + "\n" + "B" * 17
column_width = 20
expected = (
"A" * 23 + ' ' * 17 + # Padding to reach next multiple of 20
"B" * 17
)
result = replace_newlines_with_spaces(text, column_width)
# Strip is used here to remove any unintended trailing spaces
assert result.strip() == expected.strip()

# Test with column width of 1.
text = "A\nB\nC"
column_width = 1
expected = "ABC"
result = replace_newlines_with_spaces(text, column_width)
assert result == expected

# Test text with no newlines to ensure it remains unchanged.
text = "Continuous text without newlines."
column_width = 10
assert replace_newlines_with_spaces(text, column_width) == text

# Test text that ends with a newline character.
text = "Line one\nLine two\n"
column_width = 10
expected = (
"Line one" + ' ' * 2 +
"Line two" + ' ' * 2
)
result = replace_newlines_with_spaces(text, column_width)
assert result == expected

# Test text with multiple consecutive newline characters.
text = "Line one\n\nLine three"
column_width = 10
expected = (
"Line one" + ' ' * 2 +
"" + ' ' * 10 + # Empty line with padding
"Line three"
)
result = replace_newlines_with_spaces(text, column_width)
assert result == expected

0 comments on commit fc9281e

Please sign in to comment.