From 985d64cf5b142afd486d257c85506f0385e0717d Mon Sep 17 00:00:00 2001
From: Alexander Schlemmer <alexander@mail-schlemmer.de>
Date: Mon, 23 May 2022 14:41:20 +0200
Subject: [PATCH] TST: more tests for macros and support for variable
 substitutions in macro definitions

---
 src/caoscrawler/macros/macro_yaml_object.py |  2 +
 unittests/test_macros.py                    | 58 ++++++++++++++++++++-
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/src/caoscrawler/macros/macro_yaml_object.py b/src/caoscrawler/macros/macro_yaml_object.py
index 2849986e..c6b5de27 100644
--- a/src/caoscrawler/macros/macro_yaml_object.py
+++ b/src/caoscrawler/macros/macro_yaml_object.py
@@ -135,6 +135,7 @@ def macro_constructor(loader, node):
                             raise RuntimeError("params type not supported")
                     else:
                         raise RuntimeError("params type must not be None")
+                    params = substitute_dict(params, params)
                     definition = substitute_dict(macro.definition, params)
                     res.update(definition)
             else:
@@ -146,6 +147,7 @@ def macro_constructor(loader, node):
                         params.update(params_setter)
                     else:
                         raise RuntimeError("params type not supported")
+                params = substitute_dict(params, params)
                 definition = substitute_dict(macro.definition, params)
                 res.update(definition)
         else:
diff --git a/unittests/test_macros.py b/unittests/test_macros.py
index 4e27e42f..33252d38 100644
--- a/unittests/test_macros.py
+++ b/unittests/test_macros.py
@@ -250,7 +250,6 @@ testnode:
     assert dat["testnode"]["obl"]["replaced1"]["d"][0] == "a"
     assert dat["testnode"]["obl"]["replaced1"]["d"][1] == "25"
 
-
 def test_circular_macro_definition(register_macros, macro_store_reset):
     """Test the (ab-)use of macros to create an infinite loop."""
     cfood = _temp_file_load("""
@@ -456,3 +455,60 @@ extroot: !macro
     assert cfood["extroot"]["default_name"]["a"] == "default_name"
     assert cfood["extroot"]["default_name"]["v"] == "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"
+
-- 
GitLab