Skip to content

Add support for multi-file tests#76

Merged
nunoplopes merged 4 commits into
Cpp2Rust:masterfrom
lucic71:multi-tu-tests
May 17, 2026
Merged

Add support for multi-file tests#76
nunoplopes merged 4 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

Comment thread tests/lit/lit/formats/Cpp2RustTest.py Outdated
rust_version = read_rust_version()
self.pkg_name = "test_" + re.sub(r"[^a-zA-Z0-9_]", "_", self.tmp_dir.name)

(self.tmp_dir / "rust-toolchain.toml").write_text(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is gone now

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread tests/lit/lit/formats/Cpp2RustTest.py Outdated
name = "{self.pkg_name}"
version = "0.1.0"
edition = "2021"
rust-version = "{self.rust_version}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this bit is gone now too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lucic71 added 3 commits May 16, 2026 12:28
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
@nunoplopes nunoplopes merged commit e70c5f4 into Cpp2Rust:master May 17, 2026
9 checks passed
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.

2 participants