#!/usr/bin/env python3 """Synthetic regression tests for strict source-level coverage accounting.""" from __future__ import annotations import importlib.util import unittest def load_summary_module(): spec = importlib.util.spec_from_file_location( "summarize_test_coverage", "scripts/summarize-test-coverage.py" ) assert spec is not None and spec.loader is not None module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module SUMMARY = load_summary_module() SOURCE = "/libs/kaptan-navigation/" FILENAME = "/repo/libs/kaptan-navigation/src/eskf.rs" def json_report(*, expansions=None, expanded_file_id=0): return { "data": [ { "files": [ { "filename": FILENAME, "expansions": [] if expansions is None else expansions, # Segments intentionally differ from DA lines: strict # line coverage must not lower its denominator to them. "segments": [[10, 1, 3, True, True, False]], "branches": [ [10, 5, 10, 10, 0, 3, 0, expanded_file_id, 4], [10, 5, 10, 10, 2, 0, 0, expanded_file_id, 4], [11, 5, 11, 10, 0, 0, 0, expanded_file_id, 4], ], } ] } ] } LCOV = """SF:/repo/libs/kaptan-navigation/src/eskf.rs DA:10,0 DA:11,0 end_of_record SF:/repo/libs/kaptan-navigation/src/eskf.rs DA:10,3 DA:12,1 end_of_record """ class CoverageSummaryTests(unittest.TestCase): def test_unions_da_lines_and_full_span_branch_outcomes(self): self.assertEqual( SUMMARY.coverage_totals(LCOV, json_report(), SOURCE), # DA lines: 10 is covered by the second generic instance; 11 is # uncovered; 12 is covered. Branch outcomes: the two duplicate # source spans cover both outcomes, while line 11 covers neither. (2, 3, 2, 4), ) def test_rejects_macro_expansion_records(self): with self.assertRaises(SUMMARY.CoverageFormatError): SUMMARY.coverage_totals(LCOV, json_report(expansions=[{}]), SOURCE) with self.assertRaises(SUMMARY.CoverageFormatError): SUMMARY.coverage_totals(LCOV, json_report(expanded_file_id=1), SOURCE) if __name__ == "__main__": unittest.main()