_labels == ['1', '2']
    assert n8675_309['h']['2'].child_labels == ['i']
    assert n8675_309['h']['2']['i'].child_labels == ['A']


def test_build_from_section_ambiguous_1():
    n8675_309 = _setup_for_ambiguous("(1) C")
    assert n8675_309.child_labels == ['g', 'h', 'i']


def test_build_from_section_ambiguous_3():
    n8675_309 = _setup_for_ambiguous("(3) D")
    assert n8675_309.child_labels == ['g', 'h']
    assert n8675_309['h'].child_labels == ['1', '2', '3']
    assert n8675_309['h']['2'].child_labels == ['i']


def test_build_from_section_collapsed():
    with section_ctx() as ctx:
        ctx.P("(a) aaa")
        ctx.P("(1) 111")
        ctx.child_from_string('<P>(2) 222—(i) iii. (A) AAA</P>')
        ctx.P("(B) BBB")
    n309 = section.build_from_section('8675', ctx.xml)[0]
    n309 = NodeAccessor(n309)
    assert n309.child_labels == ['a']
    assert n309['a'].child_labels == ['1', '2']
    assert n309['a']['2'].child_labels == ['i']
    assert n309['a']['2']['i'].child_labels == ['A', 'B']


def test_build_from_section_italic_levels():
    with section_ctx() as ctx:
        ctx.P("(a) aaa")
        ctx.P("(1) 111")
        ctx.P("(i) iii")
        ctx.P("(A) AAA")
        ctx.child_from_string('<P>(<E T="03">1</E>) i1i1i1</P>')
        ctx.child_from_string('<P>\n(<E T="03">2</E>) i2i2i2</P>')
    node = section.build_from_section('8675', ctx.xml)[0]
    node = NodeAccessor(node)
    assert node.child_labels == ['a']
    assert node['a'].child_labels == ['1']
    assert node['a']['1'].child_labels == ['i']
    assert node['a']['1']['i'].child_labels == ['A']
    assert node['a']['1']['i']['A'].child_labels == ['1', '2']


def test_build_from_section_bad_spaces():
    with section_ctx(section=16) as ctx:
        ctx.STARS()
        ctx.child_from_string(
            '<P>(b)<E T="03">General.</E>Content Content.</P>')
    node = section.build_from_section('8675', ctx.xml)[0]
    node = NodeAccessor(node)
    assert node.label == ['8675', '16']
    assert node.child_labels == ['b']
    assert node['b'].text == "(b) General. Content Content."


def test_build_from_section_section_with_nondigits():
    with section_ctx(section="309a") as ctx:
        ctx.P("Intro content here")
    node = section.build_from_section('8675', ctx.xml)[0]
    assert node.label == ['8675', '309a']
    assert node.children == []


def test_build_from_section_fp():
    with section_ctx() as ctx:
        ctx.P("(a) aaa")
        ctx.P("(b) bbb")
        ctx.FP("fpfpfp")
        ctx.P("(c) ccc")
    node = section.build_from_section('8675', ctx.xml)[0]
    node = NodeAccessor(node)
    assert node.child_labels == ['a', 'b', 'c']
    assert node['a'].child_labels == []
    assert node['b'].child_labels == ['p1']
    assert node['b']['p1'].child_labels == []
    assert node['c'].child_labels == []


def test_build_from_section_table():
    """Account for regtext with a table"""
    with section_ctx() as ctx:
        ctx.P("(a) aaaa")
        with ctx.GPOTABLE(CDEF="s25,10", COLS=2, OPTS="L2,i1"):
            with ctx.BOXHD():
                ctx.CHED(H=1)
                ctx.CHED("Header", H=1)
            with ctx.ROW():
                ctx.ENT("Left content", I="01")
                ctx.ENT("Right content")
    node = section.build_from_section('8675', ctx.xml)[0]
    node = NodeAccessor(node)
    assert node.child_labels == ['a']
    assert node['a'].child_labels == ['p1']
    assert node['a']['p1'].text == (
        "||Header|\n|---|---|\n|Left content|Right content|")
    assert node['a']['p1'].source_xml.tag == 'GPOTABLE'


def _add_table(ctx):
    with ctx.GPOTABLE(CDEF="6.1,6.1,5.2,5.2,5.2", COLS=5, OPTS="L2"):
        with ctx.BOXHD():
            ctx.CHED("Pounds", H=1)
            ctx.CHED("Over", H=2)
            ctx.CHED("Not over", H=2)
            ctx.CHED("From inhabited building distance (feet)", H=1)
            ctx.CHED("From public railroad and highway distance (feet)", H=1)
            ctx.CHED("From above ground magazine (feet)", H=1)
        with ctx.ROW():
            ctx.ENT("0", I="01")
            ctx.ENT("1,000")
            ctx.ENT("75")
            ctx.ENT("75")
            ctx.ENT("50")
        with ctx.ROW():
            ctx.ENT("1,000", I="01")
            ctx.ENT("5,000")
            ctx.ENT("115")
            ctx.ENT("115")
            ctx.ENT("75")


def test_build_from_section_extract_with_table():
    """Account for regtext with a table in an extract"""
    subject = "Table of distances for storage of low explosives."
    with XMLBuilder("SECTION") as ctx:
        ctx.SECTNO("§ 555.219")
        ctx.SUBJECT(subject)
        with ctx.EXTRACT():
            _add_table(ctx)

    node = section.build_from_section('555', ctx.xml)[0]
    node = NodeAccessor(node)
    assert node.title == '§ 555.219'+ subject
    assert node.node_type =='regtext'
    assert node.label == ['555', '219']
    assert node.child_labels == ['p1']
    assert node['p1'].node_type == 'extract'
    assert node['p1'].child_labels == ['p1']
    assert node['p1']['p1'].node_type =='regtext'
    assert node['p1']['p1'].source_xml.tag == 'GPOTABLE'
    assert node['p1']['p1'].tagged_text.startswith('<GPOTABLE')


def test_build_from_section_extract_with_table_and_headers():
    """Account for regtext with a header and a table in an extract"""
    subject = 'Table of distances for storage of low explosives.'
    table_first_header_text = (
        "Table: Department of Defense Ammunition and Explosives Standards, "
        "Table 5-4.1 Extract; 4145.27 M, March 1969"
    )
    table_second_header_text = (
        "Table: National Fire Protection Association (NFPA) Official "
        "Standard No. 492, 1968"
    )
    with XMLBuilder("SECTION") as ctx:
        ctx.SECTNO("§ 555.219")
        ctx.SUBJECT(subject)
        with ctx.EXTRACT():
            ctx.HD(table_first_header_text, SOURCE='HD1')
