From 92605e7a4ddfa61a9aa05102b0880e3f0cc1a45e Mon Sep 17 00:00:00 2001 From: Nyan404 Date: Sun, 16 Aug 2026 23:03:14 +0300 Subject: [PATCH] Natural form fixes --- app/belki/reports.py | 26 +++++++++++++++++++++++--- tests/test_belki.py | 8 ++++---- tests/test_vk_conversation.py | 2 +- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/belki/reports.py b/app/belki/reports.py index 9ecff60..ca85812 100644 --- a/app/belki/reports.py +++ b/app/belki/reports.py @@ -17,6 +17,26 @@ def _number(value: float | int) -> str: return str(value).rstrip("0").rstrip(".") if isinstance(value, float) else str(value) +def _points_phrase(value: float | int) -> str: + """Return a number with the natural Russian form of «балл».""" + number = _number(value) + if isinstance(value, float) and not value.is_integer(): + word = "балла" + else: + integer = abs(int(value)) + last_two = integer % 100 + last = integer % 10 + if 11 <= last_two <= 14: + word = "баллов" + elif last == 1: + word = "балл" + elif last in (2, 3, 4): + word = "балла" + else: + word = "баллов" + return f"{number} {word}" + + def render_coaching(session: CoachingSession, result: CoachingResult) -> str: hashtag = "#лазание" if session.skill == "ЛУ" else "#зоркость" kitten_text = f"{session.kitten_name} [{session.kitten_id}]" @@ -28,8 +48,8 @@ def render_coaching(session: CoachingSession, result: CoachingResult) -> str: f"прокачать {session.skill}.", f"Время: {format_clock(parse_clock(session.start))} – " f"{format_clock(parse_clock(session.end))} [{result.minutes} минут].", - f"Баллы белки: {_number(result.escort_points)} баллы.", - f"Баллы бельчонка: {_number(result.kitten_points)} баллы.", + f"Баллы белки: {_points_phrase(result.escort_points)}.", + f"Баллы бельчонка: {_points_phrase(result.kitten_points)}.", ] return "\n".join(lines) @@ -61,5 +81,5 @@ def render_inspection(report: InspectionReport, result: InspectionResult | None ) else: lines.append("0") - lines.append(f"Баллы осмотра: {result.points} баллы.") + lines.append(f"Баллы осмотра: {_points_phrase(result.points)}.") return "\n".join(lines) diff --git a/tests/test_belki.py b/tests/test_belki.py index 6f5b9e6..d2678a6 100644 --- a/tests/test_belki.py +++ b/tests/test_belki.py @@ -134,8 +134,8 @@ class ReportTests(unittest.TestCase): self.assertIn("#лазание", text) self.assertIn("Дикая Вишня [1604614], помог(-ла) Серолап [1617140] прокачать ЛУ.", text) self.assertIn("Время: 11:00 – 11:20 [20 минут].", text) - self.assertIn("Баллы белки: 6 баллы.", text) - self.assertIn("Баллы бельчонка: 4 баллы.", text) + self.assertIn("Баллы белки: 6 баллов.", text) + self.assertIn("Баллы бельчонка: 4 балла.", text) self.assertNotIn("Скриншот", text) self.assertNotIn("истории", text) @@ -159,7 +159,7 @@ class ReportTests(unittest.TestCase): self.assertIn("+2 вьюнок.", text) self.assertIn("+1 паук.", text) self.assertNotIn("Скриншот", text) - self.assertIn("Баллы осмотра: 15 баллы.", text) + self.assertIn("Баллы осмотра: 15 баллов.", text) def test_spider_and_web_have_different_values(self): report = InspectionReport( @@ -175,7 +175,7 @@ class ReportTests(unittest.TestCase): resources=(("паутина", 1), ("надпись", 1)), ) self.assertEqual(calculate_inspection(report).points, 11) - self.assertIn("Баллы осмотра: 11 баллы.", render_inspection(report)) + self.assertIn("Баллы осмотра: 11 баллов.", render_inspection(report)) def test_inspection_rejects_negative_resources(self): with self.assertRaises(ValueError): diff --git a/tests/test_vk_conversation.py b/tests/test_vk_conversation.py index c883992..e690086 100644 --- a/tests/test_vk_conversation.py +++ b/tests/test_vk_conversation.py @@ -30,7 +30,7 @@ class VkConversationTests(unittest.TestCase): confirmation = self.add_screenshot(dialog, 77) self.assertIn("подтверд", confirmation.lower()) report = dialog.handle(77, "да") - self.assertIn("Баллы белки: 4 баллы.", report) + self.assertIn("Баллы белки: 4 балла.", report) def test_step_by_step_coaching_accepts_comma_separated_kittens(self): dialog = BelkiDialog()