Skip to content

test: Move the last file-local test fixtures into helpers, with self-describing names - #2858

Open
krlmlr wants to merge 2 commits into
mainfrom
claude/issue-2793-ppcpst
Open

test: Move the last file-local test fixtures into helpers, with self-describing names#2858
krlmlr wants to merge 2 commits into
mainfrom
claude/issue-2793-ppcpst

Conversation

@krlmlr

@krlmlr krlmlr commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Closes #2793.

Prepared with Claude Code.

Fixtures out of test files

Seven fixture functions still sat at the top level of test files, and three fixtures in helper-test-functions.R were plain objects rather than functions. Both shapes make a test_that() block depend on code that ran before it: the block cannot be run on its own, and — for the shared objects — one block can mutate what the next one sees.

The functions move to helper-test-functions.R, each under a section naming the test file that uses it, and the objects become functions returning a fresh value per call:

  • make_weighted_clique_graph() from test-cliques.R
  • tarjan_yannakakis_graph() from test-decomposition.R
  • load_migration_generator() and write_migration_registry() from test-generate-migrations.R
  • make_weighted_graphlet_graph() from test-glet.R
  • hrg_graph_and_fit() from test-hrg.R
  • simple_graph_with_edge_attrs() from test-operators.R
  • unique_vertex_seq_cases(), karate_oldstyle_graph() and karate_oldstyle_vertex_names(), previously top-level objects in the helper file

Each test file keeps a one-line comment pointing at the helper file, so the fixture is still discoverable from where it is used. The one snapshot that printed a fixture (V(karate_oldstyle) in _snaps/other.md) is updated to the call form.

Scanning all 73 files under tests/testthat/ confirms no test file defines a top-level function or object any more. What remains at the top level is only setup code in the files meant for it — setup.R, setup-lifecycle.R, helper-migrations.R — plus the file-level skip_on_cran() in test-aaa-auto.R, which is neither a function nor an object and whose semantics depend on running at file scope.

Names that read out of context

These helpers all share one environment, so a name has to be unambiguous far from its definition. Several were not — unique_tests() named neither what was unique nor that it held cases rather than tests, and rn(), sortgl() and threshold.net() said almost nothing. Renamed, with no change in behaviour:

  • unique_tests()unique_vertex_seq_cases()
  • rn()name_rows_by_edge_endpoints()
  • order_by_two_first_columns()sort_edgelist_rows() (it sorts rows, it does not return an ordering)
  • simple_graph_with_attrs()simple_graph_with_edge_attrs()
  • order_by_magnitude() / sort_by_magnitude()order_eigenvalues_by_magnitude() / sort_eigenvalues_by_magnitude()
  • make_graphlet_graph()make_weighted_graphlet_graph()
  • sortgl()sort_graphlets_by_size()
  • threshold.net()max_cliques_above_weight()
  • graphlets.old() / graphlets.project.old()graphlet_basis_reference() / graphlet_proj_reference()
  • karate_oldstyle()karate_oldstyle_graph(), karate_oldstyle_names()karate_oldstyle_vertex_names()
  • local_generator()load_migration_generator(), write_registry()write_migration_registry()
  • hrg_fixture()hrg_graph_and_fit()

load_migration_generator() also loses its env argument, which was never read and only carried the local_ prefix's implication of withr-style scoping. Helpers whose names were already unambiguous keep them; the four that were only terse gain a comment saying what they return.

Verification

Tests for the touched files pass locally. The one failure in test-other.R (can create graphs when igraph is not attached) is unrelated: it spawns a callr subprocess that needs igraph installed, which this sandbox does not have.

  • By submitting this pull request, I assign the copyright of my contribution to The igraph development team.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GkfBDoTdJ8qVNAqRCvBpbn

claude added 2 commits August 17, 2026 17:21
Seven fixture functions still sat at the top level of test files,
and three fixtures in `helper-test-functions.R` were plain objects
rather than functions.

Both shapes make a `test_that()` block depend on code that ran before it:
the block cannot be run on its own,
and — for the shared objects — one block can mutate what the next one sees.

The functions move to `helper-test-functions.R`,
each under a section naming the test file that uses it,
and the objects become functions returning a fresh value per call:

- `make_weighted_clique_graph()` from `test-cliques.R`
- `tarjan_yannakakis_graph()` from `test-decomposition.R`
- `local_generator()` and `write_registry()` from `test-generate-migrations.R`
- `make_graphlet_graph()` from `test-glet.R`
- `hrg_fixture()` from `test-hrg.R`
- `simple_graph_with_attrs()` from `test-operators.R`
- `unique_tests()`, `karate_oldstyle()` and `karate_oldstyle_names()`,
  previously top-level objects in the helper file

No test file defines a top-level function or object any more.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GkfBDoTdJ8qVNAqRCvBpbn
The helpers live in one environment shared by the whole suite,
so a name has to read unambiguously far from its definition.
Several did not: `unique_tests()` names neither what is unique
nor that it holds cases rather than tests,
and `rn()`, `sortgl()` and `threshold.net()` say almost nothing at all.

Renamed, with no change in behaviour:

- `unique_tests()` -> `unique_vertex_seq_cases()`
- `rn()` -> `name_rows_by_edge_endpoints()`
- `order_by_two_first_columns()` -> `sort_edgelist_rows()`
  (it sorts rows, it does not return an ordering)
- `simple_graph_with_attrs()` -> `simple_graph_with_edge_attrs()`
- `order_by_magnitude()` -> `order_eigenvalues_by_magnitude()`
- `sort_by_magnitude()` -> `sort_eigenvalues_by_magnitude()`
- `make_graphlet_graph()` -> `make_weighted_graphlet_graph()`
- `sortgl()` -> `sort_graphlets_by_size()`
- `threshold.net()` -> `max_cliques_above_weight()`
- `graphlets.old()` -> `graphlet_basis_reference()`
- `graphlets.project.old()` -> `graphlet_proj_reference()`
- `karate_oldstyle()` -> `karate_oldstyle_graph()`
- `karate_oldstyle_names()` -> `karate_oldstyle_vertex_names()`
- `local_generator()` -> `load_migration_generator()`
- `write_registry()` -> `write_migration_registry()`
- `hrg_fixture()` -> `hrg_graph_and_fit()`

`load_migration_generator()` also loses its `env` argument,
which was never read and only carried the `local_` prefix's
implication of withr-style scoping.

The helpers whose names were already unambiguous keep them,
and the four that were only terse gain a comment saying what they return.
@krlmlr krlmlr changed the title test: Move the last file-local test fixtures into helpers test: Move the last file-local test fixtures into helpers, with self-describing names Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extract all functions and objects used in tests into functions in the R/ directory or into testthat helpers

2 participants