public void IHierarchy_adds_child_sibling_value_twice_throws_ArgumentException(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test  = "test";
            string test1 = "test1";
            string test2 = "test2";
            string test3 = "test3";

            hierarchy.Add(HierarchyPath.Create <string>(), test);
            hierarchy.Add(HierarchyPath.Create("a"), test1);
            hierarchy.Add(HierarchyPath.Create("b"), test2);

            // ACT & ASSERT

            var result = Assert.Throws <ArgumentException>(() => hierarchy.Add(HierarchyPath.Create("b"), test3));

            // ASSERT

            Assert.True(result.Message.Contains("already has a value"));
            Assert.True(result.Message.Contains("'b'"));
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("b"), out var value));
            Assert.Equal(test2, value);
            Assert.Equal("path", result.ParamName);
        }
Exemple #2
0
        public void IHierarchy_remove_value_from_child_returns_true(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test  = "test";
            string test1 = "test1";
            string test2 = "test2";

            hierarchy.Add(HierarchyPath.Create <string>(), test);
            hierarchy.Add(HierarchyPath.Create("a"), test1);
            hierarchy.Add(HierarchyPath.Create("a", "b"), test2);

            // ACT

            var result = hierarchy.Remove(HierarchyPath.Create("a"));

            // ASSERT

            Assert.True(result);

            string value;

            // new node has no value
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create <string>(), out value));
            Assert.Equal(test, value);
            Assert.False(hierarchy.TryGetValue(HierarchyPath.Create("a"), out value));
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("a", "b"), out value));
            Assert.Equal(test2, value);
        }
        public void IHierarchy_RemoveNode_non_recursive_fails_if_childnode_is_present(string nodePath, string subNodePath, IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test  = "test";
            string test1 = "test1";

            hierarchy.Add(HierarchyPath.Parse(nodePath, "/"), test);
            hierarchy.Add(HierarchyPath.Parse(subNodePath, "/"), test1);

            // ACT

            var result = hierarchy.RemoveNode(HierarchyPath.Parse(nodePath, "/"), recurse: false);

            // ASSERT

            Assert.False(result);

            string value;

            // node has no value
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Parse(nodePath, "/"), out value));
            Assert.Equal(test, value);
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Parse(subNodePath, "/"), out value));
            Assert.Equal(test1, value);
        }
        public void IHierarchy_adds_grandchild_under_existing_nodes_returns_hierachy_with_same_values(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test  = "test";
            string test1 = "test1";
            string test2 = "test2";
            string test3 = "test3";

            hierarchy.Add(HierarchyPath.Create <string>(), test);
            hierarchy.Add(HierarchyPath.Create("a"), test1);
            hierarchy.Add(HierarchyPath.Create("b"), test2);

            // ACT

            hierarchy.Add(HierarchyPath.Create("a", "c"), test3);

            // ASSERT
            // new hierarchy contains the root date and the new node.
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create <string>(), out var value1));
            Assert.Same(test, value1);
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("a"), out var value2));
            Assert.Equal(test1, value2);
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("b"), out var value3));
            Assert.Equal(test2, value3);
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("a", "c"), out var value4));
            Assert.Equal(test3, value4);
        }
Exemple #5
0
        public void IHierarchy_get_children_of_root_node_on_traversal(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create("a"), "v1");
            hierarchy.Add(HierarchyPath.Create("b"), "v2");

            // ACT

            var result = hierarchy.Traverse(HierarchyPath.Create <string>()).ChildNodes.ToArray();

            // ASSERT

            Assert.Equal(2, result.Length);
        }
Exemple #6
0
        public void IHierarchy_remove_value_twice_from_root_returns_false(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE
            string test  = "test";
            string test1 = "test1";

            hierarchy.Add(HierarchyPath.Create <string>(), test);
            hierarchy.Add(HierarchyPath.Create("a"), test1);
            hierarchy.Remove(HierarchyPath.Create <string>());

            // ACT

            var result = hierarchy.Remove(HierarchyPath.Create <string>());

            // ASSERT

            Assert.False(result);
        }
        public void IHierarchy_adds_child_sets_value_at_child_node(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test  = "test";
            string test1 = "test1";

            hierarchy.Add(HierarchyPath.Create <string>(), test);

            // ACT

            hierarchy.Add(HierarchyPath.Create("a"), test1);

            // ASSERT
            // new hierarchy contains the root date and the new node.
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create <string>(), out var value1));
            Assert.Equal(test, value1);
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("a"), out var value2));
            Assert.Equal(test1, value2);
        }
Exemple #8
0
        public void IHierarchy_child_node_knows_its_path_on_traversal(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create("a"), "v1");

            // ACT

            var result = hierarchy.Traverse(HierarchyPath.Create <string>()).ChildNodes.Single().Path;

            // ASSERT

            Assert.Equal(HierarchyPath.Create("a"), result);
        }
Exemple #9
0
        public void IHierarchy_removes_value_from_root(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE
            string test  = "test";
            string test1 = "test1";

            hierarchy.Add(HierarchyPath.Create <string>(), test);
            hierarchy.Add(HierarchyPath.Create("a"), test1);

            // ACT

            var result = hierarchy.Remove(HierarchyPath.Create <string>());

            // ASSERT

            Assert.True(result);

            string value;

            Assert.False(hierarchy.TryGetValue(HierarchyPath.Create <string>(), out value));
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("a"), out value));
            Assert.Equal(test1, value);
        }
Exemple #10
0
        public void IHierarchy_get_root_nodes_value_on_traversal(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create <string>(), "v1");

            // ACT

            var result = hierarchy.Traverse(HierarchyPath.Create <string>()).TryGetValue(out var result_value);

            // ASSERT

            Assert.Equal("v1", result_value);
        }
