#!/usr/bin/env python3 # file: scripts/tests/test_audit_markdown_tables.py # version: 1 """Regression tests for the KSP Markdown table audit.""" from __future__ import annotations import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) import audit_markdown_tables class MarkdownTableAuditTests(unittest.TestCase): """Cover table recognition and source alignment semantics.""" def audit(self, contents: str) -> tuple[int, list[str]]: """Audit one temporary Markdown fixture.""" with tempfile.TemporaryDirectory() as directory: path = pathlib.Path(directory) / "fixture.md" path.write_text(contents, encoding="utf-8") return audit_markdown_tables._audit_file(path) def test_accepts_left_and_right_aligned_columns(self) -> None: """Canonical left and right padding follows the separator markers.""" table_count, errors = self.audit("| Name | Count |\n|------|------:|\n| A | 2 |\n") self.assertEqual(table_count, 1) self.assertEqual(errors, []) def test_accepts_explicit_left_and_centered_columns(self) -> None: """Leading and paired colons select left and centered alignment.""" table_count, errors = self.audit("| Name | State |\n|:-----|:-----:|\n| A | ok |\n") self.assertEqual(table_count, 1) self.assertEqual(errors, []) def test_rejects_spaced_separator_that_was_previously_skipped(self) -> None: """A malformed but recognizable separator is audited instead of ignored.""" table_count, errors = self.audit("| Name | Count |\n| --- | ---: |\n| A | 2 |\n") self.assertEqual(table_count, 1) self.assertTrue(errors) def test_rejects_left_padding_in_right_aligned_column(self) -> None: """A trailing colon requires right-aligned source cells.""" table_count, errors = self.audit("| Name | Count |\n|------|------:|\n| A | 2 |\n") self.assertEqual(table_count, 1) self.assertTrue(any("right-aligned" in error for error in errors)) def test_rejects_right_padding_in_explicit_left_column(self) -> None: """A leading colon requires left-aligned source cells.""" table_count, errors = self.audit("| Name | Count |\n|:-----|-------|\n| A | 2 |\n") self.assertEqual(table_count, 1) self.assertTrue(any("left-aligned" in error for error in errors)) if __name__ == "__main__": unittest.main()