Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix section title blank line errors in README.adoc #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions asciidoc_linter/rules/whitespace_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def check_line(
findings = []
line_content = self.get_line_content(line)

# Debug output to see why the whitespace_rules got triggered
print(f"Checking line {line_number + 1}: {line_content}")

# Check for multiple consecutive empty lines
if not line_content.strip():
self.consecutive_empty_lines += 1
Expand Down Expand Up @@ -121,7 +124,7 @@ def check_line(
# Check for blank line before section title (except for first line)
if line_number > 0:
prev_content = self.get_line_content(context[line_number - 1])
if prev_content.strip():
if prev_content.strip() and not prev_content.strip().startswith(("[.", "[[")):
findings.append(
Finding(
rule_id=self.id,
Expand All @@ -135,7 +138,7 @@ def check_line(
# Check for blank line after section title (except for last line)
if line_number < len(context) - 1:
next_content = self.get_line_content(context[line_number + 1])
if next_content.strip():
if next_content.strip() and not next_content.strip().startswith(":"):
findings.append(
Finding(
rule_id=self.id,
Expand Down
56 changes: 56 additions & 0 deletions tests/rules/test_heading_rules.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,61 @@ def test_multiple_top_level(self):
)


class TestHeadingAttributesAndRoles(unittest.TestCase):
"""Tests for headings with attributes or roles.
This rule ensures that headings followed by attributes or roles are handled correctly.
"""

def setUp(self):
"""
Given a HeadingFormatRule instance
"""
self.rule = HeadingFormatRule()

def test_heading_with_attribute(self):
"""
Given a document with headings followed by attributes
When the heading format rule is checked
Then no findings should be reported
"""
# Given: A document with headings followed by attributes
content = """
= Level 1
:attribute: value

== Level 2
:another-attribute: value
"""
# When: We check the heading format
findings = self.rule.check(content)

# Then: No findings should be reported
self.assertEqual(
len(findings), 0, "Headings followed by attributes should not produce findings"
)

def test_heading_with_role(self):
"""
Given a document with headings followed by roles
When the heading format rule is checked
Then no findings should be reported
"""
# Given: A document with headings followed by roles
content = """
[.role]
= Level 1

[[target]]
== Level 2
"""
# When: We check the heading format
findings = self.rule.check(content)

# Then: No findings should be reported
self.assertEqual(
len(findings), 0, "Headings followed by roles should not produce findings"
)


if __name__ == "__main__":
unittest.main()
24 changes: 24 additions & 0 deletions tests/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,27 @@ def test_lint_with_config_file(tmp_path, sample_asciidoc):

# Ensure that the WS001 rule is disabled and no findings are reported
assert len(report.findings) == 0


def test_lint_string_with_attributes_and_roles(mock_parser, mock_rule):
"""Test linting a string with headings followed by attributes or roles"""
content = """
= Title
:attribute: value

[.role]
== Section 1

[[target]]
== Section 2
"""

with patch("asciidoc_linter.linter.AsciiDocParser", return_value=mock_parser):
linter = AsciiDocLinter()
linter.rules = [mock_rule]

findings = linter.lint_string(content)

assert len(findings) == 0
mock_parser.parse.assert_called_once_with(content)
mock_rule.check.assert_called_once()
26 changes: 26 additions & 0 deletions tests/test_reporter.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,29 @@ def test_html_reporter_styling(sample_report):
assert "<style>" in output
assert "table {" in output
assert "border-collapse: collapse" in output


def test_console_reporter_with_attributes_and_roles():
"""Test formatting errors with headings followed by attributes or roles"""
findings = [
Finding(
message="Section title should be followed by a blank line",
severity=Severity.WARNING,
file="test.adoc",
position=Position(line=2),
rule_id="WS001",
),
Finding(
message="Section title should be preceded by a blank line",
severity=Severity.WARNING,
file="test.adoc",
position=Position(line=4),
rule_id="WS001",
),
]
report = LintReport(findings)
reporter = ConsoleReporter(enable_color=False)
lines = reporter.format_report(report).split("\n")
assert "Results for test.adoc:" in lines
assert "✗ test.adoc, line 2: Section title should be followed by a blank line" in lines
assert "✗ test.adoc, line 4: Section title should be preceded by a blank line" in lines
Loading