from srs2std.srs.structure import (
    SectionTitle,
    TableOfContents,
    complement_falling_sections,
)


def test_complement_falling_sections():
    toc = TableOfContents(
        titles=[
            SectionTitle(number="1", name="One"),
            SectionTitle(number="1.1", name="One one"),
            SectionTitle(number="1.1.1", name="One one one"),
        ]
    )

    md_doc = """\
## 1.1 One one
This is the first sentence.
This is the second sentence.
"""

    filled_md_doc = """\
# 1 One
[NULL]

## 1.1 One one
This is the first sentence.
This is the second sentence.

### 1.1.1 One one one
[NULL]
"""

    expected = filled_md_doc.strip()
    actual = complement_falling_sections(md_doc, toc).strip()
    assert actual == expected


def test_complement_falling_sections_for_empty_doc():
    toc = TableOfContents(
        titles=[
            SectionTitle(number="1", name="One"),
            SectionTitle(number="1.1", name="One one"),
            SectionTitle(number="1.1.1", name="One one one"),
        ]
    )

    md_doc = ""  # empty

    filled_md_doc = """\
# 1 One
[NULL]

## 1.1 One one
[NULL]

### 1.1.1 One one one
[NULL]
"""

    expected = filled_md_doc.strip()
    actual = complement_falling_sections(md_doc, toc).strip()
    assert actual == expected


def test_complement_falling_sections_with_ignored():
    toc = TableOfContents(
        titles=[
            SectionTitle(number="1", name="One"),
            SectionTitle(number="1.1", name="One one"),
            SectionTitle(number="1.1.1", name="One one one"),
        ]
    )

    md_doc = """\
## Ignored Section A
This is the fisrt sentence.

## 1.1 One One
This is the second sentence.
This is the third sentence.

#### Ignored Section B
This is the forth sentence.
"""

    filled_md_doc = """\
# 1 One
[NULL]

## 1.1 One one
This is the second sentence.
This is the third sentence.

#### Ignored Section B
This is the forth sentence.

### 1.1.1 One one one
[NULL]
"""

    expected = filled_md_doc.strip()
    actual = complement_falling_sections(md_doc, toc).strip()
    assert actual == expected
