Skip to content

Commit 8fa3b71

Browse files
committed
fix: handle mixed em dash + hyphen combos correctly
1 parent 66eeafc commit 8fa3b71

2 files changed

Lines changed: 70 additions & 12 deletions

File tree

src/cogs/commands/karma.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@
3737
)
3838
from src.core.functions.karma import build_leaderboard_view, build_team_leaderboard_view
3939

40-
# Matches @user or @role followed by 2+ plus signs, 2+ minus signs, or 1+ em dashes
41-
KARMA_PATTERN = re.compile(r"<@[!&]?(\d+)>\s*(\+{2,}|-{2,}|\u2014+)")
40+
# Matches @user or @role followed by 2+ plus signs, or a mix of hyphens/em dashes (min 2 "units")
41+
# Em dashes count as 2 hyphens each, so a single em dash meets the minimum.
42+
KARMA_PATTERN = re.compile(r"<@[!&]?(\d+)>\s*(\+{2,}|[\-\u2014]{1,})")
43+
44+
def _count_minus(signs: str) -> int:
45+
"""Count the effective minus value from a mix of hyphens and em dashes."""
46+
return sum(2 if c == "\u2014" else 1 for c in signs)
4247

4348

4449
class Karma(commands.Cog):
@@ -181,10 +186,11 @@ async def _resolve_targets(
181186
signs = raw_match.group(2)
182187
if signs[0] == "+":
183188
amount = len(signs)
184-
elif signs[0] == "\u2014": # em dash — counts as 2 each
185-
amount = -len(signs) * 2
186189
else:
187-
amount = -len(signs)
190+
minus_count = _count_minus(signs)
191+
if minus_count < 2:
192+
continue
193+
amount = -minus_count
188194
is_role = raw_match.group(0).startswith("<@&")
189195

190196
if is_role and mention_id in team_role_ids:

tests/test_karma_pattern.py

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
"""Tests for the karma regex pattern matching."""
1+
"""Tests for the karma regex pattern and _count_minus helper."""
22

3-
from src.cogs.commands.karma import KARMA_PATTERN
3+
from src.cogs.commands.karma import KARMA_PATTERN, _count_minus
44

55

6+
# ── Pattern matching ─────────────────────────────────────────────
7+
68
def test_matches_user_plus():
79
matches = KARMA_PATTERN.findall("<@123456> +++")
810
assert len(matches) == 1
@@ -38,9 +40,12 @@ def test_no_match_single_plus():
3840
assert len(matches) == 0
3941

4042

41-
def test_no_match_single_minus():
43+
def test_single_minus_matches_but_filtered_by_count():
44+
"""Single hyphen matches the regex, but _count_minus returns 1 (< 2 min), so
45+
_resolve_targets will skip it."""
4246
matches = KARMA_PATTERN.findall("<@123456> -")
43-
assert len(matches) == 0
47+
assert len(matches) == 1 # regex matches
48+
assert _count_minus(matches[0][1]) < 2 # but below minimum
4449

4550

4651
def test_multiple_mentions():
@@ -57,18 +62,65 @@ def test_mixed_plus_minus():
5762
assert matches[1] == ("222", "---")
5863

5964

60-
def test_matches_em_dash():
65+
def test_matches_single_em_dash():
66+
"""Single em dash = 2 units, should match."""
6167
matches = KARMA_PATTERN.findall("<@123456> \u2014")
6268
assert len(matches) == 1
63-
assert matches[0] == ("123456", "\u2014")
6469

6570

6671
def test_matches_multiple_em_dashes():
6772
matches = KARMA_PATTERN.findall("<@123456> \u2014\u2014\u2014")
6873
assert len(matches) == 1
69-
assert matches[0] == ("123456", "\u2014\u2014\u2014")
74+
75+
76+
def test_matches_em_dash_plus_hyphen():
77+
"""— followed by - should match as one group."""
78+
matches = KARMA_PATTERN.findall("<@123456> \u2014-")
79+
assert len(matches) == 1
80+
81+
82+
def test_matches_hyphen_plus_em_dash():
83+
"""- followed by — should match as one group."""
84+
matches = KARMA_PATTERN.findall("<@123456> -\u2014")
85+
assert len(matches) == 1
86+
87+
88+
def test_matches_hyphens_plus_em_dash():
89+
"""-- followed by — should match as one group."""
90+
matches = KARMA_PATTERN.findall("<@123456> --\u2014")
91+
assert len(matches) == 1
7092

7193

7294
def test_no_match_plain_text():
7395
matches = KARMA_PATTERN.findall("hello world")
7496
assert len(matches) == 0
97+
98+
99+
# ── _count_minus helper ─────────────────────────────────────────
100+
101+
def test_count_minus_hyphens():
102+
assert _count_minus("---") == 3
103+
104+
105+
def test_count_minus_em_dashes():
106+
assert _count_minus("\u2014\u2014") == 4
107+
108+
109+
def test_count_minus_mixed_em_hyphen():
110+
"""— followed by - = 2 + 1 = 3."""
111+
assert _count_minus("\u2014-") == 3
112+
113+
114+
def test_count_minus_mixed_hyphen_em():
115+
"""- followed by — = 1 + 2 = 3."""
116+
assert _count_minus("-\u2014") == 3
117+
118+
119+
def test_count_minus_single_em_dash():
120+
"""Single em dash = 2."""
121+
assert _count_minus("\u2014") == 2
122+
123+
124+
def test_count_minus_single_hyphen():
125+
"""Single hyphen = 1."""
126+
assert _count_minus("-") == 1

0 commit comments

Comments
 (0)