Skip to content

Commit eb592b3

Browse files
committed
Enhance firstinitial function
to check letter index before comma and add unit tests for name helpers
1 parent 5537d3f commit eb592b3

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎lines/firstname.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def firstinitial(allnames):
1919
spaceIndex=(letterIndex-1)
2020
# print('test:')
2121
# print(allnames[commaIndex])
22-
if (allnames[commaIndex]==','):
22+
if letterIndex >= 2 and allnames[commaIndex] == ',':
2323
# if ((letterIndex-2)=='/,'):
2424
# print('comma 2 ago')
2525
if (allnames[spaceIndex]==' '):

‎linesRis/firstname.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ def firstinitial(allnames):
1919
spaceIndex=(letterIndex-1)
2020
# print('test:')
2121
# print(allnames[commaIndex])
22-
if (allnames[commaIndex]==','):
22+
if letterIndex >= 2 and allnames[commaIndex] == ',':
2323
# if ((letterIndex-2)=='/,'):
2424
# print('comma 2 ago')
2525
if (allnames[spaceIndex]==' '):
2626
# print('space one ago')
2727
# print('letterIndex',letterIndex)
28-
firstInitialIndices
28+
firstInitialIndices.append(letterIndex)
2929
# firstinitial=allnames[0]
3030
return firstInitialIndices
3131

‎tests/test_firstname.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Check name helpers in both the BibTeX and RIS implementations."""
2+
3+
from pathlib import Path
4+
import runpy
5+
6+
import pytest
7+
8+
9+
@pytest.fixture(params=["lines", "linesRis"])
10+
def helpers(request):
11+
root = Path(__file__).resolve().parents[1]
12+
return runpy.run_path(str(root / request.param / "firstname.py"))
13+
14+
15+
@pytest.mark.parametrize("names, expected", [
16+
("Doe, Jane", [5]),
17+
("Doe, Jane and Smith, John", [5, 21]),
18+
("Kniesel, Günter", [9]),
19+
("Jane Doe", []),
20+
("", []),
21+
("J", []),
22+
("Doe, ", []),
23+
])
24+
def test_first_initial_positions(helpers, names, expected):
25+
assert helpers["firstinitial"](names) == expected
26+
27+
28+
@pytest.mark.parametrize("names, indices, expected", [
29+
("Doe, Jane and Smith, John", [3, 19], [",", ","]),
30+
("Jane Doe", [], []),
31+
("", [], []),
32+
])
33+
def test_characters_at_comma_positions(helpers, names, indices, expected):
34+
assert helpers["printFromCommaIndex"](names, indices) == expected

0 commit comments

Comments
 (0)