Repository: roboflow/supervision
File: test/dataset/formats/test_pascal_voc.py (Python)

Code before:
            ET.fromstring(
                """<object><name>test</name><bndbox><xmin>0</xmin><ymin>0</ymin><xmax>10</xmax><ymax>10</ymax></bndbox></object>"""
            ),
            DoesNotRaise(),
        ),
        (
            [0, 0, 10, 10],
            "test",
            [[0, 0], [10, 0], [10, 10], [0, 10]],
            ET.fromstring(
                """<object><name>test</name><bndbox><xmin>0</xmin><ymin>0</ymin><xmax>10</xmax><ymax>10</ymax></bndbox><polygon><x1>0</x1><y1>0</y1><x2>10</x2><y2>0</y2><x3>10</x3><y3>10</y3><x4>0</x4><y4>10</y4></polygon></object>"""
            ),
            DoesNotRaise(),
        ),
    ],
)
def test_object_to_pascal_voc(
    xyxy: np.ndarray,
    name: str,
    polygon: Optional[np.ndarray],
    expected_result,
    exception: Exception,
):
    with exception:
        result = object_to_pascal_voc(xyxy=xyxy, name=name, polygon=polygon)
        with open("/tmp/test.xml", "w") as f:
            f.write(ET.tostring(result).decode())
        with open("/tmp/exptest.xml", "w") as f:
            f.write(ET.tostring(expected_result).decode())
        assert are_xml_elements_equal(result, expected_result)


@pytest.mark.parametrize(
    "polygon_element, expected_result, exception",
    [
        (
            ET.fromstring(
                """<polygon><x1>0</x1><y1>0</y1><x2>10</x2><y2>0</y2><x3>10</x3><y3>10</y3><x4>0</x4><y4>10</y4></polygon>"""
            ),
            [[0, 0], [10, 0], [10, 10], [0, 10]],
            DoesNotRaise(),
        )
    ],
)
def test_parse_polygon_points(
    polygon_element,
    expected_result: List[list],
    exception,
):
    with exception:
        result = parse_polygon_points(polygon_element)

Review comment (suggestion):
@kirilllzaitsev, what are we testing here? 👇🏻 

```python
with open("/tmp/test.xml", "w") as f:
    f.write(ET.tostring(result).decode())
with open("/tmp/exptest.xml", "w") as f:
    f.write(ET.tostring(expected_result).decode())
```

Code after:
            ET.fromstring(
                """<object><name>test</name><bndbox><xmin>0</xmin><ymin>0</ymin><xmax>10</xmax><ymax>10</ymax></bndbox></object>"""
            ),
            DoesNotRaise(),
        ),
        (
            [0, 0, 10, 10],
            "test",
            [[0, 0], [10, 0], [10, 10], [0, 10]],
            ET.fromstring(
                """<object><name>test</name><bndbox><xmin>0</xmin><ymin>0</ymin><xmax>10</xmax><ymax>10</ymax></bndbox><polygon><x1>0</x1><y1>0</y1><x2>10</x2><y2>0</y2><x3>10</x3><y3>10</y3><x4>0</x4><y4>10</y4></polygon></object>"""
            ),
            DoesNotRaise(),
        ),
    ],
)
def test_object_to_pascal_voc(
    xyxy: np.ndarray,
    name: str,
    polygon: Optional[np.ndarray],
    expected_result,
    exception: Exception,
):
    with exception:
        result = object_to_pascal_voc(xyxy=xyxy, name=name, polygon=polygon)
        assert are_xml_elements_equal(result, expected_result)


@pytest.mark.parametrize(
    "polygon_element, expected_result, exception",
    [
        (
            ET.fromstring(
                """<polygon><x1>0</x1><y1>0</y1><x2>10</x2><y2>0</y2><x3>10</x3><y3>10</y3><x4>0</x4><y4>10</y4></polygon>"""
            ),
            [[0, 0], [10, 0], [10, 10], [0, 10]],
            DoesNotRaise(),
        )
    ],
)
def test_parse_polygon_points(
    polygon_element,
    expected_result: List[list],
    exception,
):
    with exception:
        result = parse_polygon_points(polygon_element)
        assert result == expected_result


ONE_CLASS_N_BBOX = """<annotation><object><name>test</name><bndbox><xmin>0</xmin><ymin>0</ymin><xmax>10</xmax><ymax>10</ymax></bndbox></object><object><name>test</name><bndbox><xmin>10</xmin><ymin>10</ymin><xmax>20</xmax><ymax>20</ymax></bndbox></object></annotation>"""