Skip to content

Commit 9c89d20

Browse files
16bit-ykikoclaude
andauthored
feat(tests): add compile_with_modules helper to Tester (#420)
## Summary - Add `add_module()` and `compile_with_modules()` to the `Tester` test framework - Supports both separate `add_module()` calls and single-string `#[filename]` syntax via `add_files()` - Automatically scans module dependencies with `scan_precise`, topologically sorts, builds PCMs in order, then compiles the main file - Temporary PCM files cleaned up automatically in destructor - Migrated `ModuleImport` and `ModuleReexport` semantic tokens tests to use the new API ## Test plan - [x] All 505 unit tests pass - [x] All 113 integration tests pass - [x] All 2 smoke tests pass 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Centralized, module-aware test compilation with automatic module discovery, dependency ordering, and cycle detection. * Unified "compile with modules" flow; tests now add module sources directly and no longer manage temporary module artifacts manually. * Reduced duplicated compile/diagnostic logic and improved cleanup of generated artifacts. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8bafaa8 commit 9c89d20

File tree

3 files changed

+197
-143
lines changed

3 files changed

+197
-143
lines changed

tests/unit/feature/semantic_tokens_tests.cpp

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -449,42 +449,21 @@ export @kw[module] @m0[foo].@m1[bar];
449449
}
450450

451451
TEST_CASE(ModuleImport) {
452-
auto pcm_path = fs::createTemporaryFile("test-mod", "pcm");
453-
ASSERT_TRUE(pcm_path.has_value());
454-
455-
{
456-
Tester mod;
457-
mod.add_main("mod.cppm", "export module foo;\nexport int x = 42;\n");
458-
mod.prepare("-std=c++20");
459-
mod.params.kind = CompilationKind::ModuleInterface;
460-
mod.params.output_file = *pcm_path;
461-
auto built = clice::compile(mod.params);
462-
ASSERT_TRUE(built.completed());
463-
}
452+
add_files("main.cpp", R"(
453+
#[mod.cppm]
454+
export module foo;
455+
export int x = 42;
464456
465-
add_main("main.cpp", R"cpp(
457+
#[main.cpp]
466458
@kw[import] @mod[foo];
467459
int y = x;
468-
)cpp");
469-
prepare("-std=c++20");
470-
auto fmodule_arg = std::string("-fmodule-file=foo=") + *pcm_path;
471-
owned_args.push_back(fmodule_arg);
472-
params.arguments.clear();
473-
for(auto& arg: owned_args) {
474-
params.arguments.push_back(arg.c_str());
475-
}
476-
477-
auto built = clice::compile(params);
478-
ASSERT_TRUE(built.completed());
479-
unit.emplace(std::move(built));
480-
460+
)");
461+
ASSERT_TRUE(compile_with_modules());
481462
tokens = feature::semantic_tokens(*unit, feature::PositionEncoding::UTF8);
482463
decoded = decode_utf8_tokens(unit->interested_content(), tokens);
483464

484465
EXPECT_TOKEN("kw", SymbolKind::Keyword);
485466
EXPECT_TOKEN("mod", SymbolKind::Module);
486-
487-
fs::remove(*pcm_path);
488467
}
489468

490469
TEST_CASE(ModulePartition) {
@@ -500,43 +479,21 @@ export module @m0[foo]:@m1[bar];
500479
}
501480

502481
TEST_CASE(ModuleReexport) {
503-
auto pcm_path = fs::createTemporaryFile("test-mod", "pcm");
504-
ASSERT_TRUE(pcm_path.has_value());
505-
506-
{
507-
Tester mod;
508-
mod.add_main("mod.cppm", "export module foo;\nexport int x = 42;\n");
509-
mod.prepare("-std=c++20");
510-
mod.params.kind = CompilationKind::ModuleInterface;
511-
mod.params.output_file = *pcm_path;
512-
auto built = clice::compile(mod.params);
513-
ASSERT_TRUE(built.completed());
514-
}
482+
add_files("main.cppm", R"(
483+
#[mod.cppm]
484+
export module foo;
485+
export int x = 42;
515486
516-
add_main("main.cppm", R"cpp(
487+
#[main.cppm]
517488
export module bar;
518489
export @kw[import] @mod[foo];
519-
)cpp");
520-
prepare("-std=c++20");
521-
auto fmodule_arg = std::string("-fmodule-file=foo=") + *pcm_path;
522-
owned_args.push_back(fmodule_arg);
523-
params.arguments.clear();
524-
for(auto& arg: owned_args) {
525-
params.arguments.push_back(arg.c_str());
526-
}
527-
params.kind = CompilationKind::ModuleInterface;
528-
529-
auto built = clice::compile(params);
530-
ASSERT_TRUE(built.completed());
531-
unit.emplace(std::move(built));
532-
490+
)");
491+
ASSERT_TRUE(compile_with_modules());
533492
tokens = feature::semantic_tokens(*unit, feature::PositionEncoding::UTF8);
534493
decoded = decode_utf8_tokens(unit->interested_content(), tokens);
535494

536495
EXPECT_TOKEN("kw", SymbolKind::Keyword);
537496
EXPECT_TOKEN("mod", SymbolKind::Module);
538-
539-
fs::remove(*pcm_path);
540497
}
541498

542499
TEST_CASE(GlobalModuleFragment) {

0 commit comments

Comments
 (0)