Skip to content

Commit 3a17154

Browse files
authored
Merge pull request #2223 from DimitriPapadopoulos/capitalized_fixes
Capitalize all suggested fixes
2 parents 9ccd878 + e8befc1 commit 3a17154

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

codespell_lib/_codespell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def is_text_file(filename):
495495

496496
def fix_case(word, fixword):
497497
if word == word.capitalize():
498-
return fixword.capitalize()
498+
return ', '.join(w.strip().capitalize() for w in fixword.split(','))
499499
elif word == word.upper():
500500
return fixword.upper()
501501
# they are both lower case
@@ -528,7 +528,7 @@ def ask_for_word_fix(line, wrongword, misspelling, interactivity):
528528
# we ask the user which word to use
529529

530530
r = ''
531-
opt = list(map(lambda x: x.strip(), misspelling.data.split(',')))
531+
opt = [w.strip() for w in misspelling.data.split(',')]
532532
while not r:
533533
print("%s Choose an option (blank for none): " % line, end='')
534534
for i in range(len(opt)):

codespell_lib/tests/test_basic.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,63 @@ def test_case_handling(tmpdir, capsys):
393393
assert f.read().decode('utf-8') == 'this has an ASCII error'
394394

395395

396+
def _helper_test_case_handling_in_fixes(tmpdir, capsys, reason):
397+
d = str(tmpdir)
398+
399+
with open(op.join(d, 'dictionary.txt'), 'w') as f:
400+
if reason:
401+
f.write('adoptor->adopter, adaptor, reason\n')
402+
else:
403+
f.write('adoptor->adopter, adaptor,\n')
404+
dictionary_name = f.name
405+
406+
# the mispelled word is entirely lowercase
407+
with open(op.join(d, 'bad.txt'), 'w') as f:
408+
f.write('early adoptor\n')
409+
code, stdout, _ = cs.main('-D', dictionary_name, f.name, std=True)
410+
# all suggested fixes must be lowercase too
411+
assert 'adopter, adaptor' in stdout
412+
# the reason, if any, must not be modified
413+
if reason:
414+
assert 'reason' in stdout
415+
416+
# the mispelled word is capitalized
417+
with open(op.join(d, 'bad.txt'), 'w') as f:
418+
f.write('Early Adoptor\n')
419+
code, stdout, _ = cs.main('-D', dictionary_name, f.name, std=True)
420+
# all suggested fixes must be capitalized too
421+
assert 'Adopter, Adaptor' in stdout
422+
# the reason, if any, must not be modified
423+
if reason:
424+
assert 'reason' in stdout
425+
426+
# the mispelled word is entirely uppercase
427+
with open(op.join(d, 'bad.txt'), 'w') as f:
428+
f.write('EARLY ADOPTOR\n')
429+
code, stdout, _ = cs.main('-D', dictionary_name, f.name, std=True)
430+
# all suggested fixes must be uppercase too
431+
assert 'ADOPTER, ADAPTOR' in stdout
432+
# the reason, if any, must not be modified
433+
if reason:
434+
assert 'reason' in stdout
435+
436+
# the mispelled word mixes lowercase and uppercase
437+
with open(op.join(d, 'bad.txt'), 'w') as f:
438+
f.write('EaRlY AdOpToR\n')
439+
code, stdout, _ = cs.main('-D', dictionary_name, f.name, std=True)
440+
# all suggested fixes should be lowercase
441+
assert 'adopter, adaptor' in stdout
442+
# the reason, if any, must not be modified
443+
if reason:
444+
assert 'reason' in stdout
445+
446+
447+
def test_case_handling_in_fixes(tmpdir, capsys):
448+
"""Test that the case of fixes is similar to the mispelled word."""
449+
_helper_test_case_handling_in_fixes(tmpdir, capsys, reason=False)
450+
_helper_test_case_handling_in_fixes(tmpdir, capsys, reason=True)
451+
452+
396453
def test_context(tmpdir, capsys):
397454
"""Test context options."""
398455
d = str(tmpdir)

0 commit comments

Comments
 (0)