コード例 #1
0
    public static void TestTreeModelCanAddElements()
    {
        var root = new TreeElement {
            name = "Root", depth = -1
        };
        var listOfElements = new List <TreeElement>();

        listOfElements.Add(root);

        var model = new TreeModel <TreeElement>(listOfElements);

        model.AddElement(new TreeElement {
            name = "Element"
        }, root, 0);
        model.AddElement(new TreeElement {
            name = "Element " + root.children.Count
        }, root, 0);
        model.AddElement(new TreeElement {
            name = "Element " + root.children.Count
        }, root, 0);
        model.AddElement(new TreeElement {
            name = "Sub Element"
        }, root.children[1], 0);

        // Assert order is correct
        string[] namesInCorrectOrder = { "Root", "Element 2", "Element 1", "Sub Element", "Element" };
        Assert.AreEqual(namesInCorrectOrder.Length, listOfElements.Count, "Result count does not match");
        for (int i = 0; i < namesInCorrectOrder.Length; ++i)
        {
            Assert.AreEqual(namesInCorrectOrder[i], listOfElements[i].name);
        }

        // Assert depths are valid
        TreeElementUtility.ValidateDepthValues(listOfElements);
    }
コード例 #2
0
    public static void TestTreeToListWorks()
    {
        // Arrange
        TestElement root = new TestElement("root", -1);

        root.children = new List <TreeElement>();
        root.children.Add(new TestElement("A", 0));
        root.children.Add(new TestElement("B", 0));
        root.children.Add(new TestElement("C", 0));

        root.children[1].children = new List <TreeElement>();
        root.children[1].children.Add(new TestElement("Bchild", 1));

        root.children[1].children[0].children = new List <TreeElement>();
        root.children[1].children[0].children.Add(new TestElement("Bchildchild", 2));

        // Test
        List <TestElement> result = new List <TestElement>();

        TreeElementUtility.TreeToList(root, result);

        // Assert
        string[] namesInCorrectOrder = { "root", "A", "B", "Bchild", "Bchildchild", "C" };
        Assert.AreEqual(namesInCorrectOrder.Length, result.Count, "Result count is not match");
        for (int i = 0; i < namesInCorrectOrder.Length; ++i)
        {
            Assert.AreEqual(namesInCorrectOrder[i], result[i].name);
        }
        TreeElementUtility.ValidateDepthValues(result);
    }