|
| 1 | +from nameparser import HumanName |
| 2 | +from tests.base import HumanNameTestBase |
| 3 | + |
| 4 | + |
| 5 | +class FirstNamePrefixesTestCase(HumanNameTestBase): |
| 6 | + |
| 7 | + def test_is_first_name_prefix_true(self) -> None: |
| 8 | + hn = HumanName("test") |
| 9 | + assert hn.is_first_name_prefix("Abdul") |
| 10 | + |
| 11 | + def test_is_first_name_prefix_false(self) -> None: |
| 12 | + hn = HumanName("test") |
| 13 | + assert not hn.is_first_name_prefix("Ahmed") |
| 14 | + |
| 15 | + # --- no-comma: basic joining --- |
| 16 | + def test_no_comma_basic_join(self) -> None: |
| 17 | + hn = HumanName("abdul salam ahmed salem") |
| 18 | + self.m(hn.first, "abdul salam", hn) |
| 19 | + self.m(hn.middle, "ahmed", hn) |
| 20 | + self.m(hn.last, "salem", hn) |
| 21 | + |
| 22 | + def test_no_comma_three_tokens_no_middle(self) -> None: |
| 23 | + hn = HumanName("abdul salam salem") |
| 24 | + self.m(hn.first, "abdul salam", hn) |
| 25 | + self.m(hn.middle, "", hn) |
| 26 | + self.m(hn.last, "salem", hn) |
| 27 | + |
| 28 | + def test_no_comma_guard_two_tokens_no_join(self) -> None: |
| 29 | + """Guard: only last name remains after prefix → no join.""" |
| 30 | + hn = HumanName("abdul salam") |
| 31 | + self.m(hn.first, "abdul", hn) |
| 32 | + self.m(hn.last, "salam", hn) |
| 33 | + |
| 34 | + def test_no_comma_guard_suffix_not_swallowed(self) -> None: |
| 35 | + """Guard: prefix + one name + suffix — suffix must not become last.""" |
| 36 | + hn = HumanName("abdul salam jr") |
| 37 | + self.m(hn.first, "abdul", hn) |
| 38 | + self.m(hn.last, "salam", hn) |
| 39 | + self.m(hn.suffix, "jr", hn) |
| 40 | + |
| 41 | + # --- lastname-comma path --- |
| 42 | + def test_lastname_comma_join(self) -> None: |
| 43 | + hn = HumanName("salem, abdul salam") |
| 44 | + self.m(hn.first, "abdul salam", hn) |
| 45 | + self.m(hn.middle, "", hn) |
| 46 | + self.m(hn.last, "salem", hn) |
| 47 | + |
| 48 | + def test_lastname_comma_join_with_middle(self) -> None: |
| 49 | + hn = HumanName("salem, abdul salam ahmed") |
| 50 | + self.m(hn.first, "abdul salam", hn) |
| 51 | + self.m(hn.middle, "ahmed", hn) |
| 52 | + self.m(hn.last, "salem", hn) |
| 53 | + |
| 54 | + # --- interaction with titles --- |
| 55 | + def test_title_kept_prefix_joins(self) -> None: |
| 56 | + hn = HumanName("Dr. abdul salam ahmed salem") |
| 57 | + self.m(hn.title, "Dr.", hn) |
| 58 | + self.m(hn.first, "abdul salam", hn) |
| 59 | + self.m(hn.middle, "ahmed", hn) |
| 60 | + self.m(hn.last, "salem", hn) |
| 61 | + |
| 62 | + # --- interaction with last-name prefixes --- |
| 63 | + def test_abu_bakr_al_baghdadi(self) -> None: |
| 64 | + """abu joins forward as first-prefix; al joins forward as last-prefix.""" |
| 65 | + hn = HumanName("abu bakr al baghdadi") |
| 66 | + self.m(hn.first, "abu bakr", hn) |
| 67 | + self.m(hn.last, "al baghdadi", hn) |
| 68 | + |
| 69 | + # --- interaction with suffixes --- |
| 70 | + def test_suffix_kept_prefix_joins(self) -> None: |
| 71 | + hn = HumanName("abdul salam ahmed salem jr") |
| 72 | + self.m(hn.first, "abdul salam", hn) |
| 73 | + self.m(hn.middle, "ahmed", hn) |
| 74 | + self.m(hn.last, "salem", hn) |
| 75 | + self.m(hn.suffix, "jr", hn) |
| 76 | + |
| 77 | + # --- guard / no-op --- |
| 78 | + def test_mohamad_unchanged(self) -> None: |
| 79 | + """mohamad is deliberately not in first_name_prefixes.""" |
| 80 | + hn = HumanName("Mohamad Ali Khalil") |
| 81 | + self.m(hn.first, "Mohamad", hn) |
| 82 | + self.m(hn.middle, "Ali", hn) |
| 83 | + self.m(hn.last, "Khalil", hn) |
| 84 | + |
| 85 | + def test_single_token_already_joined_unchanged(self) -> None: |
| 86 | + """abdulsalam is one token — not in the set, no join.""" |
| 87 | + hn = HumanName("abdulsalam ahmed salem") |
| 88 | + self.m(hn.first, "abdulsalam", hn) |
| 89 | + self.m(hn.middle, "ahmed", hn) |
| 90 | + self.m(hn.last, "salem", hn) |
| 91 | + |
| 92 | + def test_prefix_alone_no_join(self) -> None: |
| 93 | + """Single-word name that is a prefix: nothing to join.""" |
| 94 | + hn = HumanName("abdul") |
| 95 | + self.m(hn.first, "abdul", hn) |
| 96 | + |
| 97 | + def test_lastname_comma_prefix_only_no_join(self) -> None: |
| 98 | + """Prefix as sole post-comma token: nothing to join.""" |
| 99 | + hn = HumanName("salem, abdul") |
| 100 | + self.m(hn.first, "abdul", hn) |
| 101 | + self.m(hn.last, "salem", hn) |
| 102 | + |
| 103 | + def test_mid_name_prefix_becomes_last_prefix(self) -> None: |
| 104 | + """abu in non-first position is handled as a last-name prefix, not first-name.""" |
| 105 | + hn = HumanName("ahmed abu bakr") |
| 106 | + self.m(hn.first, "ahmed", hn) |
| 107 | + self.m(hn.last, "abu bakr", hn) |
| 108 | + |
| 109 | + # --- opt-out --- |
| 110 | + def test_opt_out_via_clear(self) -> None: |
| 111 | + """Clearing first_name_prefixes restores prior behavior.""" |
| 112 | + from nameparser.config import Constants |
| 113 | + c = Constants(first_name_prefixes=set()) |
| 114 | + hn = HumanName("abdul salam ahmed salem", constants=c) |
| 115 | + self.m(hn.first, "abdul", hn) |
| 116 | + self.m(hn.middle, "salam ahmed", hn) |
| 117 | + self.m(hn.last, "salem", hn) |
| 118 | + |
0 commit comments