| 213 | |
| 214 | |
| 215 | class TestBuildPreamble(unittest.TestCase): |
| 216 | def test_normal_preamble(self): |
| 217 | text = ( |
| 218 | "# NAME\n\nfoo - a tool\n\n# SYNOPSIS\n\nfoo [opts]\n\n# OPTIONS\n\n**-v**" |
| 219 | ) |
| 220 | preamble = _build_preamble(text) |
| 221 | self.assertIn("foo - a tool", preamble) |
| 222 | self.assertIn("foo [opts]", preamble) |
| 223 | # OPTIONS should not be in preamble |
| 224 | self.assertNotIn("-v", preamble) |
| 225 | |
| 226 | def test_description_only_first_paragraph(self): |
| 227 | text = ( |
| 228 | "# NAME\n\nfoo\n\n" |
| 229 | "# DESCRIPTION\n\nFirst paragraph.\n\nSecond paragraph.\n\nThird." |
| 230 | ) |
| 231 | preamble = _build_preamble(text) |
| 232 | self.assertIn("First paragraph.", preamble) |
| 233 | self.assertNotIn("Third.", preamble) |
| 234 | |
| 235 | def test_oversized_name_section_is_truncated(self): |
| 236 | huge_name = "word " * (_MAX_PREAMBLE_CHARS // 3) |
| 237 | text = f"# NAME\n\n{huge_name}\n\n# OPTIONS\n\n**-v**" |
| 238 | preamble = _build_preamble(text) |
| 239 | self.assertLessEqual(len(preamble), _MAX_PREAMBLE_CHARS + 50) |
| 240 | self.assertTrue(preamble.endswith("[…truncated]")) |
| 241 | |
| 242 | def test_truncation_at_line_boundary(self): |
| 243 | """Truncation should not cut in the middle of a line.""" |
| 244 | lines = [f"line {i} " + "x" * 100 for i in range(_MAX_PREAMBLE_CHARS // 100)] |
| 245 | huge_name = "\n".join(lines) |
| 246 | text = f"# NAME\n\n{huge_name}\n\n# OPTIONS\n\n**-v**" |
| 247 | preamble = _build_preamble(text) |
| 248 | # The last real content line (before the marker) should be complete |
| 249 | preamble_lines = preamble.split("\n") |
| 250 | self.assertEqual(preamble_lines[-1], "[…truncated]") |
| 251 | # The second-to-last line should be one of our generated lines |
| 252 | self.assertTrue(preamble_lines[-2].startswith("line ")) |
| 253 | |
| 254 | def test_no_preamble_sections(self): |
| 255 | text = "# OPTIONS\n\n**-v** verbose\n\n# EXAMPLES\n\nfoo -v" |
| 256 | preamble = _build_preamble(text) |
| 257 | self.assertEqual(preamble, "") |
| 258 | |
| 259 | |
| 260 | # --------------------------------------------------------------------------- |
nothing calls this directly
no outgoing calls
no test coverage detected