--- id: "0047" title: "C++ tests foundation: Catch2 + tests para top-20 primitivos" status: completado type: feature domain: - cpp-stack scope: multi-app priority: alta depends: [] blocks: [] related: [] created: 2026-05-17 updated: 2026-05-17 tags: [] --- # 0047 — C++ tests foundation: Catch2 + tests para top-20 primitivos ## Metadata | Campo | Valor | |-------|-------| | **ID** | 0047 | | **Estado** | pendiente | | **Prioridad** | alta | | **Tipo** | feature — testing infra | ## Dependencias Ninguna. --- ## Objetivo Montar Catch2 (single-header, vendoreado) en `cpp/`, integrar con CMake y `ctest`, y escribir tests unitarios para el top-20 de primitivos puros del registry. Pasar de 4% a >25% de cobertura en C++. ## Contexto Hoy hay 3 funciones C++ con `tested: true` (3.7% de 81). El indexer ya soporta extraer tests automaticamente al campo `tests` del `.md` cuando hay `test_file_path`. Falta el framework, la integracion y la cobertura inicial. ## Arquitectura ``` cpp/ ├── vendor/catch2/ │ └── catch_amalgamated.{hpp,cpp} # NEW — Catch2 v3 amalgamated, MIT ├── tests/ │ ├── CMakeLists.txt # NEW — un test per-file │ ├── test_tokens.cpp # NEW │ ├── test_button.cpp # NEW (state-only — sin GUI render) │ ├── test_select.cpp # NEW │ ├── test_text_input.cpp # NEW │ ├── test_badge.cpp # NEW │ ├── test_kpi_card.cpp # NEW (calculo de delta/format) │ ├── test_pie_chart.cpp # NEW (slice_at logic) │ ├── test_bar_chart.cpp # NEW (label rotation logic) │ ├── test_tree_view.cpp # NEW │ ├── test_modal_dialog.cpp # NEW │ ├── test_toolbar.cpp # NEW │ ├── test_toast.cpp # NEW │ ├── test_empty_state.cpp # NEW │ ├── test_page_header.cpp # NEW │ ├── test_dashboard_panel.cpp # NEW │ ├── test_dashboard_grid.cpp # NEW │ ├── test_sparkline.cpp # NEW │ ├── test_table_view.cpp # NEW │ ├── test_icon_button.cpp # NEW │ └── test_tween_curves.cpp # NEW (puro, ideal candidato) └── CMakeLists.txt # MOD — anadir tests subdir si BUILD_TESTING ``` ## Tareas ### Fase 1 — Vendoreado de Catch2 1.1 Bajar `catch_amalgamated.hpp` + `catch_amalgamated.cpp` (Catch2 v3 single-header) a `cpp/vendor/catch2/`. Anadir LICENSE y README pequeno. 1.2 No usar FetchContent — stay offline. Catch2 v3 es BSL-1.0. ### Fase 2 — Integracion CMake 2.1 En `cpp/CMakeLists.txt`, anadir option `BUILD_TESTING` (default ON). Si activo, `add_subdirectory(tests)`. 2.2 `cpp/tests/CMakeLists.txt`: una funcion CMake `add_fn_test(name srcs)` que: - Crea un executable test linkeado contra `fn_framework`, ImGui (sin GLFW si no hace falta), Catch2. - Anade al `ctest` con `add_test(NAME ${name} COMMAND ${name})`. ### Fase 3 — Tests para puros (sin GUI) 3.1 `tween_curves`: lerp, ease_in, ease_out, edge cases (t=0, t=1, valores fuera de rango). 3.2 `slice_at` de pie_chart (es funcion interna — exponer via header de test o moverla a `pie_math.h`). 3.3 Cualquier helper puro de `bar_chart` (rotacion de labels), `kpi_card` (formato de delta). ### Fase 4 — Tests para componentes con state pero sin render (parcial) 4.1 Para `process_state_machine`, `file_poll_diff`, `sql_parse` (creados en 0045 si existen): tests directos. 4.2 Para componentes UI (`button`, `select`, `tree_view`, etc.): solo logica accesible sin contexto ImGui (callbacks, state setters, formateo). Render real queda para tests visuales (issue 0048). ### Fase 5 — Hook con el indexer 5.1 Para cada funcion testeada, actualizar su `.md`: `tested: true`, `test_file_path: cpp/tests/test_.cpp`. El indexer puebla `tests` automaticamente desde el archivo. 5.2 `./fn index` y verificar: ```bash sqlite3 registry.db "SELECT COUNT(*) FROM functions WHERE lang='cpp' AND tested=1;" ``` Meta: >=20 (de 81 = 25%). ### Fase 6 — CI gate (warmup, sin failure) 6.1 Anadir `cpp/scripts/run_tests.sh` que hace `cmake --build && ctest --output-on-failure`. 6.2 Documentar en `cpp/PATTERNS.md` (issue 0041) que toda funcion nueva debe tener `tested: true`. ## Decisiones - Catch2 v3 amalgamated (no header-only) — compila mas rapido despues del primer build. - Tests **no** intentan renderizar ImGui — pintar requiere GL context, complicado en CI. Tests visuales se hacen en issue 0048 con primitives_gallery + screenshots. ## Riesgos - Algunos primitivos no tienen logica testeable sin contexto ImGui (ej. `toolbar` solo concatena items). En esos casos, escribir test trivial que verifica que el codigo compila + linkea, o saltarlo y documentar en `notes`. - Catch2 puede tardar 30-60s la primera vez. Aceptable. ## Validacion ```bash cd cpp/build && cmake -DBUILD_TESTING=ON .. && cmake --build . -j ctest --output-on-failure sqlite3 registry.db "SELECT id FROM functions WHERE lang='cpp' AND tested=1;" ``` 20+ tests pasando, 20+ funciones con `tested: true`.