提交 3262c280 编写于 作者: L Lukáš Doktor

tree: Add support "fingerprint" method

The TreeNode.fingerprint() generates string, which represents the value
of the node. The idea is to represent the content of the node and it's
environment and such for the purpose of evaluating, whether other node
is alike.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 9865f1d7
......@@ -269,6 +269,9 @@ class MuxTreeNode(tree.TreeNode):
def __repr__(self):
return '%s(name=%r)' % (self.__class__.__name__, self.name)
def fingerprint(self):
return "%s%s" % (super(MuxTreeNode, self).fingerprint(), self.ctrl)
def merge(self, other):
"""
Merges `other` node into this one without checking the name of the
......
......@@ -133,6 +133,12 @@ class TreeNode(object):
""" Inverted eq """
return not self == other
def fingerprint(self):
"""
Reports string which represents the value of this node.
"""
return "%s%s" % (self.path, self.environment)
def add_child(self, node):
"""
Append node as child. Nodes with the same name gets merged into the
......
......@@ -493,5 +493,45 @@ class TestPathParent(unittest.TestCase):
self.assertNotEqual(mux.path_parent('/os/linux'), '/')
class TestFingerprint(unittest.TestCase):
def test_fingerprint(self):
"""
Verifies the fingerprint is correctly evaluated
"""
node1 = tree.TreeNode("node1", {"foo": "bar"})
node1_fingerprint = node1.fingerprint()
node1duplicate = tree.TreeNode("node1", {"foo": "bar"})
self.assertEqual(node1_fingerprint, node1duplicate.fingerprint())
node1b_value = tree.TreeNode("node1", {"foo": "baz"})
self.assertNotEqual(node1_fingerprint, node1b_value.fingerprint())
node1b_name = tree.TreeNode("node2", {"foo": "bar"})
self.assertNotEqual(node1_fingerprint, node1b_name)
node1b_path = tree.TreeNode("node1", {"foo": "bar"})
tree.TreeNode("root", children=(node1b_path,))
self.assertNotEqual(node1_fingerprint, node1b_path.fingerprint())
node1b_env_orig = tree.TreeNode("node1", {"foo": "bar"})
tree.TreeNode("root", {"bar": "baz"}, children=(node1b_env_orig))
node1b_env_origb = tree.TreeNode("node1",
{"foo": "bar", "bar": "baz"})
tree.TreeNode("root", children=(node1b_env_origb,))
self.assertNotEqual(node1b_env_orig.fingerprint(),
node1b_env_origb.fingerprint())
def test_tree_mux_node(self):
"""
Check the extension of fingerprint in MuxTreeNode
"""
node1 = tree.TreeNode("node1", {"foo": "bar"})
node1m = mux.MuxTreeNode("node1", {"foo": "bar"})
node1m_fingerprint = node1m.fingerprint()
self.assertNotEqual(node1.fingerprint(), node1m_fingerprint)
node1mduplicate = mux.MuxTreeNode("node1", {"foo": "bar"})
self.assertEqual(node1m_fingerprint, node1mduplicate.fingerprint())
node1mb_ctrl = mux.MuxTreeNode("node1", {"foo": "bar"})
node1mb_ctrl.ctrl = [mux.Control(0, 0)]
self.assertNotEqual(node1m_fingerprint, node1mb_ctrl.fingerprint())
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册