Repository: byuccl/spydrnet

Issue:
Add .name to elements as a pointer to ['NAME']

Discussion:

Patch:
diff --git a/spydrnet/ir/element.py b/spydrnet/ir/element.py
index ae406c5..24d33ef 100644
--- a/spydrnet/ir/element.py
+++ b/spydrnet/ir/element.py
@@ -36,6 +36,22 @@ class Element(object):
     @property
     def data(self):
         return DictView(self._data)
+        
+    @property
+    def name(self):
+        return self._data.get(".NAME", None)
+        
+    @name.setter
+    def name(self, value):
+        if value is None and ".NAME" in self:
+            del self[".NAME"]
+        else:
+            self[".NAME"] = value
+
+    @name.deleter
+    def name(self):
+        if ".NAME" in self:
+            del self[".NAME"]
 
     def __setitem__(self, key, value):
         """


Test patch:
diff --git a/spydrnet/ir/tests/test_element.py b/spydrnet/ir/tests/test_element.py
index c93d096..a9d7c20 100644
--- a/spydrnet/ir/tests/test_element.py
+++ b/spydrnet/ir/tests/test_element.py
@@ -4,6 +4,9 @@ from spydrnet.ir.element import Element
 
 
 class TestElement(unittest.TestCase):
+    def setUp(self) -> None:
+        self.element = Element()
+
     def test_constructor(self):
         element1 = Element()
         self.assertTrue(element1, "Constructor return None type or empty collection")
@@ -11,19 +14,32 @@ class TestElement(unittest.TestCase):
         self.assertNotEqual(element1, element2, "Unique objects are considered equal.")
 
     def test_dictionary(self):
-        element = Element()
-        self.assertFalse('NAME' in element)
-        element['NAME'] = "TestName"
-        self.assertTrue('NAME' in element)
-        for key in element:
-            self.assertEqual(element[key], "TestName")
-        del element['NAME']
-        self.assertFalse('NAME' in element)
-        element['NAME'] = "DifferentName"
-        name = element.pop('NAME')
+        self.assertFalse('NAME' in self.element)
+        self.element['NAME'] = "TestName"
+        self.assertTrue('NAME' in self.element)
+        for key in self.element:
+            self.assertEqual(self.element[key], "TestName")
+        del self.element['NAME']
+        self.assertFalse('NAME' in self.element)
+        self.element['NAME'] = "DifferentName"
+        name = self.element.pop('NAME')
         self.assertEqual(name, "DifferentName")
 
+    def test_name(self):
+        self.element.name = "TestName"
+        self.assertTrue(".NAME" in self.element)
+        self.assertEqual(self.element.name, "TestName")
+
+    def test_del_name(self):
+        self.assertIsNone(self.element.name)
+        self.element.name = None
+        self.element.name = "TestName"
+        self.element.name = None
+        self.assertFalse(".NAME" in self.element)
+        self.element.name = "TestName"
+        del self.element.name
+        self.assertFalse(".NAME" in self.element)
+
     def test_data_view(self):
-        element = Element()
-        element['NAME'] = "TestName"
-        self.assertEqual(element.data, {'NAME': 'TestName'})
+        self.element['NAME'] = "TestName"
+        self.assertEqual(self.element.data, {'NAME': 'TestName'})