style: Format test_coder.py

This commit is contained in:
Paul Gauthier (aider) 2025-05-10 11:46:27 -07:00
parent 1127b8b559
commit ea1189b8ec

View file

@ -1,6 +1,6 @@
import locale
import os import os
import tempfile import tempfile
import locale
import unittest import unittest
from pathlib import Path from pathlib import Path
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
@ -1204,7 +1204,9 @@ This command will print 'Hello, World!' to the console."""
self.assertEqual(coder.normalize_language("es"), "Spanish") self.assertEqual(coder.normalize_language("es"), "Spanish")
self.assertEqual(coder.normalize_language("de_DE.UTF-8"), "German") self.assertEqual(coder.normalize_language("de_DE.UTF-8"), "German")
self.assertEqual(coder.normalize_language("ja"), "Japanese") self.assertEqual(coder.normalize_language("ja"), "Japanese")
self.assertEqual(coder.normalize_language("unknown_code"), "unknown_code") # Fallback to original self.assertEqual(
coder.normalize_language("unknown_code"), "unknown_code"
) # Fallback to original
# Test with babel.Locale mocked (available) # Test with babel.Locale mocked (available)
mock_babel_locale = MagicMock() mock_babel_locale = MagicMock()
@ -1239,7 +1241,9 @@ This command will print 'Hello, World!' to the console."""
# 2. Test with locale.getlocale() # 2. Test with locale.getlocale()
with patch("locale.getlocale", return_value=("en_GB", "UTF-8")) as mock_getlocale: with patch("locale.getlocale", return_value=("en_GB", "UTF-8")) as mock_getlocale:
with patch.object(coder, "normalize_language", return_value="British English") as mock_norm: with patch.object(
coder, "normalize_language", return_value="British English"
) as mock_norm:
self.assertEqual(coder.get_user_language(), "British English") self.assertEqual(coder.get_user_language(), "British English")
mock_getlocale.assert_called_once() mock_getlocale.assert_called_once()
mock_norm.assert_called_once_with("en_GB") mock_norm.assert_called_once_with("en_GB")
@ -1254,7 +1258,9 @@ This command will print 'Hello, World!' to the console."""
env_vars_to_test = ["LANG", "LANGUAGE", "LC_ALL", "LC_MESSAGES"] env_vars_to_test = ["LANG", "LANGUAGE", "LC_ALL", "LC_MESSAGES"]
# Test LANG # Test LANG
with patch("locale.getlocale", side_effect=Exception("locale error")): # Mock locale to fail with patch(
"locale.getlocale", side_effect=Exception("locale error")
): # Mock locale to fail
with patch("os.environ.get") as mock_env_get: with patch("os.environ.get") as mock_env_get:
mock_env_get.side_effect = lambda key: "de_DE.UTF-8" if key == "LANG" else None mock_env_get.side_effect = lambda key: "de_DE.UTF-8" if key == "LANG" else None
with patch.object(coder, "normalize_language", return_value="German") as mock_norm: with patch.object(coder, "normalize_language", return_value="German") as mock_norm:
@ -1276,7 +1282,9 @@ This command will print 'Hello, World!' to the console."""
coder.chat_language = "it_IT" coder.chat_language = "it_IT"
with patch("locale.getlocale", return_value=("en_US", "UTF-8")) as mock_getlocale: with patch("locale.getlocale", return_value=("en_US", "UTF-8")) as mock_getlocale:
with patch("os.environ.get", return_value="de_DE") as mock_env_get: with patch("os.environ.get", return_value="de_DE") as mock_env_get:
with patch.object(coder, "normalize_language", side_effect=lambda x: x.upper()) as mock_norm: with patch.object(
coder, "normalize_language", side_effect=lambda x: x.upper()
) as mock_norm:
self.assertEqual(coder.get_user_language(), "IT_IT") # From chat_language self.assertEqual(coder.get_user_language(), "IT_IT") # From chat_language
mock_norm.assert_called_once_with("it_IT") mock_norm.assert_called_once_with("it_IT")
mock_getlocale.assert_not_called() mock_getlocale.assert_not_called()