public void DeserializeFromXml_Reader() { using (XmlReader reader = XmlReader.Create(GetGraphFilePath(TestGraphFileName))) { AdjacencyGraph <string, EquatableEdge <string> > adjacencyGraph = reader.DeserializeFromXml( "graph", "node", "edge", "", r => new AdjacencyGraph <string, EquatableEdge <string> >(), r => r.GetAttribute("id"), r => new EquatableEdge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute"))); AssetTestGraphContent( adjacencyGraph, (v1, v2, weight) => new EquatableEdge <string>(v1, v2)); } using (XmlReader reader = XmlReader.Create(GetGraphFilePath(TestGraphFileName))) { BidirectionalGraph <string, EquatableEdge <string> > bidirectionalGraph = reader.DeserializeFromXml( "graph", "node", "edge", "", r => new BidirectionalGraph <string, EquatableEdge <string> >(), r => r.GetAttribute("id"), r => new EquatableEdge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute"))); AssetTestGraphContent( bidirectionalGraph, (v1, v2, weight) => new EquatableEdge <string>(v1, v2)); } using (XmlReader reader = XmlReader.Create(GetGraphFilePath(TestGraphFileName))) { UndirectedGraph <string, EquatableTaggedEdge <string, double> > undirectedGraph = reader.DeserializeFromXml( "graph", "node", "edge", "", r => new UndirectedGraph <string, EquatableTaggedEdge <string, double> >(), r => r.GetAttribute("id"), r => new EquatableTaggedEdge <string, double>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute"), int.Parse( r.GetAttribute("weight") ?? throw new AssertionException("Must have weight attribute")))); AssetTestGraphContent( undirectedGraph, (v1, v2, weight) => new EquatableTaggedEdge <string, double>(v1, v2, weight)); } }
public void DeserializeFromXml_Reader_Throws() { // ReSharper disable ReturnValueOfPureMethodIsNotUsed // ReSharper disable AssignNullToNotNullAttribute Assert.Throws <ArgumentNullException>( () => ((XmlReader)null).DeserializeFromXml( "graph", "node", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); using (XmlReader reader = XmlReader.Create(GetGraphFilePath(TestGraphFileName))) { Assert.Throws <ArgumentNullException>(() => reader.DeserializeFromXml <string, Edge <string>, AdjacencyGraph <string, Edge <string> > >( "graph", "node", "edge", "", null, r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentNullException>(() => reader.DeserializeFromXml <string, Edge <string>, AdjacencyGraph <string, Edge <string> > >( "graph", "node", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), null, r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentNullException>(() => reader.DeserializeFromXml <string, Edge <string>, AdjacencyGraph <string, Edge <string> > >( "graph", "node", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), null)); Assert.Throws <ArgumentNullException>( () => reader.DeserializeFromXml( null, r => r.Name == "vertex", r => r.Name == "edge", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentNullException>( () => reader.DeserializeFromXml( r => r.Name == "graph", null, r => r.Name == "edge", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentNullException>( () => reader.DeserializeFromXml( r => r.Name == "graph", r => r.Name == "vertex", null, r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentException>(() => reader.DeserializeFromXml( null, "node", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentException>(() => reader.DeserializeFromXml( "", "node", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentException>(() => reader.DeserializeFromXml( "graph", null, "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentException>(() => reader.DeserializeFromXml( "graph", "", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentException>(() => reader.DeserializeFromXml( "graph", "node", null, "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentException>(() => reader.DeserializeFromXml( "graph", "node", "", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); Assert.Throws <ArgumentNullException>(() => reader.DeserializeFromXml( "graph", "node", "edge", null, r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); // No graph node found Assert.Throws <InvalidOperationException>(() => reader.DeserializeFromXml( "g", // No node named "g" for the graph "node", "edge", "", r => new AdjacencyGraph <string, Edge <string> >(), r => r.GetAttribute("id"), r => new Edge <string>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute")))); } // ReSharper restore AssignNullToNotNullAttribute // ReSharper restore ReturnValueOfPureMethodIsNotUsed }
public void DeserializeFromXml() { XPathDocument document = new XPathDocument(GetGraphFilePath("repro12273.xml")); UndirectedGraph <string, TaggedEdge <string, double> > undirectedGraph = document.DeserializeFromXml( "graph", "node", "edge", nav => new UndirectedGraph <string, TaggedEdge <string, double> >(), nav => nav.GetAttribute("id", ""), nav => new TaggedEdge <string, double>( nav.GetAttribute("source", ""), nav.GetAttribute("target", ""), int.Parse(nav.GetAttribute("weight", "")))); using (XmlReader reader = XmlReader.Create(GetGraphFilePath("repro12273.xml"))) { UndirectedGraph <string, TaggedEdge <string, double> > undirectedGraph2 = reader.DeserializeFromXml( "graph", "node", "edge", "", r => new UndirectedGraph <string, TaggedEdge <string, double> >(), r => r.GetAttribute("id"), r => new TaggedEdge <string, double>( r.GetAttribute("source") ?? throw new AssertionException("Must have source attribute"), r.GetAttribute("target") ?? throw new AssertionException("Must have target attribute"), int.Parse(r.GetAttribute("weight") ?? throw new AssertionException("Must have weight attribute")))); Assert.AreEqual(undirectedGraph.VertexCount, undirectedGraph2.VertexCount); Assert.AreEqual(undirectedGraph.EdgeCount, undirectedGraph2.EdgeCount); } }