Exemple #11
0
        public void IHierarchy_start_at_inner_node_stil_allows_to_ascend(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create("a"), "v1");
            hierarchy.Add(HierarchyPath.Create("a", "b", "c"), "v2");

            // ACT

            var result = hierarchy.Traverse(HierarchyPath.Create("a", "b", "c"));

            // ASSERT

            Assert.NotNull(result);
            Assert.Equal(new[]
            {
                HierarchyPath.Create("a", "b", "c"),
                HierarchyPath.Create("a", "b"),
                HierarchyPath.Create("a"),
                HierarchyPath.Create <string>(),
            },
                         result.AncestorsAndSelf().Select(n => n.Path).ToArray());
        }
        public void IHierarchy_RemoveNode_removes_inner_node_from_hierarchy_and_descendants(string nodeToDelete, IHierarchy <string, string> hierarchy)
        {
            // ARRANGE
            // node
            //    \
            //     subNode

            var nodePath = HierarchyPath.Parse(nodeToDelete, "/");

            hierarchy.Add(nodePath, nodePath.ToString());

            // add subnode with value
            var subNode = nodePath.Join("subNode");

            hierarchy.Add(subNode, subNode.ToString());

            // ACT
            // remove node and subNode

            var result = hierarchy.RemoveNode(nodePath, recurse: true);

            // ASSERT
            // node could be removed

            Assert.True(result);

            // values of node and subNode can't be read anymore
            Assert.False(hierarchy.TryGetValue(nodePath, out var valueNode));
            Assert.False(hierarchy.TryGetValue(subNode, out var valueSubNode));

            // nodes are no longer present
            if (!nodePath.IsRoot)
            {
                Assert.Throws <KeyNotFoundException>(() => hierarchy.Traverse(nodePath));
            }
            Assert.Throws <KeyNotFoundException>(() => hierarchy.Traverse(subNode));
        }
Exemple #13
0
        public void IHierarchy_throw_if_start_path_doesnt_exist(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create("a"), "v1");

            var node_a = hierarchy.Traverse(HierarchyPath.Create <string>()).Children().First();

            // ACT

            var result = Assert.Throws <KeyNotFoundException>(() => hierarchy.Traverse(HierarchyPath.Create("b")));

            // ASSERT

            Assert.True(result.Message.Contains("'b'"));
        }
Exemple #14
0
        public void IHierarchy_child_of_root_has_root_as_parent_on_traversal(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create("a"), "v1");

            var root = hierarchy.Traverse(HierarchyPath.Create <string>());

            // ACT

            var result = hierarchy.Traverse(HierarchyPath.Create <string>()).Children().Single().Parent();

            // ASSERT

            Assert.Equal(root, result);
        }
        public void IHierarchy_adds_value_to_root_node(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test = "test";

            // ACT

            hierarchy.Add(HierarchyPath.Create <string>(), test);

            // ASSERT
            // new hierarchy contains all values

            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create <string>(), out var value));
            Assert.Same(test, value);
        }
Exemple #16
0
        public void IHierarchy_start_at_child_of_root_on_traversal(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create("a"), "v1");

            var node_a = hierarchy.Traverse(HierarchyPath.Create <string>()).Children().Single();

            // ACT

            var result = hierarchy.Traverse(HierarchyPath.Create("a"));

            // ASSERT

            Assert.NotNull(result);
            Assert.Equal(HierarchyPath.Create("a"), result.Path);
        }
        public void IHierarchy_removes_root_by_removing_its_value(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            hierarchy.Add(HierarchyPath.Create <string>(), "test");

            // ACT

            var result = hierarchy.RemoveNode(HierarchyPath.Create <string>(), recurse: false);

            // ASSERT

            Assert.True(result);

            // value is removed
            Assert.False(hierarchy.TryGetValue(HierarchyPath.Create <string>(), out var value));
        }
        public void IHierarchy_RemoveNode_twice_returns_true_for_root_node(string path, bool recurse, IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test = "test";

            hierarchy.Add(HierarchyPath.Parse(path, "/"), test);
            hierarchy.RemoveNode(HierarchyPath.Parse(path, "/"), recurse: recurse);

            // ACT

            var result = hierarchy.RemoveNode(HierarchyPath.Parse(path, "/"), recurse: recurse);

            // ASSERT

            Assert.True(result);
        }
Exemple #19
0
        public void IHierarchy_removes_false_if_no_value_was_removed(IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            string test2 = "test2";

            hierarchy.Add(HierarchyPath.Create("a", "b"), test2);

            // ACT

            var result = hierarchy.Remove(HierarchyPath.Create <string>());

            // ASSERT

            Assert.False(result);

            string value;

            // new node has no value
            Assert.True(hierarchy.TryGetValue(HierarchyPath.Create("a", "b"), out value));
            Assert.Equal(test2, value);
        }
        public void IHierarchy_RemoveNode_removes_leaf_from_hierarchy(string pathToDelete, bool recurse, IHierarchy <string, string> hierarchy)
        {
            // ARRANGE

            var path = HierarchyPath.Parse(pathToDelete, "/");

            hierarchy.Add(path, pathToDelete);

            // ACT
            // the value of recurse doen't make difference

            var result = hierarchy.RemoveNode(path, recurse: recurse);

            // ASSERT
            // result must be true always

            Assert.True(result);

            // node has no value
            Assert.False(hierarchy.TryGetValue(path, out var _));

            // nodes are no longer present
            Assert.Throws <KeyNotFoundException>(() => hierarchy.Traverse(path));
        }