diff --git a/unittests/test_macros.py b/unittests/test_macros.py index 5244307db8e694ffb4864380d33936ebb76ae715..53837e920e93f2cc318d62549145a0e8ac757372 100644 --- a/unittests/test_macros.py +++ b/unittests/test_macros.py @@ -142,7 +142,7 @@ def test_multi_macros_toplevel(register_macros, macro_store_reset): dat_loader = list(yaml.safe_load_all(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: test_one @@ -171,7 +171,7 @@ def test_load_definition(register_macros, macro_store_reset): txt = """ --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 --- extroot: type: Directory @@ -188,7 +188,7 @@ extroot: cfood = _temp_file_load(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: test_one @@ -257,12 +257,90 @@ testnode: assert dat["testnode"]["obl"]["replaced1"]["d"][1] == "25" +def test_macros_in_macros(register_macros, macro_store_reset): + """ + Test that macros can be used in macro definitions. + """ + cfood = _temp_file_load(""" +--- +metadata: + crawler-version: 0.5.1 + macros: + - !defmacro + name: one_macro + params: + a: 25 + definition: + macro_sub_$a: + b: $a + another_param: 3 + - !defmacro + name: test_macrodef + params: {} + definition: + macro_top: !macro + one_macro: + - a: 17 + - {} + - a: 98 + not_macro: + a: 26 +--- +extroot: !macro + test_macrodef: + """) + + assert "test_macro" not in cfood["extroot"] + assert cfood["extroot"]["macro_top"]["not_macro"]["a"] == 26 + d = cfood["extroot"]["macro_top"] + assert d["macro_sub_17"]["b"] == "17" + assert d["macro_sub_17"]["another_param"] == 3 + assert d["macro_sub_25"]["b"] == "25" + assert d["macro_sub_25"]["another_param"] == 3 + assert d["macro_sub_98"]["b"] == "98" + assert d["macro_sub_98"]["another_param"] == 3 + + +@pytest.mark.xfail( + reason="This is discussed in the following issue" + "https://gitlab.com/caosdb/caosdb-crawler/-/issues/74." +) +def test_silent_overwrite(register_macros, macro_store_reset): + cfood = _temp_file_load(""" +--- +metadata: + crawler-version: 0.5.1 + macros: + - !defmacro + name: one_macro + params: + a: 25 + definition: + macro_sub: + b: $a + another_param: 3 + - !defmacro + name: test_macrodef + params: {} + definition: + macro_top: !macro + one_macro: + - a: 17 + - a: 98 +--- +extroot: !macro + test_macrodef: + """) + + assert len(cfood["extroot"]["macro_top"]) == 2 + + 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(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: test_one @@ -311,7 +389,7 @@ def test_use_macro_twice(): cfood = _temp_file_load(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: test_twice @@ -345,7 +423,7 @@ def test_documentation_example_2(): cfood = _temp_file_load(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: MarkdownFile @@ -383,7 +461,7 @@ def test_documentation_example_1(): cfood = _temp_file_load(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: SimulationDatasetFile @@ -432,7 +510,7 @@ def test_def_replacements(): cfood = _temp_file_load(""" --- metadata: - crawler-version: 0.3.1 + crawler-version: 0.5.1 macros: - !defmacro name: test_def_replacements