MCPcopy Index your code
hub / github.com/Aider-AI/aider / test_get_user_language

Method test_get_user_language

tests/basic/test_coder.py:1265–1328  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1263 self.assertEqual(coder.normalize_language("en_US"), "English") # Falls back to map
1264
1265 def test_get_user_language(self):
1266 io = InputOutput()
1267 coder = Coder.create(self.GPT35, None, io=io)
1268
1269 # 1. Test with self.chat_language set
1270 coder.chat_language = "fr_CA"
1271 with patch.object(coder, "normalize_language", return_value="French Canadian") as mock_norm:
1272 self.assertEqual(coder.get_user_language(), "French Canadian")
1273 mock_norm.assert_called_once_with("fr_CA")
1274 coder.chat_language = None # Reset
1275
1276 # 2. Test with locale.getlocale()
1277 with patch("locale.getlocale", return_value=("en_GB", "UTF-8")) as mock_getlocale:
1278 with patch.object(
1279 coder, "normalize_language", return_value="British English"
1280 ) as mock_norm:
1281 self.assertEqual(coder.get_user_language(), "British English")
1282 mock_getlocale.assert_called_once()
1283 mock_norm.assert_called_once_with("en_GB")
1284
1285 # Test with locale.getlocale() returning None or empty
1286 with patch("locale.getlocale", return_value=(None, None)) as mock_getlocale:
1287 with patch("os.environ.get") as mock_env_get: # Ensure env vars are not used yet
1288 mock_env_get.return_value = None
1289 self.assertIsNone(coder.get_user_language()) # Should be None if nothing found
1290
1291 # 3. Test with environment variables: LANG
1292 with patch(
1293 "locale.getlocale", side_effect=Exception("locale error")
1294 ): # Mock locale to fail
1295 with patch("os.environ.get") as mock_env_get:
1296 mock_env_get.side_effect = lambda key: "de_DE.UTF-8" if key == "LANG" else None
1297 with patch.object(coder, "normalize_language", return_value="German") as mock_norm:
1298 self.assertEqual(coder.get_user_language(), "German")
1299 mock_env_get.assert_any_call("LANG")
1300 mock_norm.assert_called_once_with("de_DE")
1301
1302 # Test LANGUAGE (takes precedence over LANG if both were hypothetically checked
1303 # by os.environ.get, but our code checks in order, so we mock the first one it finds)
1304 with patch("locale.getlocale", side_effect=Exception("locale error")):
1305 with patch("os.environ.get") as mock_env_get:
1306 mock_env_get.side_effect = lambda key: "es_ES" if key == "LANGUAGE" else None
1307 with patch.object(coder, "normalize_language", return_value="Spanish") as mock_norm:
1308 self.assertEqual(coder.get_user_language(), "Spanish")
1309 mock_env_get.assert_any_call("LANGUAGE") # LANG would be called first
1310 mock_norm.assert_called_once_with("es_ES")
1311
1312 # 4. Test priority: chat_language > locale > env
1313 coder.chat_language = "it_IT"
1314 with patch("locale.getlocale", return_value=("en_US", "UTF-8")) as mock_getlocale:
1315 with patch("os.environ.get", return_value="de_DE") as mock_env_get:
1316 with patch.object(
1317 coder, "normalize_language", side_effect=lambda x: x.upper()
1318 ) as mock_norm:
1319 self.assertEqual(coder.get_user_language(), "IT_IT") # From chat_language
1320 mock_norm.assert_called_once_with("it_IT")
1321 mock_getlocale.assert_not_called()
1322 mock_env_get.assert_not_called()

Callers

nothing calls this directly

Calls 3

InputOutputClass · 0.90
createMethod · 0.80
get_user_languageMethod · 0.80

Tested by

no test coverage detected