Пример #1
0
def xml_compare(elem1, elem2, ellipsis=False):
    expect(elem1.tag) == elem2.tag
    for key, value in elem1.attrib.items():
        expect(elem2.attrib.get(key)) == value
    for key in elem2.attrib.keys():
        expect(elem1.attrib).contains(key)
    text1 = elem1.text.strip() if elem1.text else ''
    text2 = elem2.text.strip() if elem2.text else ''
    tail1 = elem1.tail.strip() if elem1.tail else ''
    tail2 = elem2.tail.strip() if elem2.tail else ''
    if ellipsis:
        if not ellipsis_match(text1, text2):
            raise ValueError('text mismatch: %r != %r' % (elem1.text,
                                                          elem2.text))
        if not ellipsis_match(tail1, tail2):
            raise ValueError('tail mismatch: %r != %r' % (elem1.text,
                                                          elem2.text))
    else:
        expect(text1) == text2
        expect(tail1) == tail2
    children1 = elem1.getchildren()
    children2 = elem2.getchildren()
    expect(len(children1)) == len(children2)
    for child1, child2 in zip(children1, children2):
        xml_compare(child1, child2, ellipsis)
Пример #2
0
def xml_compare(elem1, elem2, ellipsis=False):
    """Compare XML elements

    :param bool ellipsis: Support ellipsis for 'any' match
    """
    assert elem1.tag == elem2.tag
    for key, value in elem1.attrib.items():
        assert elem2.attrib.get(key) == value
    for key in elem2.attrib.keys():
        assert key in elem1.attrib
    text1 = elem1.text.strip() if elem1.text else ''
    text2 = elem2.text.strip() if elem2.text else ''
    tail1 = elem1.tail.strip() if elem1.tail else ''
    tail2 = elem2.tail.strip() if elem2.tail else ''
    if ellipsis:
        if not ellipsis_match(text1, text2):
            raise ValueError('text mismatch: %r != %r' % (elem1.text,
                                                          elem2.text))
        if not ellipsis_match(tail1, tail2):
            raise ValueError('tail mismatch: %r != %r' % (elem1.text,
                                                          elem2.text))
    else:
        assert text1 == text2
        assert tail1 == tail2
    children1 = elem1.getchildren()
    children2 = elem2.getchildren()
    assert len(children1) == len(children2)
    for child1, child2 in zip(children1, children2):
        xml_compare(child1, child2, ellipsis)
Пример #3
0
 def test_sunset(self, stdout):
     locations = NumberedPoints(['52.015;-0.221', '52.168;0.040'])
     locations.sun_events('sunset')
     lines = stdout.getvalue().splitlines()
     expect(ellipsis_match('Sunset at ... in location 1', lines[0])) \
         == True
     expect(ellipsis_match('Sunset at ... in location 2', lines[1])) \
         == True
Пример #4
0
 def test_sunset(self, capsys):
     locations = NumberedPoints(['52.015;-0.221', '52.168;0.040'])
     locations.sun_events('sunset')
     lines = capsys.readouterr()[0].splitlines()
     assert ellipsis_match('Sunset at ... in location 1', lines[0]) \
         == True
     assert ellipsis_match('Sunset at ... in location 2', lines[1]) \
         == True
Пример #5
0
def test_readme() -> None:
    with open("README.rst", "r") as fid:
        blocks = literal_blocks(fid.read())

    for block in blocks:
        if block.startswith("$"):
            lines = block.rstrip().splitlines()
            command = lines[0].lstrip("$ ")

            try:
                actual_lines = (
                    i.rstrip()
                    for i in subprocess.check_output(
                        command, shell=True, stderr=subprocess.PIPE
                    )
                    .decode("utf-8")
                    .rstrip()
                    .splitlines()
                )
            except subprocess.CalledProcessError as e:
                pytest.fail(e.stdout.decode("utf-8") + e.stderr.decode("utf-8"))

            expected = "\n".join(lines[1:])
            actual = "\n".join(actual_lines)

            if ELLIPSIS_MARKER in expected:
                assert ellipsis_match(expected, actual)
            else:
                assert expected == actual