How to use assert_matches method in PyHamcrest

Best Python code snippet using PyHamcrest_python

test_xacro.py

Source:test_xacro.py Github

copy

Full Screen

...263class TestXacroBase(unittest.TestCase):264 def __init__(self, *args, **kwargs):265 super(TestXacroBase, self).__init__(*args, **kwargs)266 self.ignore_nodes = []267 def assert_matches(self, a, b):268 self.assertTrue(xml_matches(a, b, self.ignore_nodes))269 def quick_xacro(self, xml, cli=None, **kwargs):270 args = {}271 if cli:272 opts, _ = xacro.cli.process_args(cli, require_input=False)273 args.update(vars(opts)) # initialize with cli args274 args.update(kwargs) # explicit function args have highest priority275 doc = xacro.parse(xml)276 xacro.process_doc(doc, **args)277 return doc278 def run_xacro(self, input_path, *args):279 args = list(args)280 subprocess.call(['xacro', input_path] + args)281# class to match XML docs while ignoring any comments282class TestXacroCommentsIgnored(TestXacroBase):283 def __init__(self, *args, **kwargs):284 super(TestXacroCommentsIgnored, self).__init__(*args, **kwargs)285 self.ignore_nodes = [xml.dom.Node.COMMENT_NODE]286 def test_pr2(self):287 # run xacro on the pr2 tree snapshot288 test_dir = os.path.abspath(os.path.dirname(__file__))289 pr2_xacro_path = os.path.join(test_dir, 'robots', 'pr2', 'pr2.urdf.xacro')290 pr2_golden_parse_path = os.path.join(test_dir, 'robots', 'pr2', 'pr2_1.11.4.xml')291 self.assert_matches(292 xml.dom.minidom.parse(pr2_golden_parse_path),293 self.quick_xacro(open(pr2_xacro_path)))294# standard test class (including the test from TestXacroCommentsIgnored)295class TestXacro(TestXacroCommentsIgnored):296 def __init__(self, *args, **kwargs):297 super(TestXacroCommentsIgnored, self).__init__(*args, **kwargs)298 self.ignore_nodes = []299 def test_invalid_property_name(self):300 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">301 <xacro:property name="invalid.name"/></a>'''302 self.assertRaises(xacro.XacroException, self.quick_xacro, src)303 def test_dynamic_macro_names(self):304 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">305 <xacro:macro name="foo"><a>foo</a></xacro:macro>306 <xacro:macro name="bar"><b>bar</b></xacro:macro>307 <xacro:property name="var" value="%s"/>308 <xacro:call macro="${var}"/></a>'''309 res = '''<a>%s</a>'''310 self.assert_matches(self.quick_xacro(src % "foo"), res % "<a>foo</a>")311 self.assert_matches(self.quick_xacro(src % "bar"), res % "<b>bar</b>")312 def test_dynamic_macro_name_clash(self):313 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">314 <xacro:macro name="foo"><a name="foo"/></xacro:macro>315 <xacro:macro name="call"><a name="bar"/></xacro:macro>316 <xacro:call macro="foo"/></a>'''317 self.assertRaises(xacro.XacroException, self.quick_xacro, src)318 def test_dynamic_macro_undefined(self):319 self.assertRaises(xacro.XacroException,320 self.quick_xacro,321 '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">322 <xacro:call macro="foo"/></a>''')323 def test_macro_undefined(self):324 self.assertRaises(xacro.XacroException,325 self.quick_xacro,326 '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">327 <xacro:undefined><foo/><bar/></xacro:undefined></a>''')328 def test_xacro_element(self):329 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">330 <xacro:macro name="foo" params="name"><xacro:element xacro:name="${name}"/></xacro:macro>331 <xacro:foo name="A"/>332 <xacro:foo name="B"/>333</a>'''334 res = '''<a><A/><B/></a>'''335 self.assert_matches(self.quick_xacro(src), res)336 def test_xacro_attribute(self):337 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">338 <xacro:macro name="foo" params="name value">339 <tag><xacro:attribute name="${name}" value="${value}"/></tag>340 </xacro:macro>341 <xacro:foo name="A" value="foo"/>342 <xacro:foo name="B" value="bar"/>343</a>'''344 res = '''<a><tag A="foo"/><tag B="bar"/></a>'''345 self.assert_matches(self.quick_xacro(src), res)346 def test_inorder_processing(self):347 src = '''348<xml xmlns:xacro="http://www.ros.org/wiki/xacro">349 <xacro:property name="foo" value="1.0"/>350 <xacro:macro name="m" params="foo"><a foo="${foo}"/></xacro:macro>351 <xacro:m foo="1 ${foo}"/>352 <!-- now redefining the property and macro -->353 <xacro:property name="foo" value="2.0"/>354 <xacro:macro name="m" params="foo"><b bar="${foo}"/></xacro:macro>355 <xacro:m foo="2 ${foo}"/>356</xml>'''357 expected = '''358<xml>359 <a foo="1 1.0"/>360 <b bar="2 2.0"/>361</xml>362'''363 self.assert_matches(self.quick_xacro(src), expected)364 def test_should_replace_before_macroexpand(self):365 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">366<xacro:macro name="inner" params="*the_block">367 <in_the_inner><xacro:insert_block name="the_block" /></in_the_inner>368</xacro:macro>369<xacro:macro name="outer" params="*the_block">370 <in_the_outer><xacro:inner><xacro:insert_block name="the_block" /></xacro:inner></in_the_outer>371</xacro:macro>372<xacro:outer><woot /></xacro:outer></a>'''373 res = '''<a><in_the_outer><in_the_inner><woot /></in_the_inner></in_the_outer></a>'''374 self.assert_matches(self.quick_xacro(src), res)375 def test_evaluate_macro_params_before_body(self):376 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">377 <xacro:macro name="foo" params="lst">${lst[-1]}</xacro:macro>378 <xacro:foo lst="${[1,2,3]}"/></a>'''379 self.assert_matches(self.quick_xacro(src), '''<a>3</a>''')380 def test_macro_params_escaped_string(self):381 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">382 <xacro:macro name="foo" params="a='1 -2' c=3"><bar a="${a}" c="${c}"/></xacro:macro>383 <xacro:foo/></a>'''384 self.assert_matches(self.quick_xacro(src), '''<a><bar a="1 -2" c="3"/></a>''')385 def test_property_replacement(self):386 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">387 <xacro:property name="foo" value="42" />388 <the_foo result="${foo}" />389</a>'''390 res = '''<a><the_foo result="42"/></a>'''391 self.assert_matches(self.quick_xacro(src), res)392 def test_property_scope_parent(self):393 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">394 <xacro:macro name="foo" params="factor">395 <xacro:property name="foo" value="${21*factor}" scope="parent"/>396 </xacro:macro>397 <xacro:foo factor="2"/><a foo="${foo}"/></a>'''398 self.assert_matches(self.quick_xacro(src), '''<a><a foo="42"/></a>''')399 def test_property_scope_global(self):400 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">401 <xacro:macro name="foo" params="factor">402 <xacro:macro name="bar">403 <xacro:property name="foo" value="${21*factor}" scope="global"/>404 </xacro:macro>405 <xacro:bar/>406 </xacro:macro>407 <xacro:foo factor="2"/><a foo="${foo}"/></a>'''408 self.assert_matches(self.quick_xacro(src), '''<a><a foo="42"/></a>''')409 def test_property_in_comprehension(self):410 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">411 <xacro:property name="abc" value="${[1,2,3]}"/>412 <xacro:property name="xyz" value="${[abc[i]*abc[i] for i in [0,1,2]]}"/>413 ${xyz}414 </a>'''415 self.assert_matches(self.quick_xacro(src), '''<a>[1, 4, 9]</a>''')416 def test_math_ignores_spaces(self):417 src = '''<a><f v="${0.9 / 2 - 0.2}" /></a>'''418 self.assert_matches(self.quick_xacro(src), '''<a><f v="0.25" /></a>''')419 def test_substitution_args_find(self):420 resolved = self.quick_xacro('''<a>$(find xacro)/test/test_xacro.py</a>''').firstChild.firstChild.data421 self.assertEqual(os.path.realpath(resolved), os.path.realpath(__file__))422 def test_substitution_args_arg(self):423 res = '''<a><f v="my_arg" /></a>'''424 self.assert_matches(self.quick_xacro('''<a><f v="$(arg sub_arg)" /></a>''', cli=['sub_arg:=my_arg']), res)425 def test_escaping_dollar_braces(self):426 src = '''<a b="$${foo}" c="$$${foo}" d="text $${foo}" e="text $$${foo}" f="$$(pwd)" />'''427 res = '''<a b="${foo}" c="$${foo}" d="text ${foo}" e="text $${foo}" f="$(pwd)" />'''428 self.assert_matches(self.quick_xacro(src), res)429 def test_just_a_dollar_sign(self):430 src = '''<a b="$" c="text $" d="text $ text"/>'''431 self.assert_matches(self.quick_xacro(src), src)432 def test_multiple_insert_blocks(self):433 self.assert_matches(self.quick_xacro('''<a xmlns:xacro="http://www.ros.org/wiki/xacro">434<xacro:macro name="foo" params="*block">435 <xacro:insert_block name="block" />436 <xacro:insert_block name="block" />437</xacro:macro>438<xacro:foo>439 <a_block />440</xacro:foo>441</a>'''), '''<a>442 <a_block />443 <a_block />444</a>''')445 def test_multiple_blocks(self):446 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">447<xacro:macro name="foo" params="*block{A} *block{B}">448 <xacro:insert_block name="block1" />449 <xacro:insert_block name="block2" />450</xacro:macro>451<xacro:foo>452 <block1/>453 <block2/>454</xacro:foo>455</a>'''456 res = '''<a>457<block{A}/>458<block{B}/>459</a>'''460 # test both, reversal and non-reversal of block order461 for d in [dict(A='1', B='2'), dict(A='2', B='1')]:462 self.assert_matches(self.quick_xacro(src.format(**d)), res.format(**d))463 def test_integer_stays_integer(self):464 self.assert_matches(self.quick_xacro('''<a xmlns:xacro="http://www.ros.org/wiki/xacro">465<xacro:macro name="m" params="num">466 <test number="${num}" />467</xacro:macro>468<xacro:m num="100" />469</a>'''), '''470<a>471 <test number="100" />472</a>''')473 def test_insert_block_property(self):474 self.assert_matches(self.quick_xacro('''<a xmlns:xacro="http://www.ros.org/wiki/xacro">475<xacro:macro name="bar">bar</xacro:macro>476<xacro:property name="val" value="2" />477<xacro:property name="some_block">478 <some_block attr="${val}"><xacro:bar/></some_block>479</xacro:property>480<foo>481 <xacro:insert_block name="some_block" />482</foo>483</a>'''), '''484<a>485<foo><some_block attr="2">bar</some_block></foo>486</a>''')487 def test_include(self):488 src = '''<a xmlns:xacro="http://www.ros.org/xacro"><xacro:include filename="include1.xml"/></a>'''489 self.assert_matches(self.quick_xacro(src), '''<a><inc1/></a>''')490 def test_include_glob(self):491 src = '''<a xmlns:xacro="http://www.ros.org/xacro"><xacro:include filename="include{glob}.xml"/></a>'''492 res = '<a><inc1/><inc2/></a>'493 for pattern in ['*', '?', '[1-2]']:494 self.assert_matches(self.quick_xacro(src.format(glob=pattern)), res)495 def test_include_nonexistent(self):496 self.assertRaises(xacro.XacroException,497 self.quick_xacro, '''<a xmlns:xacro="http://www.ros.org/xacro">498 <xacro:include filename="include-nada.xml" /></a>''')499 def test_include_deprecated(self):500 # <include> tags with some non-trivial content should not issue the deprecation warning501 src = '''<a><include filename="nada"><tag/></include></a>'''502 with capture_stderr(self.quick_xacro, src) as (result, output):503 self.assert_matches(result, src)504 self.assertEqual(output, '')505 def test_include_from_variable(self):506 doc = '''<a xmlns:xacro="http://www.ros.org/xacro">507 <xacro:property name="file" value="include1.xml"/>508 <xacro:include filename="${file}" /></a>'''509 self.assert_matches(self.quick_xacro(doc), '''<a><inc1/></a>''')510 def test_include_recursive(self):511 self.assert_matches(self.quick_xacro('''512<a xmlns:xacro="http://www.ros.org/xacro">513 <xacro:include filename="include1.xml"/>514 <xacro:include filename="./include1.xml"/>515 <xacro:include filename="subdir/include-recursive.xacro"/>516</a>'''), '''517<a><inc1/><inc1/><subdir_inc1/><subdir_inc1/><inc1/></a>''')518 def test_include_with_namespace(self):519 src = '''520<a xmlns:xacro="http://www.ros.org/wiki/xacro">521 <xacro:property name="var" value="main"/>522 <xacro:include filename="include1.xacro" ns="A"/>523 <xacro:include filename="include2.xacro" ns="B"/>524 <xacro:A.foo/><B.foo/>525 <main var="${var}" A="${2*A.var}" B="${B.var+1}"/>526</a>'''527 res = '''528<a>529 <inc1/><inc2/><main var="main" A="2" B="3"/>530</a>'''531 self.assert_matches(self.quick_xacro(src), res)532 def test_boolean_if_statement(self):533 self.assert_matches(self.quick_xacro('''534<robot xmlns:xacro="http://www.ros.org/wiki/xacro">535 <xacro:if value="false">536 <a />537 </xacro:if>538 <xacro:if value="true">539 <b />540 </xacro:if>541</robot>'''), '''542<robot>543 <b />544</robot>''')545 def test_invalid_if_statement(self):546 self.assertRaises(xacro.XacroException,547 self.quick_xacro,548 '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">549 <xacro:if value="nonsense"><foo/></xacro:if></a>''')550 def test_integer_if_statement(self):551 self.assert_matches(self.quick_xacro('''552<robot xmlns:xacro="http://www.ros.org/wiki/xacro">553 <xacro:if value="${0*42}">554 <a />555 </xacro:if>556 <xacro:if value="0">557 <b />558 </xacro:if>559 <xacro:if value="${0}">560 <c />561 </xacro:if>562 <xacro:if value="${1*2+3}">563 <d />564 </xacro:if>565</robot>'''), '''566<robot>567 <d />568</robot>''')569 def test_float_if_statement(self):570 self.assert_matches(self.quick_xacro('''571<robot xmlns:xacro="http://www.ros.org/wiki/xacro">572 <xacro:if value="${3*0.0}">573 <a />574 </xacro:if>575 <xacro:if value="${3*0.1}">576 <b />577 </xacro:if>578</robot>'''), '''579<robot>580 <b />581</robot>''')582 def test_property_if_statement(self):583 self.assert_matches(self.quick_xacro('''584<robot xmlns:xacro="http://www.ros.org/wiki/xacro">585 <xacro:property name="condT" value="${True}"/>586 <xacro:property name="condF" value="${False}"/>587 <xacro:if value="${condF}"><a /></xacro:if>588 <xacro:if value="${condT}"><b /></xacro:if>589 <xacro:if value="${True}"><c /></xacro:if>590</robot>'''), '''591<robot>592 <b /><c />593</robot>''')594 def test_consecutive_if(self):595 self.assert_matches(self.quick_xacro('''596<a xmlns:xacro="http://www.ros.org/wiki/xacro">597 <xacro:if value="1"><xacro:if value="0"><a>bar</a></xacro:if></xacro:if>598</a>'''), '''<a/>''')599 def test_equality_expression_in_if_statement(self):600 self.assert_matches(self.quick_xacro('''601<a xmlns:xacro="http://www.ros.org/wiki/xacro">602 <xacro:property name="var" value="useit"/>603 <xacro:if value="${var == 'useit'}"><foo>bar</foo></xacro:if>604 <xacro:if value="${'use' in var}"><bar>foo</bar></xacro:if>605</a>'''), '''606<a>607 <foo>bar</foo>608 <bar>foo</bar>609</a>''')610 def test_no_evaluation(self):611 self.assert_matches(self.quick_xacro('''612<a xmlns:xacro="http://www.ros.org/wiki/xacro">613 <xacro:property name="xyz" value="5 -2"/>614 <foo>${xyz}</foo>615</a>'''), '''616<a>617 <foo>5 -2</foo>618</a>''')619 def test_math_expressions(self):620 self.assert_matches(self.quick_xacro('''621<a xmlns:xacro="http://www.ros.org/wiki/xacro">622 <foo function="${1. + sin(pi)}"/>623</a>'''), '''624<a>625 <foo function="1.0"/>626</a>''')627 def test_consider_non_elements_if(self):628 self.assert_matches(self.quick_xacro('''629<a xmlns:xacro="http://www.ros.org/wiki/xacro">630 <xacro:if value="1"><!-- comment --> text <b>bar</b></xacro:if>631</a>'''), '''632<a><!-- comment --> text <b>bar</b></a>''')633 def test_consider_non_elements_block(self):634 self.assert_matches(635 self.quick_xacro('''<a xmlns:xacro="http://www.ros.org/wiki/xacro">636<xacro:macro name="foo" params="*block">637 <!-- comment -->638 foo639 <xacro:insert_block name="block" />640</xacro:macro>641<xacro:foo>642 <!-- ignored comment -->643 ignored text644 <a_block />645</xacro:foo>646</a>'''), '''647<a>648 <!-- comment -->649 foo650 <a_block />651</a>''')652 def test_ignore_xacro_comments(self):653 self.assert_matches(self.quick_xacro('''654<a xmlns:xacro="http://www.ros.org/wiki/xacro">655 <!-- A -->656 <!-- ignore multiline comments before any xacro tag -->657 <!-- ignored -->658 <xacro:property name="foo" value="1"/>659 <!-- ignored -->660 <xacro:if value="1"><!-- B --></xacro:if>661 <!-- ignored -->662 <xacro:macro name="foo"><!-- C --></xacro:macro>663 <!-- ignored -->664 <xacro:foo/>665</a>'''), '''666<a><!-- A --><!-- B --><!-- C --></a>''')667 def test_recursive_evaluation(self):668 self.assert_matches(self.quick_xacro('''669<robot xmlns:xacro="http://www.ros.org/wiki/xacro">670 <xacro:property name="a" value=" 42 "/>671 <xacro:property name="a2" value="${ 2 * a }"/>672 <a doubled="${a2}"/>673</robot>'''), '''674<robot>675 <a doubled="84"/>676</robot>''')677 def test_recursive_evaluation_wrong_order(self):678 self.assert_matches(self.quick_xacro('''679<robot xmlns:xacro="http://www.ros.org/wiki/xacro">680 <xacro:property name="a2" value="${2*a}"/>681 <xacro:property name="a" value="42"/>682 <a doubled="${a2}"/>683</robot>'''), '''684<robot>685 <a doubled="84"/>686</robot>''')687 def test_recursive_definition(self):688 self.assertRaises(xacro.XacroException,689 self.quick_xacro, '''690<robot xmlns:xacro="http://www.ros.org/wiki/xacro">691 <xacro:property name="a" value="${a2}"/>692 <xacro:property name="a2" value="${2*a}"/>693 <a doubled="${a2}"/>694</robot>''')695 def test_multiple_recursive_evaluation(self):696 self.assert_matches(self.quick_xacro('''697<robot xmlns:xacro="http://www.ros.org/wiki/xacro">698 <xacro:property name="a" value="1"/>699 <xacro:property name="b" value="2"/>700 <xacro:property name="c" value="3"/>701 <xacro:property name="product" value="${a*b*c}"/>702 <answer product="${product}"/>703</robot>'''), '''704<robot>705 <answer product="6"/>706</robot>''')707 def test_multiple_definition_and_evaluation(self):708 self.assert_matches(self.quick_xacro('''709<robot xmlns:xacro="http://www.ros.org/wiki/xacro">710 <xacro:property name="a" value="42"/>711 <xacro:property name="b" value="${a}"/>712 <xacro:property name="b" value="${-a}"/>713 <xacro:property name="b" value="${a}"/>714 <answer b="${b} ${b} ${b}"/>715</robot>'''), '''716<robot>717 <answer b="42 42 42"/>718</robot>''')719 def test_transitive_evaluation(self):720 self.assert_matches(self.quick_xacro('''721<robot xmlns:xacro="http://www.ros.org/wiki/xacro">722 <xacro:property name="a" value="42"/>723 <xacro:property name="b" value="${a}"/>724 <xacro:property name="c" value="${b}"/>725 <xacro:property name="d" value="${c}"/>726 <answer d="${d}"/>727</robot>'''), '''728<robot>729 <answer d="42"/>730</robot>''')731 def test_multi_tree_evaluation(self):732 self.assert_matches(self.quick_xacro('''733<robot xmlns:xacro="http://www.ros.org/wiki/xacro">734 <xacro:property name="a" value="42"/>735 <xacro:property name="b" value="2.1"/>736 <xacro:property name="c" value="${a}"/>737 <xacro:property name="d" value="${b}"/>738 <xacro:property name="f" value="${c*d}"/>739 <answer f="${f}"/>740</robot>'''), '''741<robot>742 <answer f="88.2"/>743</robot>''')744 def test_from_issue(self):745 self.assert_matches(self.quick_xacro('''746<robot xmlns:xacro="http://www.ros.org/wiki/xacro">747 <xacro:property name="x" value="42"/>748 <xacro:property name="wheel_width" value="${x}"/>749 <link name="my_link">750 <origin xyz="0 0 ${wheel_width/2}"/>751 </link>752</robot>'''), '''753<robot>754 <link name="my_link">755 <origin xyz="0 0 21.0"/>756 </link>757</robot>''')758 def test_recursive_bad_math(self):759 self.assertRaises(xacro.XacroException, self.quick_xacro, '''760<robot xmlns:xacro="http://www.ros.org/wiki/xacro">761 <xacro:property name="x" value="0"/>762 <tag badness="${1/x}"/>763</robot>''')764 def test_default_param(self):765 self.assert_matches(self.quick_xacro('''766<robot xmlns:xacro="http://www.ros.org/wiki/xacro">767 <xacro:macro name="fixed_link" params="parent_link:=base_link child_link *joint_pose">768 <link name="${child_link}"/>769 <joint name="${child_link}_joint" type="fixed">770 <xacro:insert_block name="joint_pose" />771 <parent link="${parent_link}"/>772 <child link="${child_link}" />773 </joint>774 </xacro:macro>775 <xacro:fixed_link child_link="foo">776 <origin xyz="0 0 0" rpy="0 0 0" />777 </xacro:fixed_link >778</robot>'''), '''779<robot>780 <link name="foo"/>781 <joint name="foo_joint" type="fixed">782 <origin rpy="0 0 0" xyz="0 0 0"/>783 <parent link="base_link"/>784 <child link="foo"/>785 </joint>786</robot>''')787 def test_default_param_override(self):788 self.assert_matches(self.quick_xacro('''789<robot xmlns:xacro="http://www.ros.org/wiki/xacro">790 <xacro:macro name="fixed_link" params="parent_link:=base_link child_link *joint_pose">791 <link name="${child_link}"/>792 <joint name="${child_link}_joint" type="fixed">793 <xacro:insert_block name="joint_pose" />794 <parent link="${parent_link}"/>795 <child link="${child_link}" />796 </joint>797 </xacro:macro>798 <xacro:fixed_link child_link="foo" parent_link="bar">799 <origin xyz="0 0 0" rpy="0 0 0" />800 </xacro:fixed_link >801</robot>'''), '''802<robot>803 <link name="foo"/>804 <joint name="foo_joint" type="fixed">805 <origin rpy="0 0 0" xyz="0 0 0"/>806 <parent link="bar"/>807 <child link="foo"/>808 </joint>809</robot>''')810 def test_param_missing(self):811 self.assertRaises(xacro.XacroException, self.quick_xacro, '''812<robot xmlns:xacro="http://www.ros.org/wiki/xacro">813 <xacro:macro name="fixed_link" params="parent_link child_link *joint_pose">814 <link name="${child_link}"/>815 <joint name="${child_link}_joint" type="fixed">816 <xacro:insert_block name="joint_pose" />817 <parent link="${parent_link}"/>818 <child link="${child_link}" />819 </joint>820 </xacro:macro>821 <xacro:fixed_link child_link="foo">822 <origin xyz="0 0 0" rpy="0 0 0" />823 </xacro:fixed_link >824</robot>''')825 def test_default_arg(self):826 self.assert_matches(self.quick_xacro('''827<robot xmlns:xacro="http://www.ros.org/wiki/xacro">828 <xacro:arg name="foo" default="2"/>829 <link name="my_link">830 <origin xyz="0 0 $(arg foo)"/>831 </link>832</robot>833'''), '''834<robot>835 <link name="my_link">836 <origin xyz="0 0 2"/>837 </link>838</robot>''')839 def test_default_arg_override(self):840 self.assert_matches(self.quick_xacro('''841<robot xmlns:xacro="http://www.ros.org/wiki/xacro">842 <xacro:arg name="foo" default="2"/>843 <link name="my_link">844 <origin xyz="0 0 $(arg foo)"/>845 </link>846</robot>847''', ['foo:=4']), '''848<robot>849 <link name="my_link">850 <origin xyz="0 0 4"/>851 </link>852</robot>''')853 def test_default_arg_missing(self):854 self.assertRaises(Exception, self.quick_xacro, '''855<a xmlns:xacro="http://www.ros.org/wiki/xacro">856 <a arg="$(arg foo)"/>857</a>858''')859 def test_default_arg_empty(self):860 self.assert_matches(self.quick_xacro('''861<a xmlns:xacro="http://www.ros.org/wiki/xacro">862<xacro:arg name="foo" default=""/>$(arg foo)</a>'''), '''<a/>''')863 def test_broken_include_error_reporting(self):864 self.assertRaises(xml.parsers.expat.ExpatError, self.quick_xacro,865 '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">866 <xacro:include filename="broken.xacro"/></a>''')867 self.assertEqual(xacro.filestack, [None, './broken.xacro'])868 def test_broken_input_doesnt_create_empty_output_file(self):869 # run xacro on broken input file to make sure we don't create an870 # empty output file871 tmp_dir_name = tempfile.mkdtemp() # create directory we can trash872 output_path = os.path.join(tmp_dir_name, "should_not_exist")873 self.run_xacro('broken.xacro', '-o', output_path)874 output_file_created = os.path.isfile(output_path)875 shutil.rmtree(tmp_dir_name) # clean up after ourselves876 self.assertFalse(output_file_created)877 def test_create_subdirs(self):878 # run xacro to create output file in non-existent directory879 # to make sure this directory will be created by xacro880 tmp_dir_name = tempfile.mkdtemp() # create directory we can trash881 shutil.rmtree(tmp_dir_name) # ensure directory is removed882 output_path = os.path.join(tmp_dir_name, "out")883 self.run_xacro('include1.xml', '-o', output_path)884 output_file_created = os.path.isfile(output_path)885 shutil.rmtree(tmp_dir_name) # clean up after ourselves886 self.assertTrue(output_file_created)887 def test_iterable_literals_plain(self):888 self.assert_matches(self.quick_xacro('''889<a xmlns:xacro="http://www.ros.org/wiki/xacro">890 <xacro:property name="list" value="[0, 1+1, 2]"/>891 <xacro:property name="tuple" value="(0,1+1,2)"/>892 <xacro:property name="dict" value="{'a':0, 'b':1+1, 'c':2}"/>893 <a list="${list}" tuple="${tuple}" dict="${dict}"/>894</a>'''), '''895<a>896 <a list="[0, 1+1, 2]" tuple="(0,1+1,2)" dict="{'a':0, 'b':1+1, 'c':2}"/>897</a>''')898 def test_iterable_literals_eval(self):899 self.assert_matches(self.quick_xacro('''900<a xmlns:xacro="http://www.ros.org/wiki/xacro">901 <xacro:property name="list" value="${[0, 1+1, 2]}"/>902 <xacro:property name="tuple" value="${(0,1+1,2)}"/>903 <xacro:property name="dic" value="${dict(a=0, b=1+1, c=2)}"/>904 <a list="${list}" tuple="${tuple}" dict="${dic}"/>905</a>'''), '''906<a>907 <a list="[0, 2, 2]" tuple="(0, 2, 2)" dict="{'a': 0, 'c': 2, 'b': 2}"/>908</a>''')909 def test_literals_eval(self):910 self.assert_matches(self.quick_xacro('''911<a xmlns:xacro="http://www.ros.org/wiki/xacro">912 <xacro:property name="f" value="1.23"/>913 <xacro:property name="i" value="123"/>914 <xacro:property name="s" value="1_2_3"/>915 float=${f+1} int=${i+1} string=${s}916</a>'''), '''917<a>918 float=2.23 int=124 string=1_2_3919</a>''')920 def test_enforce_xacro_ns(self):921 self.assert_matches(self.quick_xacro('''922<a xmlns:xacro="http://www.ros.org/wiki/xacro">923 <arg name="foo" value="bar"/>924 <include filename="foo"/>925</a>''', xacro_ns=False), '''926<a>927 <arg name="foo" value="bar"/>928 <include filename="foo"/>929</a>''')930 def test_issue_68_numeric_arg(self):931 # If a property is assigned from a substitution arg, then this properties' value was932 # no longer converted to a python type, so that e.g. 0.5 remained u'0.5'.933 # If this property is then used in a numerical expression an exception is thrown.934 self.assert_matches(self.quick_xacro('''935<a xmlns:xacro="http://www.ros.org/wiki/xacro">936 <xacro:arg name="foo" default="0.5"/>937 <xacro:property name="prop" value="$(arg foo)" />938 <a prop="${prop-0.3}"/>939</a>940'''), '''941<a>942 <a prop="0.2"/>943</a>''')944 def test_transitive_arg_evaluation(self):945 self.assert_matches(self.quick_xacro('''946<a xmlns:xacro="http://www.ros.org/wiki/xacro">947 <xacro:arg name="foo" default="0.5"/>948 <xacro:arg name="bar" default="$(arg foo)"/>949 <xacro:property name="prop" value="$(arg bar)" />950 <a prop="${prop-0.3}"/>951</a>952'''), '''953<a>954 <a prop="0.2"/>955</a>''')956 def test_macro_name_with_colon(self):957 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">958 <xacro:macro name="xacro:my_macro"><foo/></xacro:macro>959 <xacro:my_macro/>960 </a>'''961 res = '''<a><foo/></a>'''962 with capture_stderr(self.quick_xacro, src) as (result, output):963 self.assert_matches(result, res)964 self.assertTrue("macro names must not contain prefix 'xacro:'" in output)965 def test_overwrite_globals(self):966 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">967 <xacro:property name="pi" value="3.14"/></a>'''968 with capture_stderr(self.quick_xacro, src) as (result, output):969 self.assert_matches(result, '<a/>')970 self.assertTrue(output)971 def test_no_double_evaluation(self):972 src = '''973<a xmlns:xacro="http://www.ros.org/xacro">974 <xacro:macro name="foo" params="a b:=${a} c:=$${a}"> a=${a} b=${b} c=${c} </xacro:macro>975 <xacro:property name="a" value="1"/>976 <xacro:property name="d" value="$${a}"/>977 <d d="${d}"><xacro:foo a="2"/></d>978</a>'''979 res = '''<a><d d="${a}"> a=2 b=1 c=${a} </d></a>'''980 self.assert_matches(self.quick_xacro(src), res)981 def test_property_forwarding(self):982 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">983 <xacro:property name="arg" value="42"/>984 <xacro:macro name="foo" params="arg:=^%s">${arg}</xacro:macro>985 <xacro:foo/>986 </a>'''987 res = '''<a>%s</a>'''988 self.assert_matches(self.quick_xacro(src % ''), res % '42')989 self.assert_matches(self.quick_xacro(src % '|'), res % '42')990 self.assert_matches(self.quick_xacro(src % '|6'), res % '42')991 def test_extension_in_expression(self):992 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">${2*'$(arg var)'}</a>'''993 res = '''<a>%s</a>'''994 self.assert_matches(self.quick_xacro(src, ['var:=xacro']), res % (2 * 'xacro'))995 def test_expression_in_extension(self):996 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">$(arg ${'v'+'ar'})</a>'''997 res = '''<a>%s</a>'''998 self.assert_matches(self.quick_xacro(src, ['var:=xacro']), res % 'xacro')999 def test_target_namespace(self):1000 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro" xacro:targetNamespace="http://www.ros.org"/>'''1001 res = '''<a xmlns="http://www.ros.org"/>'''1002 self.assert_matches(self.quick_xacro(src), res)1003 def test_target_namespace_only_from_root(self):1004 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro"><b xacro:targetNamespace="http://www.ros.org"/></a>'''1005 res = '''<a><b/></a>'''1006 self.assert_matches(self.quick_xacro(src), res)1007 def test_include_lazy(self):1008 doc = ('''<a xmlns:xacro="http://www.ros.org/xacro">1009 <xacro:if value="false"><xacro:include filename="non-existent"/></xacro:if></a>''')1010 self.assert_matches(self.quick_xacro(doc), '''<a/>''')1011 def test_issue_63_fixed_with_inorder_processing(self):1012 self.assert_matches(self.quick_xacro('''1013<a xmlns:xacro="http://www.ros.org/wiki/xacro">1014 <xacro:arg name="has_stuff" default="false"/>1015 <xacro:if value="$(arg has_stuff)">1016 <xacro:include file="$(find nonexistent_package)/stuff.urdf" />1017 </xacro:if>1018</a>'''), '<a/>')1019 def test_include_from_macro(self):1020 src = '''1021 <a xmlns:xacro="http://www.ros.org/xacro">1022 <xacro:macro name="foo" params="file:=include1.xml"><xacro:include filename="${file}"/></xacro:macro>1023 <xacro:foo/>1024 <xacro:foo file="${abs_filename('include1.xml')}"/>1025 <xacro:include filename="subdir/foo.xacro"/>1026 <xacro:foo file="$(cwd)/subdir/include1.xml"/>1027 </a>'''1028 res = '''<a><inc1/><inc1/><subdir_inc1/><subdir_inc1/></a>'''1029 self.assert_matches(self.quick_xacro(src), res)1030 def test_dotify(self):1031 src = '''1032 <a xmlns:xacro="http://www.ros.org/xacro">1033 <xacro:property name="settings" value="${dotify(dict(a=1, b=2))}"/>1034 ${settings.a + settings.b}1035 </a>'''1036 res = '''<a>3</a>'''1037 self.assert_matches(self.quick_xacro(src), res)1038 def test_yaml_support(self):1039 src = '''1040<a xmlns:xacro="http://www.ros.org/wiki/xacro">1041 <xacro:property name="settings" value="${load_yaml('settings.yaml')}"/>1042 <xacro:property name="type" value="$(arg type)"/>1043 <xacro:include filename="${settings['arms'][type]['file']}"/>1044 <xacro:call macro="${settings['arms'][type]['macro']}"/>1045</a>'''1046 res = '''<a><{tag}/></a>'''1047 for i in ['inc1', 'inc2']:1048 self.assert_matches(self.quick_xacro(src, cli=['type:=%s' % i]),1049 res.format(tag=i))1050 def test_yaml_support_dotted(self):1051 src = '''1052<a xmlns:xacro="http://www.ros.org/wiki/xacro">1053 <xacro:property name="settings" value="${load_yaml('settings.yaml')}"/>1054 <xacro:property name="type" value="$(arg type)"/>1055 <xacro:include filename="${settings.arms[type].file}"/>1056 <xacro:call macro="${settings.arms[type].macro}"/>1057</a>'''1058 res = '''<a><{tag}/></a>'''1059 for i in ['inc1', 'inc2']:1060 self.assert_matches(self.quick_xacro(src, cli=['type:=%s' % i]),1061 res.format(tag=i))1062 def test_yaml_support_dotted_key_error(self):1063 src = '''1064<a xmlns:xacro="http://www.ros.org/wiki/xacro">1065 <xacro:property name="settings" value="${load_yaml('settings.yaml')}"/>1066 <xacro:property name="bar" value="${settings.baz}"/>1067 ${bar}1068</a>'''1069 self.assertRaises(xacro.XacroException, self.quick_xacro, src)1070 def test_yaml_support_dotted_arith(self):1071 src = '''1072<a xmlns:xacro="http://www.ros.org/wiki/xacro">1073 <xacro:property name="settings" value="${load_yaml('settings.yaml')}"/>1074 <xacro:property name="bar" value="${settings.arms.inc2.props.port + 1}"/>1075 ${bar}1076</a>'''1077 res = '''<a>4243</a>'''1078 self.assert_matches(self.quick_xacro(src), res)1079 def test_yaml_support_key_in_dict(self):1080 src = '''1081<a xmlns:xacro="http://www.ros.org/wiki/xacro">1082 <xacro:property name="settings" value="${load_yaml('settings.yaml')}"/>1083 ${'arms' in settings} ${'baz' in settings}1084</a>'''1085 res = '''<a>True False</a>'''1086 self.assert_matches(self.quick_xacro(src), res)1087 def test_yaml_support_list_of_x(self):1088 src = '''1089 <a xmlns:xacro="http://www.ros.org/wiki/xacro">1090 <xacro:property name="list" value="${load_yaml('list.yaml')}"/>1091 ${list[0][1]} ${list[1][0]} ${list[2].a.A} ${list[2].a.B[0]} ${list[2].a.B[1]} ${list[2].b[0]}1092 </a>'''1093 res = '''<a>A2 B1 1 2 3 4</a>'''1094 self.assert_matches(self.quick_xacro(src), res)1095 def test_yaml_custom_constructors(self):1096 src = '''1097<a xmlns:xacro="http://www.ros.org/wiki/xacro">1098 <xacro:property name="values" value="${load_yaml('constructors.yaml')}"/>1099 <values a="${values.a}" b="${values.b}" c="${values.c}"/>1100</a>'''1101 res = '''<a><values a="{}" b="{}" c="42"/></a>'''.format(math.pi, 0.5*math.pi)1102 self.assert_matches(self.quick_xacro(src), res)1103 def test_yaml_custom_constructors_illegal_expr(self):1104 for file in ['constructors_bad1.yaml', 'constructors_bad2.yaml']:1105 src = '''1106<a xmlns:xacro="http://www.ros.org/wiki/xacro">1107 <xacro:property name="values" value="${{load_yaml({file})}}"/>1108 <values a="${{values.a}}" />1109</a>'''1110 self.assertRaises(xacro.XacroException, self.quick_xacro, src.format(file=file))1111 def test_xacro_exist_required(self):1112 src = '''1113<a xmlns:xacro="http://www.ros.org/wiki/xacro">1114 <xacro:include filename="non-existent.xacro"/>1115</a>'''1116 self.assertRaises(xacro.XacroException, self.quick_xacro, src)1117 def test_xacro_exist_optional(self):1118 src = '''1119<a xmlns:xacro="http://www.ros.org/wiki/xacro">1120 <xacro:include filename="non-existent.xacro" optional="True"/>1121</a>'''1122 res = '''<a></a>'''1123 self.assert_matches(self.quick_xacro(src), res)1124 def test_macro_default_param_evaluation_order(self):1125 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1126<xacro:macro name="foo" params="arg:=${2*foo}">1127 <xacro:property name="foo" value="-"/>1128 <f val="${arg}"/>1129</xacro:macro>1130<xacro:property name="foo" value="${3*7}"/>1131<xacro:foo/>1132<xacro:property name="foo" value="*"/>1133<xacro:foo/>1134</a>'''1135 res = '''<a>1136<f val="42"/><f val="**"/></a>'''1137 self.assert_matches(self.quick_xacro(src), res)1138 def test_default_property(self):1139 src = '''1140 <a xmlns:xacro="http://www.ros.org/xacro">1141 <xacro:property name="prop" default="false"/>1142 <xacro:unless value="${prop}">1143 <foo/>1144 <xacro:property name="prop" value="true"/>1145 </xacro:unless>1146 <!-- second foo should be ignored -->1147 <xacro:unless value="${prop}">1148 <foo/>1149 <xacro:property name="prop" value="true"/>1150 </xacro:unless>1151 </a>'''1152 res = '''<a><foo/></a>'''1153 self.assert_matches(self.quick_xacro(src), res)1154 def test_unicode_literal_parsing(self):1155 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">🍔 </a>'''1156 self.assert_matches(self.quick_xacro(src), '''<a>🍔 </a>''')1157 def test_unicode_property(self):1158 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1159<xacro:property name="burger" value="🍔"/>1160${burger}</a>'''1161 res = '''<a>🍔</a>'''1162 self.assert_matches(self.quick_xacro(src), res)1163 def test_unicode_property_attribute(self):1164 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1165<xacro:property name="burger" value="🍔"/>1166<b c="${burger}"/></a>'''1167 res = '''<a><b c="🍔"/></a>'''1168 self.assert_matches(self.quick_xacro(src), res)1169 def test_unicode_property_block(self):1170 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1171<xacro:property name="burger">1172🍔1173</xacro:property>1174<xacro:insert_block name="burger"/></a>'''1175 res = '''<a>🍔</a>'''1176 self.assert_matches(self.quick_xacro(src), res)1177 def test_unicode_conditional(self):1178 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1179<xacro:property name="burger" value="🍔"/>1180<xacro:if value="${burger == u'🍔'}">1181🍟1182</xacro:if>1183</a>'''1184 res = '''<a>🍟</a>'''1185 self.assert_matches(self.quick_xacro(src), res)1186 def test_unicode_macro(self):1187 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1188<xacro:macro name="burger" params="how_many">1189${u'🍔' * how_many}1190</xacro:macro>1191<xacro:burger how_many="4"/>1192</a>'''1193 res = '''<a>🍔🍔🍔🍔</a>'''1194 self.assert_matches(self.quick_xacro(src), res)1195 def test_unicode_file(self):1196 # run the full xacro processing pipeline on a file with1197 # unicode characters in it and make sure the output is correct1198 test_dir = os.path.abspath(os.path.dirname(__file__))1199 input_path = os.path.join(test_dir, 'emoji.xacro')1200 tmp_dir_name = tempfile.mkdtemp() # create directory we can trash1201 output_path = os.path.join(tmp_dir_name, "out.xml")1202 self.run_xacro(input_path, '-o', output_path)1203 self.assertTrue(os.path.isfile(output_path))1204 self.assert_matches(xml.dom.minidom.parse(output_path), '''<robot>🍔</robot>''')1205 shutil.rmtree(tmp_dir_name) # clean up after ourselves1206 def test_macro_name_clash(self):1207 src = '''<a xmlns:xacro="http://www.ros.org/wiki/xacro">1208<xacro:macro name="foo"><bar/></xacro:macro>1209<foo/></a>1210'''1211 self.assert_matches(self.quick_xacro(src, ['--xacro-ns']), '<a><foo/></a>')1212 self.assert_matches(self.quick_xacro(src), '<a><bar/></a>')1213 def test_process_return_value(self):1214 test_dir = os.path.abspath(os.path.dirname(__file__))1215 input_path = os.path.join(test_dir, 'emoji.xacro')1216 self.assert_matches(xacro.process(input_path), '<robot>🍔</robot>')1217 def test_invalid_syntax(self):1218 self.assertRaises(xacro.XacroException, self.quick_xacro, '<a>a${</a>')1219 self.assertRaises(xacro.XacroException, self.quick_xacro, '<a>${b</a>')1220 self.assertRaises(xacro.XacroException, self.quick_xacro, '<a>${{}}</a>')1221 self.assertRaises(xacro.XacroException, self.quick_xacro, '<a>a$(</a>')1222 self.assertRaises(xacro.XacroException, self.quick_xacro, '<a>$(b</a>')1223if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run PyHamcrest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful