Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of unused alignment #132

Merged
merged 2 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,11 +1261,11 @@ def _handle_field(self, field):

# align "=" has been handled
if align == '<':
s = '%s%s+' % (s, fill)
s = '%s%s*' % (s, fill)
elif align == '>':
s = '%s*%s' % (fill, s)
elif align == '^':
s = '%s*%s%s+' % (fill, s, fill)
s = '%s*%s%s*' % (fill, s, fill)

return s

Expand Down
27 changes: 16 additions & 11 deletions test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,17 +865,22 @@ def test_pickling_bug_110(self):
# prior to the fix, this would raise an AttributeError
pickle.dumps(p)

def test_search_centered_bug_112(self):
r = parse.parse("{:^},{:^}", " 12 , 34 ")
self.assertEqual(r[1], "34")
r = parse.search("{:^},{:^}", " 12 , 34 ")
self.assertEqual(r[1], "34")

def test_search_left_align_bug_112(self):
r = parse.parse("{:<},{:<}", "12 ,34 ")
self.assertEqual(r[1], "34")
r = parse.search("{:<},{:<}", "12 ,34 ")
self.assertEqual(r[1], "34")
def test_unused_centered_alignment_bug(self):
r = parse.parse("{:^2S}", "foo")
self.assertEqual(r[0], "foo")
r = parse.search("{:^2S}", "foo")
self.assertEqual(r[0], "foo")

# specifically test for the case in issue #118 as well
r = parse.parse("Column {:d}:{:^}", "Column 1: Timestep")
self.assertEqual(r[0], 1)
self.assertEqual(r[1], "Timestep")

def test_unused_left_alignment_bug(self):
r = parse.parse("{:<2S}", "foo")
self.assertEqual(r[0], "foo")
r = parse.search("{:<2S}", "foo")
self.assertEqual(r[0], "foo")

def test_match_trailing_newline(self):
r = parse.parse('{}', 'test\n')
Expand Down