test: Move the last file-local test fixtures into helpers, with self-describing names - #2858
Open
krlmlr wants to merge 2 commits into
Open
test: Move the last file-local test fixtures into helpers, with self-describing names#2858krlmlr wants to merge 2 commits into
krlmlr wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Rwere plain objects rather than functions. Both shapes make atest_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()fromtest-cliques.Rtarjan_yannakakis_graph()fromtest-decomposition.Rload_migration_generator()andwrite_migration_registry()fromtest-generate-migrations.Rmake_weighted_graphlet_graph()fromtest-glet.Rhrg_graph_and_fit()fromtest-hrg.Rsimple_graph_with_edge_attrs()fromtest-operators.Runique_vertex_seq_cases(),karate_oldstyle_graph()andkarate_oldstyle_vertex_names(), previously top-level objects in the helper fileEach 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-levelskip_on_cran()intest-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, andrn(),sortgl()andthreshold.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 itsenvargument, which was never read and only carried thelocal_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 acallrsubprocess that needs igraph installed, which this sandbox does not have.🤖 Generated with Claude Code
https://claude.ai/code/session_01GkfBDoTdJ8qVNAqRCvBpbn