Files

61 lines
3.4 KiB
Markdown

# Tests for ki18n
The suite uses [pytest](https://docs.pytest.org/) with
[pytest-asyncio](https://pytest-asyncio.readthedocs.io/). The Textual dialogs are
operated through Textual's `Pilot` (clicks and key presses in the headless test
driver `App.run_test`).
`Ki18n.get_locale()` is replaced by a stub returning `None` for every test (autouse fixture in `conftest.py`), so the results do not depend on the machine's locale; `test_locale.py` restores the real implementation, other tests use the `system_locale` fixture.
Every test runs in temporary directories (installed and user resources;
`XDG_DATA_HOME` and `LOCALAPPDATA` are redirected), so real user data is never
read. The `Ki18n` singleton is reset before and after every test.
```sh
uv sync # installs the dev dependency group (pytest, pytest-asyncio)
uv run pytest # or: python3 -m pytest
uv run pytest -k plural # run a subset
```
Configuration lives in `pyproject.toml` (`[tool.pytest.ini_options]`):
`testpaths`, `pythonpath = ["src"]` (no installation needed) and
`asyncio_mode = "auto"`.
## Layout
| File | Contents |
| --- | --- |
| `conftest.py` | Fixtures: `env`, `installed`, `manager`, `log_warnings`, singleton reset |
| `support.py` | Example data (`EN`, `DE`, dialog keys) and the `Env` helper class |
| `test_normalize.py` | Language ID normalization, fallback chains |
| `test_paths.py` | `ki18n.paths`: frozen detection, `bundle_root()` independent of where ki18n lives |
| `test_construction.py` | Constructor, singleton, `project_root`/`local_root`, data directories |
| `test_discovery.py` | `get_available_languages()`, user and installed resources |
| `test_loading.py` | Validation and merging of `strings.json`, regional layers |
| `test_set_language.py` | `set_language()`, error cases, listeners |
| `test_translate.py` | `t()`: fallback order, formatting |
| `test_plural.py` | `p()`: categories, `#count`/`{count}`, fallbacks |
| `test_default_language.py` | Default language from the system locale (regional fallback, `DEFAULT_LANGUAGE` fallback, explicit language unaffected) |
| `test_help.py` | `get_help()`: regions, user files, English fallback |
| `test_locale.py` | `Ki18n.get_locale()`: POSIX environment, macOS `defaults`, Windows API (all simulated on any host) |
| `test_lazy_handle.py` | Module handle `i18n`, alias names |
| `test_public_api.py` | Public API as documented in the README (package-level exports) |
| `test_shipped_resources.py` | Consistency of the shipped `i18n/` files |
| `test_screens.py` | `HelpScreen` / `LanguageSelectScreen` driven with `Pilot`, plus a small integration app |
## Fixtures
- `env` - isolated `Env`: `env.install(lang, strings, help)`, `env.user(...)`,
`env.make()` / `env.remake()`, directories `app_dir`, `user_dir`, `data_home`.
- `installed` - `env` with the example languages `en` and `de` installed, no manager yet.
- `manager` - `installed` plus a constructed `Ki18n` instance.
- `system_locale` - callable that makes `Ki18n.get_locale()` report a locale, e.g. `system_locale("de_AT")`, or `None`.
- `log_warnings` - callable returning the library's WARNING messages so far.
## Known deviations between documentation and code
None at the moment. If a test is added for documented behavior that the code
does not fulfil yet, mark it `xfail(strict=True)`: it is reported as `xfailed`
until the code is fixed and then turns into a failure (`XPASS(strict)`), which
is the signal to remove the marker.