Skip to content

Commit 615bdf5

Browse files
committed
Enhance hidden line detection in mdbook-swift-test.py. Update regex to correctly identify hidden lines and add regression tests for unhide_line function to ensure proper functionality.
1 parent dd312e5 commit 615bdf5

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

scripts/mdbook-swift-test.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
55
Code blocks beginning with ```swift are compiled, unless it is ignored (```swift,ignore).
66
In those blocks,
7-
- lines beginning with # are hidden from readers but are compiled.
7+
- lines of the form "#" or "# " + text are hidden from readers but are compiled.
8+
(Swift # directives like #if and #file are unchanged; they have no space after #.)
89
- lines beginning with \\# are shown and compiled, including the # (e.g. \\#available(...)).
910
- placeholder bodies `{ ... }` are replaced with `{ fatalError() }`.
1011
@@ -71,8 +72,9 @@ class TestResult:
7172
error: str
7273

7374

74-
# Hidden setup lines: optional indent, then #, then optional space and rest of line.
75-
_HIDDEN_LINE = re.compile(r"^(\s*)#(?: (.*))?$")
75+
# Hidden setup lines: optional indent, then either "#" alone or "# " + content.
76+
# Does not match #if, #file, #available(...), etc. (no space after #).
77+
_HIDDEN_LINE = re.compile(r"^(\s*)#(?: (.*)|)$")
7678

7779

7880
def unhide_line(line: str) -> str:
@@ -86,6 +88,31 @@ def unhide_line(line: str) -> str:
8688
return line
8789

8890

91+
def _self_test_unhide_line() -> None:
92+
"""Regression tests for hidden-line detection (run with --self-test)."""
93+
cases = [
94+
("# import Foundation", "import Foundation"),
95+
(" # import Foundation", " import Foundation"),
96+
("#", ""),
97+
(" #", " "),
98+
("# ", ""),
99+
("# let n = 0", "let n = 0"),
100+
("extension Array {", "extension Array {"),
101+
("\\#available(swift, 5)", "#available(swift, 5)"),
102+
# Swift # directives and macros must not be treated as hidden lines
103+
("#if", "#if"),
104+
("#endif", "#endif"),
105+
("#file", "#file"),
106+
("#foo", "#foo"),
107+
("#available(swift, 5)", "#available(swift, 5)"),
108+
(" #if DEBUG", " #if DEBUG"),
109+
]
110+
for inp, want in cases:
111+
got = unhide_line(inp)
112+
if got != want:
113+
raise AssertionError(f"unhide_line({inp!r}) == {got!r}, want {want!r}")
114+
115+
89116
# Matches code blocks: ```info\n...content...\n```
90117
# Groups: (1) info string e.g. "swift,ignore", (2) block content
91118
CODE_BLOCK = re.compile(
@@ -270,6 +297,11 @@ def _usage_stderr() -> None:
270297

271298

272299
def main() -> None:
300+
if len(sys.argv) > 1 and sys.argv[1] == "--self-test":
301+
_self_test_unhide_line()
302+
print("OK: unhide_line self-test passed", file=sys.stderr)
303+
sys.exit(0)
304+
273305
if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help"):
274306
print(__doc__.strip(), file=sys.stderr)
275307
sys.exit(0)

0 commit comments

Comments
 (0)