"""Helpers for unit tests.""" from textwrap import dedent from typing import cast from lxml import etree from src.ddex.e import E class Helpers(): """Helpers for tests.""" @staticmethod def print_xml(element) -> str: """Render the element to an XML string.""" # Add a test root to catch xmlns attributes element = E('TestRoot', element) etree.indent(element, space=' ') document_buffer = etree.tostring( element, encoding='utf-8', pretty_print=True, ) stringified = cast(bytes, document_buffer).decode('utf-8') # Strip TestRoot middle = dedent('\n'.join(stringified.split('\n')[1:-2])) return middle