Skip to content
Snippets Groups Projects
Commit 2c9531e9 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

Merge branch 'f-var-substitutions-macro-definitions' into 'dev'

TST: more tests for macros and support for variable substitutions in macro definitions

See merge request !76
parents ebb29ce0 2a2a7a55
No related branches found
No related tags found
2 merge requests!91Release 0.3,!76TST: more tests for macros and support for variable substitutions in macro definitions
Pipeline #32039 passed
...@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
definition. definition.
- JSON schema validation can also be used in the DictElementConverter - JSON schema validation can also be used in the DictElementConverter
- YAMLFileConverter class; to parse YAML files - YAMLFileConverter class; to parse YAML files
- Variables can now be substituted within the definition of yaml macros
### Changed ### ### Changed ###
...@@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -30,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated ### ### Deprecated ###
- The DictXYElements are now depricated and are now synonyms for the - The DictXYElements are now depricated and are now synonyms for the
XYElements. XYElements.
......
...@@ -135,6 +135,7 @@ def macro_constructor(loader, node): ...@@ -135,6 +135,7 @@ def macro_constructor(loader, node):
raise RuntimeError("params type not supported") raise RuntimeError("params type not supported")
else: else:
raise RuntimeError("params type must not be None") raise RuntimeError("params type must not be None")
params = substitute_dict(params, params)
definition = substitute_dict(macro.definition, params) definition = substitute_dict(macro.definition, params)
res.update(definition) res.update(definition)
else: else:
...@@ -146,6 +147,7 @@ def macro_constructor(loader, node): ...@@ -146,6 +147,7 @@ def macro_constructor(loader, node):
params.update(params_setter) params.update(params_setter)
else: else:
raise RuntimeError("params type not supported") raise RuntimeError("params type not supported")
params = substitute_dict(params, params)
definition = substitute_dict(macro.definition, params) definition = substitute_dict(macro.definition, params)
res.update(definition) res.update(definition)
else: else:
......
...@@ -456,3 +456,60 @@ extroot: !macro ...@@ -456,3 +456,60 @@ extroot: !macro
assert cfood["extroot"]["default_name"]["a"] == "default_name" assert cfood["extroot"]["default_name"]["a"] == "default_name"
assert cfood["extroot"]["default_name"]["v"] == "default_name" assert cfood["extroot"]["default_name"]["v"] == "default_name"
assert cfood["extroot"]["default_name"]["macro_name"] == "default_name" assert cfood["extroot"]["default_name"]["macro_name"] == "default_name"
def test_list_macro_application(register_macros, macro_store_reset):
dat = yaml.load("""
defs:
- !defmacro
name: test
params:
a: 2
definition:
expanded_$a:
param: $a
- !defmacro
name: test2
params:
a: 2
definition:
expanded_${a}_test2:
param: $a
testnode:
obl: !macro
test:
- a: 4
- a: 2
test2:
a: 4
""", Loader=yaml.SafeLoader)
assert dat["testnode"]["obl"]["expanded_4"]["param"] == "4"
assert dat["testnode"]["obl"]["expanded_2"]["param"] == "2"
assert dat["testnode"]["obl"]["expanded_4_test2"]["param"] == "4"
def test_variable_in_macro_definition(register_macros, macro_store_reset):
dat = yaml.load("""
defs:
- !defmacro
name: test
params:
a: 2
b: $a
definition:
expanded_$a:
param: $a
param_b: $b
testnode:
obl: !macro
test:
- a: 4
- a: 2
b: 4
""", Loader=yaml.SafeLoader)
assert dat["testnode"]["obl"]["expanded_4"]["param"] == "4"
assert dat["testnode"]["obl"]["expanded_4"]["param_b"] == "4"
assert dat["testnode"]["obl"]["expanded_2"]["param"] == "2"
assert dat["testnode"]["obl"]["expanded_2"]["param_b"] == "4"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment