Commit fc16cd3
Refactor match_taxa() and expand match-step test coverage (#283)
Pins `match_taxa()` behaviour with tests, then refactors the matcher
against them. **#287 has been merged into this branch**, so this PR now
carries both halves of the work and is the single merge to `master`.
## Part 1 — safety net (this branch's original scope)
- **Fix** malformed `aligned_reason` for fuzzy genus-level
`aff.`/`affinis` matches (`match_06b`/`06c`/`06d`), which appended the
date without a separating ` (` — output was `...genus-rank2026-..)`.
- **Extend** `test_matches_alignments_updates.csv` with inputs for the 7
match steps no benchmark previously reached (`03e`, `04e`, `06e`, `08e`,
`12g`, `12h`, `12i`); now covers all 54. Refreshed the previously-unused
`alignment_code` column to real values.
- **Assert** `alignment_code` in the alignment regression test (was
selected but never checked — which is why the reason bug hid).
- **Add** `test-match_branches.R`: dark-branch resolution,
`aligned_reason` well-formedness, and a full-output snapshot
(`_snaps/match_branches.md`, date-normalised).
## Part 2 — the refactor (#287, reviewed by @ehwenk)
`R/match_taxa.R` was ~2150 lines of near-identical blocks, each
repeating: build a logical index → `match()` a resource table →
`mutate()` seven columns → `redistribute()` → early-return. Five helpers
now carry that shape: `apply_match()`, `match_reference_name()`,
`genus_sp_name()`/`higher_rank_name()`, `fuzzy_match_column()`,
`drop_scratch()`. Code lines excluding comments and blanks: **1606 →
766**; total file 2156 → 1192, with the per-branch taxonomic comments
kept verbatim.
`apply_match()` appends ` (<date>)` to `aligned_reason` centrally, so
the separator that broke in Part 1 can no longer drift between match
steps.
**One behaviour fix:** `align_taxa(full = TRUE)` leaked the internal
`identifier_string`, `identifier_string2` and `aligned_name_tmp` columns
whenever every input aligned before the last match step ran — cleanup
only sat on the final return, not the 53 early ones. Now runs on every
exit path.
**Review follow-ups applied:** helper arguments are named for the output
columns they populate (`aligned_reason`, `alignment_code`,
`taxonomic_dataset`, `taxon_rank`) so they stay distinct from
`update_reason` on the update side; `column` → `name_type`; and the
matcher is described as a linear series of "match steps" rather than
"branches".
## Verification
- All 54 alignment codes present and in the same order.
- `align_taxa(full = TRUE)` byte-identical to pre-refactor across all 26
columns for the 253 benchmark + dark-step + ad-hoc names, under four
option combinations. Only difference is the three leaked columns
disappearing from the early-exit case.
- Full suite: **175 passed, 0 failed, 0 skipped**, with
`_snaps/match_branches.md` unmodified — the safety net from Part 1 held
across the whole refactor.
- `R CMD check --as-cran`: 0 errors, 0 warnings. (2 notes, both
pre-existing on `master`: `AGENTS.md` and `CITATION.cff` are not in
`.Rbuildignore`.)
## Not fixed here
Four `aligned_reason` strings contain stray double spaces
(`identification but`, `same genus, but`, `hybrid, but`), preserved
exactly since fixing them changes user-facing output. There is also more
repetition left to collapse in the fuzzy genus/family steps — noted in
`AGENTS.md` as the next target.
Closes #279. Related: #281
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>1 parent 8aafec6 commit fc16cd3
8 files changed
Lines changed: 1305 additions & 2064 deletions
File tree
- R
- tests/testthat
- _snaps
- benchmarks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
23 | 43 | | |
24 | 44 | | |
25 | 45 | | |
| |||
88 | 108 | | |
89 | 109 | | |
90 | 110 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
95 | 115 | | |
96 | 116 | | |
97 | 117 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
3 | 6 | | |
4 | 7 | | |
5 | 8 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | 90 | | |
94 | 91 | | |
95 | 92 | | |
| |||
0 commit comments