public void Test_NodesAreImmutable() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ); NameNode newName = tree.Root.FirstName.SetFirstName("Kal"); Assert.Null(newName.Parent); Assert.Null(newName.Parent); Assert.NotNull(newName); Assert.NotSame(tree.Root.FirstName, newName); Assert.NotSame(tree.Root, newName.Parent); Assert.Equal("Kal", newName.Name); FullNameNode newRoot = tree.Root.ReplaceNode(tree.Root.FirstName, newName); Assert.Null(newRoot.Parent); Assert.NotNull(newRoot.Tree); Assert.Same(newRoot.InternalNode.FirstName, newName.InternalNode); Assert.NotSame(tree.Root, newRoot); Assert.NotSame(tree, newRoot.Tree); Assert.NotSame(newName, newRoot.FirstName); }
public void Test_NewNodesAreGeneratedAsTreeIsTraversed() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ); Assert.NotNull(tree); Assert.NotNull(tree.Root); FullNameNode root = tree.Root as FullNameNode; Assert.Null(root.Parent); Assert.NotNull(root.Tree); Assert.Same(tree, root.Tree); Assert.Equal(0, root.Position); Assert.Equal(11, root.Width); Assert.Equal("{Kallyn Gowdy}", root.ToString()); NameNode firstName = root.FirstName; Assert.NotNull(firstName.Parent); Assert.Same(root, firstName.Parent); Assert.Same(tree, firstName.Tree); Assert.Equal(0, firstName.Position); NameNode lastName = root.LastName; Assert.NotNull(lastName); Assert.Same(root, lastName.Parent); Assert.Same(tree, firstName.Tree); Assert.Equal(6, lastName.Position); }
public void Test_BuildSyntaxTree() { // The input string into the parser // In our case, the parser is a very simple lastname-firstname parser, where the first token in // the input is the first name and the rest are just added on. string[] input = { "First", "Last" }; NameNode[] names = new NameNode[input.Length]; // In our case, the first grammar rule would be to reduce all of the names in the input to a list of NameInternalNode objects // e.g. // FullName -> { Name } // Name -> Word for (int i = 0; i < input.Length; i++) { // The parser identifies a node and creates the internal representation of it string name = input[i]; NameNode nameNode = new NameNode(name); names[i] = nameNode; } // Then the parser checks to see if a reduction to a full name node can be made and reduces if it can FullNameNode fullName = new FullNameNode(names); // Then a full syntax tree is produced and returned MockSyntaxTree tree = new MockSyntaxTree(fullName); Assert.Collection( tree.Root.Children, node => Assert.Equal("First", node.ToString()), node => Assert.Equal("Last", node.ToString()) ); }
public void Test_StrictEqualsCoversChildren() { NameNode firstName = new NameNode("First", new SyntaxTrivia("\n\n "), new SyntaxTrivia("\n")); NameNode lastName = new NameNode("Last", new SyntaxTrivia(" "), new SyntaxTrivia("\t\t")); FullNameNode fullName = new FullNameNode(firstName, lastName); NameNode otherFirstName = new NameNode("First", new SyntaxTrivia("\n "), new SyntaxTrivia("\n")); FullNameNode otherFullName = new FullNameNode(otherFirstName, lastName); Assert.Equal(fullName, otherFullName); Assert.False(fullName.StrictEquals(otherFullName)); }
public void Test_InsertNodeCreatesNewFacadeTree() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ); NameNode middleName = new NameNode("G."); FullNameNode fullName = (FullNameNode)tree.FullName.InsertNode(1, middleName); Assert.Equal(13, fullName.Width); Assert.Equal("{Kallyn G. Gowdy}", fullName.ToString()); Assert.NotSame(tree, fullName.Tree); Assert.Collection( fullName.Children, n => Assert.Equal(new NameNode("Kallyn"), n), n => Assert.Equal(new NameNode("G."), n), n => Assert.Equal(new NameNode("Gowdy"), n) ); Assert.Collection( tree.FullName.Children, n => Assert.Same(n.InternalNode, fullName.FirstName.InternalNode), n => Assert.Same(n.InternalNode, fullName.LastName.InternalNode) ); Assert.Collection( tree.FullName.Children, n => Assert.NotSame(n, fullName.FirstName), n => Assert.NotSame(n, fullName.LastName) ); Assert.Collection( fullName.Children, n => Assert.Same(n.InternalNode, fullName.FirstName.InternalNode), n => Assert.Same(n.InternalNode, middleName.InternalNode), n => Assert.Same(n.InternalNode, fullName.LastName.InternalNode) ); }
public void Test_SetRootCreatesNewImmutableTree() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ); FullNameNode newFullName = new FullNameNode( new NameNode("K"), new NameNode("G") ); SyntaxTree newTree = tree.SetRoot(newFullName); Assert.NotNull(newTree); Assert.IsType <MockSyntaxTree>(newTree); Assert.NotSame(tree, newTree); }
public void Test_ReplaceNodeCreatesNewFacadeTree() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ); NameNode lastName = tree.FullName.LastName; NameNode newLastName = new NameNode("G"); FullNameNode newFullName = tree.FullName.ReplaceNode(lastName, newLastName); Assert.NotSame(tree.FullName, newFullName); Assert.NotEqual(tree.FullName, newFullName); Assert.NotSame(tree, newFullName.Tree); Assert.NotEqual(tree, newFullName.Tree); }
public void Test_RemoveNode() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("G."), new NameNode("Gowdy"), new NameNode("Other") ) ); FullNameNode fullName = (FullNameNode)tree.FullName.RemoveNode(tree.FullName.Children[3]); Assert.Collection( fullName.Children, n => Assert.Equal(new NameNode("Kallyn"), n), n => Assert.Equal(new NameNode("G."), n), n => Assert.Equal(new NameNode("Gowdy"), n) ); Assert.Collection( tree.FullName.Children, n => Assert.Same(n.InternalNode, fullName.FirstName.InternalNode), n => Assert.Same(n.InternalNode, fullName.Children[1].InternalNode), n => Assert.Same(n.InternalNode, fullName.LastName.InternalNode), Assert.NotNull ); Assert.Collection( tree.FullName.Children, n => Assert.NotSame(n, fullName.FirstName), n => Assert.NotSame(n, fullName.Children[1]), n => Assert.NotSame(n, fullName.LastName), Assert.NotNull ); Assert.Equal(13, fullName.Width); Assert.Equal("{Kallyn G. Gowdy}", fullName.ToString()); }
public void Test_AddNodeCreatesNewFacadeTree() { MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ); Assert.Equal(2, tree.FullName.Children.Count); NameNode otherLastName = new NameNode("Other"); FullNameNode fullName = (FullNameNode)tree.FullName.AddNode(otherLastName); Assert.Collection( fullName.Children, n => Assert.Equal(new NameNode("Kallyn"), n), n => Assert.Equal(new NameNode("Gowdy"), n), n => Assert.Equal(new NameNode("Other"), n) ); Assert.Collection( tree.FullName.Children, n => Assert.Same(n.InternalNode, fullName.FirstName.InternalNode), n => Assert.Same(n.InternalNode, fullName.Children[1].InternalNode) // Last name gets changed by AddNode ); Assert.Collection( tree.FullName.Children, n => Assert.NotSame(n, fullName.FirstName), n => Assert.NotSame(n, fullName.Children[1]) ); Assert.Equal(16, fullName.Width); Assert.Equal("{Kallyn Gowdy Other}", fullName.ToString()); }
public void Test_TreeIsProperlyCreatedWithReferences() { NameNode firstName = new NameNode("Kallyn"); Assert.Equal("Kallyn", firstName.Name); Assert.Null(firstName.Parent); Assert.Null(firstName.Tree); Assert.Equal(0, firstName.Position); Assert.NotNull(firstName.Children); Assert.Equal(0, firstName.Children.Count); Assert.Equal(6, firstName.Width); NameNode lastName = new NameNode("Gowdy"); Assert.Equal("Gowdy", lastName.Name); Assert.Null(lastName.Parent); Assert.Null(lastName.Tree); Assert.NotNull(lastName.Children); Assert.Equal(0, lastName.Position); Assert.Equal(0, lastName.Children.Count); Assert.Equal(5, lastName.Width); FullNameNode fullName = new FullNameNode( firstName, lastName ); Assert.Equal(new FullNameNode(new NameNode("Kallyn"), new NameNode("Gowdy")), fullName); Assert.Null(fullName.Parent); Assert.Null(fullName.Tree); Assert.NotNull(fullName.Children); Assert.Equal(0, fullName.Position); Assert.Equal(2, fullName.Children.Count); Assert.Same(fullName.FirstName, fullName.Children[0]); Assert.Same(fullName.LastName, fullName.Children[1]); Assert.Equal(11, fullName.Width); Assert.Equal(firstName, fullName.FirstName); Assert.Equal(lastName, fullName.LastName); Assert.Equal(firstName.InternalNode, fullName.FirstName.InternalNode); Assert.Equal(lastName.InternalNode, fullName.LastName.InternalNode); Assert.Equal(0, fullName.FirstName.Position); Assert.Equal(6, fullName.LastName.Position); MockSyntaxTree tree = new MockSyntaxTree( fullName ); Assert.Equal(new MockSyntaxTree( new FullNameNode( new NameNode("Kallyn"), new NameNode("Gowdy") ) ), tree); Assert.Equal(fullName, tree.Root); Assert.Same(tree.Root, tree.FullName); }