#!/usr/bin/env python3 """Proofs for scripts/dev/extract_modules.py on synthetic sources.""" import pathlib import sys import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from extract_modules import plan # noqa: E402 BODY = "".join(f" fn f{n}() {{}}\n" for n in range(5)) SOURCE = f'use a::b;\n\n#[cfg(test)]\nmod tests {{\n const P: &str = include_str!("x.ptx");\n{BODY}}}\n\npub(crate) mod small {{\n fn g() {{}}\n}}\n\nfn keep() {{}}\n' class ExtractModules(unittest.TestCase): def test_a_large_inline_module_moves_and_keeps_its_attributes(self): text, files, skipped = plan(SOURCE, pathlib.Path("d"), 3) self.assertIn("#[cfg(test)]\nmod tests;", text) self.assertEqual(list(files), [pathlib.Path("d/tests.rs")]) self.assertEqual(skipped, []) def test_the_body_is_dedented_and_includes_go_one_level_up(self): _, files, _ = plan(SOURCE, pathlib.Path("d"), 3) body = files[pathlib.Path("d/tests.rs")] self.assertIn('include_str!("../x.ptx")', body) self.assertIn("\nfn f0() {}\n", body) def test_small_modules_and_functions_stay(self): text, _, _ = plan(SOURCE, pathlib.Path("d"), 3) self.assertIn("pub(crate) mod small {", text) self.assertIn("fn keep() {}", text) def test_include_macro_without_a_suffix_is_deepened(self): _, files, _ = plan("mod m {\n include!(\"a.rs\");\n fn f() {}\n}\n", pathlib.Path("d"), 1) self.assertIn('include!("../a.rs")', files[pathlib.Path("d/m.rs")]) def test_a_path_attribute_inside_the_block_keeps_it_in_place(self): text, files, skipped = plan('mod m {\n #[path = "q.rs"]\n mod q;\n fn f() {}\n}\n', pathlib.Path("d"), 1) self.assertEqual(files, {}) self.assertIn("mod m {", text) self.assertEqual(len(skipped), 1) if __name__ == "__main__": unittest.main()