Skip to content

Add support for multi-file tests#69

Closed
lucic71 wants to merge 5 commits into
Cpp2Rust:masterfrom
lucic71:multi-tu-tests
Closed

Add support for multi-file tests#69
lucic71 wants to merge 5 commits into
Cpp2Rust:masterfrom
lucic71:multi-tu-tests

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 11, 2026

This PR adds support for writing multi translation-unit tests in the below format. All multi translation-unit tests go into tests/multi-file, one test per directory. test.expectations is an optional file that contains one of the following test expectations: no-compile, translation-fail, panic, nondet-result; or it can be empty if the test is successful:

tests/multi-file/
└── extern_functions
    ├── a.c
    ├── b.c
    ├── CMakeLists.txt
    ├── out
    │   ├── refcount
    │   │   └── extern_functions.rs
    │   └── unsafe
    │       └── extern_functions.rs
    └── test.expectations

I took this opportunity to refactor the logic inside Cpp2RustTest.py because the multi-file addition did not fit well into the current model. Now Cpp2RustTest.py is composed of:

  • TestExpectations: dataclass describing the expected output of the test: should_panic, should_not_compile, should_not_translate, is_nondet_result, xfail. For classic tests (ub, benchmarks, unit-tests), TestExpectations is populated from the comment line inside the source file, for multi-file tests, TestExpectations is populated from test.expectatoins
  • TestContext: dataclass that defines all test steps (setup, translate, check_expected, build_cpp, build_rust, run_cpp, run_rust, compare) and keeps test specific data as member variables, including TestExpectations
  • Cpp2RustTest: entry point of lit, contains execute (which calls directly into TestContext) getTestsForPath and getTestsInDirectory
  • free helper functions

@lucic71 lucic71 marked this pull request as draft May 11, 2026 12:04
Drop _ from RE_ global variables
Add compare step that compares cpp_result and rust_result
Use Path instead of str for TestContext fields where it makes sense
Simplify preconditions for each TestContext pass
@lucic71 lucic71 marked this pull request as ready for review May 11, 2026 13:38
@lucic71
Copy link
Copy Markdown
Contributor Author

lucic71 commented May 11, 2026

Besides the Cpp2RustTest.py refactor. I also linted the code and added a python lint/format check in format.yml.

I didn't add ruff inside the CMakeLists format rule because this file is expected to be modified very few times.

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.

1 participant