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 TestDiagnostic() { MockMessageProvider provider = new MockMessageProvider(); SyntaxTree syntaxTree = new MockSyntaxTree(); CultureInfo englishCulture = CultureHelpers.EnglishCulture; DiagnosticInfo di1 = new DiagnosticInfo(provider, 1); Assert.Equal(1, di1.Code); Assert.Equal(DiagnosticSeverity.Error, di1.Severity); Assert.Equal("MOCK0001", di1.MessageIdentifier); Assert.Equal("The first error", di1.GetMessage(englishCulture)); DiagnosticInfo di2 = new DiagnosticInfo(provider, 1002, "Elvis", "Mort"); Assert.Equal(1002, di2.Code); Assert.Equal(DiagnosticSeverity.Warning, di2.Severity); Assert.Equal("MOCK1002", di2.MessageIdentifier); Assert.Equal("The second warning about Elvis and Mort", di2.GetMessage(englishCulture)); Location l1 = new SourceLocation(syntaxTree, new TextSpan(5, 8)); var d1 = new CSDiagnostic(di2, l1); Assert.Equal(l1, d1.Location); Assert.Same(syntaxTree, d1.Location.SourceTree); Assert.Equal(new TextSpan(5, 8), d1.Location.SourceSpan); Assert.Equal(0, d1.AdditionalLocations.Count()); Assert.Same(di2, d1.Info); }
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 TestCustomErrorInfo() { MockMessageProvider provider = new MockMessageProvider(); SyntaxTree syntaxTree = new MockSyntaxTree(); DiagnosticInfo di3 = new CustomErrorInfo(provider, "OtherSymbol", new SourceLocation(syntaxTree, new TextSpan(14, 8))); var d3 = new CSDiagnostic(di3, new SourceLocation(syntaxTree, new TextSpan(1, 1))); Assert.Same(syntaxTree, d3.Location.SourceTree); Assert.Equal(new TextSpan(1, 1), d3.Location.SourceSpan); Assert.Equal(1, d3.AdditionalLocations.Count()); Assert.Equal(new TextSpan(14, 8), d3.AdditionalLocations.First().SourceSpan); Assert.Equal("OtherSymbol", (d3.Info as CustomErrorInfo).OtherSymbol); }
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_InternalNodeCanBeSharedBetweenDifferentTrees() { NameNode firstName = new NameNode("Kallyn"); MockSyntaxTree tree = new MockSyntaxTree( new FullNameNode( firstName, new NameNode("Gowdy") ) ); Assert.Equal("Kallyn", tree.FullName.FirstName.ToString()); MockSyntaxTree newTree = tree.SetRoot( new FullNameNode( firstName, new NameNode("G") ) ) as MockSyntaxTree; Assert.NotNull(newTree); Assert.NotSame(tree, newTree); Assert.NotSame(tree.Root, newTree.Root); Assert.NotSame(tree.Root.FirstName, firstName); Assert.NotSame(newTree.Root.FirstName, firstName); Assert.NotSame(tree.Root.FirstName, newTree.Root.FirstName); Assert.NotEqual(tree, newTree); Assert.NotEqual(tree.Root, newTree.Root); Assert.Equal(tree.Root.FirstName, firstName); Assert.Equal(newTree.Root.FirstName, firstName); Assert.Equal(tree.Root.FirstName, newTree.Root.FirstName); Assert.Same(tree.Root.FirstName.InternalNode, firstName.InternalNode); }
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); }