Skip to content
Snippets Groups Projects
Commit 74f4e7ef authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

ENH: implementation of multi macro and additional tests

parent 7d5e3e88
No related branches found
No related tags found
2 merge requests!53Release 0.1,!25F macros
Pipeline #23202 passed with warnings
......@@ -134,4 +134,9 @@ def multimacro_constructor(loader, node):
It can be registered in pyaml using:
yaml.SafeLoader.add_constructor("!multimacro", multimacro_constructor)
"""
pass
res = dict()
for val in node.value:
res.update(macro_constructor(loader, val))
return res
......@@ -22,7 +22,10 @@
# ** end header
#
from caoscrawler.macros import defmacro_constructor, macro_constructor, multimacro_constructor
from caoscrawler.macros import (defmacro_constructor,
macro_constructor,
multimacro_constructor)
from caoscrawler.macros.macro_yaml_object import macro_store
import yaml
import pytest
......@@ -32,7 +35,11 @@ def register_macros():
yaml.SafeLoader.add_constructor("!macro", macro_constructor)
yaml.SafeLoader.add_constructor("!multimacro", multimacro_constructor)
def test_macros(register_macros):
@pytest.fixture
def macro_store_reset():
macro_store.clear()
def test_macros(register_macros, macro_store_reset):
dat = yaml.load("""
defs:
- !defmacro
......@@ -58,7 +65,7 @@ testnode:
assert "expanded_bla" not in dat["testnode"]["obl"]
assert "bla" not in dat["testnode"]["obl"]["expanded_yea"]
def test_macro_list_replacment(register_macros):
def test_macro_list_replacment(register_macros, macro_store_reset):
dat = yaml.load("""
defs:
- !defmacro
......@@ -84,3 +91,89 @@ testnode:
assert len(dat["testnode"]["obl"]["expanded_yea"]["blubb"]) == 2
assert dat["testnode"]["obl"]["expanded_yea"]["blubb"][0] == "ok4"
assert dat["testnode"]["obl"]["expanded_yea"]["blubb"][1]["yea"] == "$variable"
def test_multi_macros(register_macros, macro_store_reset):
dat = yaml.load("""
defs:
- !defmacro
name: test_one
params: {}
definition:
replaced1: ok
- !defmacro
name: test_two
params: {}
definition:
replaced2: ok
replaced3: ok
testnode:
obl: !multimacro
- name: test_one
- name: test_two
""", Loader=yaml.SafeLoader)
print(yaml.dump(dat))
assert dat["testnode"]["obl"]["replaced1"] == "ok"
assert dat["testnode"]["obl"]["replaced2"] == "ok"
assert dat["testnode"]["obl"]["replaced3"] == "ok"
@pytest.mark.xfail
def test_multi_macros_toplevel(register_macros, macro_store_reset):
"""
See: https://gitlab.indiscale.com/caosdb/src/caosdb-crawler/-/issues/23
"""
dat = yaml.load("""
defs:
- !defmacro
name: test_one
params: {}
definition:
replaced1: ok
- !defmacro
name: test_two
params: {}
definition:
replaced2: ok
replaced3: ok
testnode: !multimacro
- name: test_one
- name: test_two
""", Loader=yaml.SafeLoader)
assert dat["testnode"]["replaced1"] == "ok"
assert dat["testnode"]["replaced2"] == "ok"
assert dat["testnode"]["replaced3"] == "ok"
@pytest.mark.xfail
def test_replace_arbitrary_objects(register_macros, macro_store_reset):
"""
See: https://gitlab.indiscale.com/caosdb/src/caosdb-crawler/-/issues/24
"""
dat = yaml.load("""
defs:
- !defmacro
name: test
params:
b: 25
testvar_list:
- a
- $b
testvar_dict:
t1: a
t2: $b
definition:
replaced1:
$b: ok
c: $testvar_dict
d: $testvar_list
testnode:
obl: !macro
name: test
""", Loader=yaml.SafeLoader)
print(yaml.dump(dat))
assert dat["testnode"]["obl"]["replaced1"]["c"]["t1"] == "a"
assert dat["testnode"]["obl"]["replaced1"]["c"]["t2"] == "25"
assert dat["testnode"]["obl"]["replaced1"]["d"][0] == "a"
assert dat["testnode"]["obl"]["replaced1"]["d"][1] == "25"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment