Skip to content

Commit

Permalink
move doctests to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bernt-matthias committed Jan 13, 2022
1 parent c1604f5 commit 7ff99ec
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 91 deletions.
97 changes: 6 additions & 91 deletions lib/galaxy/util/xml_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,97 +160,12 @@ def _expand_macro(element, expand_el, macros, tokens):

def _expand_yield_statements(macro_def, expand_el):
"""
Modifies the macro_def element by replacing all <yield/> tags below the
macro_def element by the children of the expand_el
>>> from galaxy.util import XML, xml_to_string
>>> expand_el = XML('''
... <expand macro="test">
... <token name="token1">
... <content_of_token1/>
... <more_content_of_token1/>
... </token>
... <sub_of_expand_1/>
... <token name="token2">
... <content_of_token2/>
... <more_content_of_token2/>
... </token>
... <sub_of_expand_2/>
... </expand>''')
>>> macro_def = XML('''
... <xml name="test">
... <A><yield/></A>
... <yield name="token1"/>
... <B><yield/><yield name="token2"/></B>
... </xml>''')
>>> _expand_yield_statements(macro_def, expand_el)
>>> print(xml_to_string(macro_def, pretty=True))
<?xml version="1.0" ?>
<xml name="test">
<A>
<sub_of_expand_1/>
<sub_of_expand_2/>
</A>
<content_of_token1/>
<more_content_of_token1/>
<B>
<sub_of_expand_1/>
<sub_of_expand_2/>
<content_of_token2/>
<more_content_of_token2/>
</B>
</xml>
>>> # test replacement of top level yields
>>> macro_def = XML('''
... <xml name="test">
... <blah/>
... <yield/>
... <blah>
... <yield name="token1"/>
... </blah>
... <yield name="token2"/>
... </xml>''')
>>> _expand_yield_statements(macro_def, expand_el)
>>> print(xml_to_string(macro_def, pretty=True))
<?xml version="1.0" ?>
<xml name="test">
<blah/>
<sub_of_expand_1/>
<sub_of_expand_2/>
<blah>
<content_of_token1/>
<more_content_of_token1/>
</blah>
<content_of_token2/>
<more_content_of_token2/>
</xml>
>>> # test recursion like replacements
>>> expand_el = XML('''
... <expand macro="test">
... <token name="token1">
... <T1><yield name="token2"/></T1>
... </token>
... <token name="token2">
... <T2><yield/></T2>
... </token>
... <T/>
... </expand>''')
>>> macro_def = XML('''
... <xml name="test">
... <A><yield name="token1"/></A>
... </xml>''')
>>> _expand_yield_statements(macro_def, expand_el)
>>> print(xml_to_string(macro_def, pretty=True))
<?xml version="1.0" ?>
<xml name="test">
<A>
<T1>
<T2>
<T/>
</T2>
</T1>
</A>
</xml>
Modifies the macro_def element by replacing
1. all named yield tags by the content of the corresponding token tags
- token tags need to be direct children of the expand
- processed in order of definition of the token tags
2. all unnamed yield tags by the non-token children of the expand tag
"""
# replace named yields
for token_el in expand_el.findall('./token'):
Expand Down
142 changes: 142 additions & 0 deletions test/unit/tool_util/test_tool_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,145 @@ def load(self, name="tool.xml", preprocess=True):
assert input_els[0].find("cow").text == "hello"
assert input_els[1].find("cow").text == "world"
assert input_els[2].find("cow").text == "the_default"

# test expansion of named and unnamed yield
# - named yields are replaced by content of the corresponding token
# - unnamed yields are replaced by all non-token elements of the expand tag
with TestToolDirectory() as tool_dir:
tool_dir.write('''
<tool>
<macros>
<xml name="test">
<A>
<yield/>
</A>
<yield name="token1"/>
<B>
<yield/>
<yield name="token2"/>
</B>
</xml>
</macros>
<expand macro="test">
<token name="token1">
<content_of_token1/>
<more_content_of_token1/>
</token>
<sub_of_expand_1/>
<token name="token2">
<content_of_token2/>
<more_content_of_token2/>
</token>
<sub_of_expand_2/>
</expand>
</tool>
''')
xml = tool_dir.load()
assert xml_to_string(xml, pretty=True) == '''<?xml version="1.0" ?>
<tool>
<macros>
</macros>
<A>
<sub_of_expand_1/>
<sub_of_expand_2/>
</A>
<content_of_token1/>
<more_content_of_token1/>
<B>
<sub_of_expand_1/>
<sub_of_expand_2/>
<content_of_token2/>
<more_content_of_token2/>
</B>
</tool>'''

# test replacement of multiple top level yield
with TestToolDirectory() as tool_dir:
tool_dir.write('''
<tool>
<macros>
<xml name="test">
<blah/>
<yield/>
<blah>
<yield name="token1"/>
</blah>
<yield name="token2"/>
</xml>
</macros>
<expand macro="test">
<token name="token1">
<content_of_token1/>
<more_content_of_token1/>
</token>
<sub_of_expand_1/>
<token name="token2">
<content_of_token2/>
<more_content_of_token2/>
</token>
<sub_of_expand_2/>
</expand>
</tool>
''')
xml = tool_dir.load()
assert xml_to_string(xml, pretty=True) == '''<?xml version="1.0" ?>
<tool>
<macros>
</macros>
<blah/>
<sub_of_expand_1/>
<sub_of_expand_2/>
<blah>
<content_of_token1/>
<more_content_of_token1/>
</blah>
<content_of_token2/>
<more_content_of_token2/>
</tool>'''


# test 'recursive' replacement with named yields
# since named yields are processed in order of the definition of the
# corresponding tokens:
# - replacing yield for token1 introduces yield for token2
# - replacing yield for token2 introduced unnamed yield
# - replacing unnamed yield gives the only non-token element of the expand
with TestToolDirectory() as tool_dir:
tool_dir.write('''
<tool>
<macros>
<xml name="test">
<A>
<yield name="token1"/>
</A>
</xml>
</macros>
<expand macro="test">
<token name="token1">
<T1>
<yield name="token2"/>
</T1>
</token>
<token name="token2">
<T2>
<yield/>
</T2>
</token>
<T/>
</expand>
</tool>''')

xml = tool_dir.load()
print(xml_to_string(xml, pretty=True))
assert xml_to_string(xml, pretty=True) == '''<?xml version="1.0" ?>
<tool>
<macros>
</macros>
<A>
<T1>
<T2>
<T/>
</T2>
</T1>
</A>
</tool>'''

0 comments on commit 7ff99ec

Please sign in to comment.