Skip to content

Commit

Permalink
wip: start to try string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rpoyner-tri committed Jul 7, 2023
1 parent 4c29c63 commit 96feca8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
21 changes: 15 additions & 6 deletions bindings/pydrake/multibody/_inertia_fixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ def format_inertia(self, input_text: str, inertial_facts: ElementFacts,
raise NotImplementedError


def is_synth_element(el: ElementFacts) -> bool:
"""Return true if the ElementFacts is synthetic.
Synthetic elements are placeholders for later expansion into output XML.
"""
return el.end.index == el.start.index


def make_synth_element(parent: ElementFacts, name: str) -> ElementFacts:
"""Make a new synthetic child ElementFacts for a given parent ElementFacts.
The parent must already have at least one child.
Expand All @@ -116,7 +124,7 @@ def make_synth_element(parent: ElementFacts, name: str) -> ElementFacts:
kid0 = parent.children[0]
synth_el = ElementFacts(kid0.prior_data, name, {},
kid0.depth, kid0.start)
synth_el.end = kid0.start
synth_el.end = synth_el.start
return synth_el


Expand Down Expand Up @@ -152,19 +160,20 @@ def adjusted_element_end_index(input_text: str, facts: ElementFacts) -> int:
"""Returns the index of the end of all of the text (including the element
closure), when given an ElementFacts produced by the expat parse.
"""
if facts.start.index == facts.end.index:
if is_synth_element(facts):
# Empty, synthetic, "pseudo" element.
return facts.end.index

if input_text[facts.end.index:].startswith(f"</{facts.name}"):
# Typical case:
# `<inertial>...</inertial>`.
# ^ # Raw index points here.
# ^ # Adjusted index points here.
return facts.end.index + input_text[facts.end.index:].find('>') + 1
else:
# `<inertial/>` corner case.
# ^ # Raw index points here; good to go
return facts.end.index

# `<inertial/>` corner case.
# ^ # Raw index points here; good to go
return facts.end.index


def make_format_helpers(facts: ElementFacts) -> (
Expand Down
18 changes: 18 additions & 0 deletions bindings/pydrake/multibody/test/fix_inertia_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,28 @@
temp_directory,
)
from pydrake.multibody._inertia_fixer import (
fix_inertia_from_string,
InertiaFixer,
)


class TestFixInertiaFromString(unittest.TestCase):
def test_min_urdf(self):
input_text = '<robot name="a"/>'
output = fix_inertia_from_string(input_text, "urdf")
self.assertEqual(output, input_text)

def test_min_sdf(self):
input_text = """\
<sdf version="1.6">
<model name="a">
<link name="b"/>
</model>
</sdf>"""
output = fix_inertia_from_string(input_text, "sdf")
self.assertEqual(output, input_text)


class TestInertiaFixer(unittest.TestCase):
def setUp(self):
self._temp_dir = Path(temp_directory())
Expand Down

0 comments on commit 96feca8

Please sign in to comment.