From af6dd03dc5c360f98192d8d81af67928db59d6b2 Mon Sep 17 00:00:00 2001 From: IanCa Date: Wed, 6 Sep 2023 19:17:59 -0500 Subject: [PATCH] Always copy definition contents in fully. Add tests for description gathering --- hed/models/definition_entry.py | 3 +- tests/models/test_string_util.py | 57 +++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/hed/models/definition_entry.py b/hed/models/definition_entry.py index 4f3aa8d74..1406f41d2 100644 --- a/hed/models/definition_entry.py +++ b/hed/models/definition_entry.py @@ -48,9 +48,8 @@ def get_definition(self, replace_tag, placeholder_value=None, return_copy_of_tag replace_tag = replace_tag.copy() output_contents = [replace_tag] if self.contents: - output_group = self.contents + output_group = copy.deepcopy(self.contents) if placeholder_value: - output_group = copy.deepcopy(self.contents) placeholder_tag = output_group.find_placeholder_tag() if not placeholder_tag: raise ValueError("Internal error related to placeholders in definition mapping") diff --git a/tests/models/test_string_util.py b/tests/models/test_string_util.py index f6d091e00..27cb13879 100644 --- a/tests/models/test_string_util.py +++ b/tests/models/test_string_util.py @@ -1,6 +1,6 @@ import unittest from hed import HedString, load_schema_version -from hed.models.string_util import split_base_tags, split_def_tags +from hed.models.string_util import split_base_tags, split_def_tags, gather_descriptions import copy @@ -131,5 +131,60 @@ def test_case_5(self): self.check_split_def_tags(hed_string, def_names, expected_string, expected_string2) +class TestGatherDescriptions(unittest.TestCase): + def setUp(self): + self.schema = load_schema_version() + + def test_gather_single_description(self): + input_str = "Sensory-event, Description/This is a test." + hed_string = HedString(input_str, hed_schema=self.schema) + + result = gather_descriptions(hed_string) + expected_result = "This is a test." + + self.assertEqual(result, expected_result) + self.assertNotIn("Description", str(result)) + + def test_gather_multiple_descriptions(self): + input_str = "Sensory-event, Description/First description, Second-tag, Description/Second description." + hed_string = HedString(input_str, hed_schema=self.schema) + + result = gather_descriptions(hed_string) + expected_result = "First description. Second description." + + self.assertEqual(result, expected_result) + self.assertNotIn("Description", str(result)) + + def test_gather_no_descriptions(self): + input_str = "Sensory-event, No-description-here, Another-tag" + hed_string = HedString(input_str, hed_schema=self.schema) + + result = gather_descriptions(hed_string) + expected_result = "" + + self.assertEqual(result, expected_result) + self.assertNotIn("Description", str(result)) + + def test_gather_descriptions_mixed_order(self): + input_str = "Sensory-event, Description/First., Another-tag, Description/Second, Third-tag, Description/Third." + hed_string = HedString(input_str, hed_schema=self.schema) + + result = gather_descriptions(hed_string) + expected_result = "First. Second. Third." + + self.assertEqual(result, expected_result) + self.assertNotIn("Description", str(result)) + + def test_gather_descriptions_missing_period(self): + input_str = "Sensory-event, Description/First, Description/Second" + hed_string = HedString(input_str, hed_schema=self.schema) + + result = gather_descriptions(hed_string) + expected_result = "First. Second." + + self.assertEqual(result, expected_result) + self.assertNotIn("Description", str(result)) + + if __name__ == '__main__': unittest.main() \ No newline at end of file