TddExample

April 23, 2009
[Need to build up a good example, small to demonstrate, but showing a class evolving multiple responsibilities and failing tests that first point that out]


This example isn't great - too dependent on .NET framework. I had to expand the FinishCurrentElement method to work both with EndElements and an element without an EndElement but inside the element reading attributes.


public void FinishCurrentElement()
{
XmlNodeType startNodeType = _reader.NodeType;
while (!_reader.EOF)
{
if (_reader.NodeType == XmlNodeType.EndElement) { break; }
if (
(startNodeType != XmlNodeType.Element)
&&
(_reader.NodeType == XmlNodeType.Element)
)
{
break;
}
_reader.Read();
}
if (_reader.NodeType == XmlNodeType.EndElement) { _reader.ReadEndElement(); }
_reader.MoveToContent();
}

[TestMethod]
public void TestFinishCurrentElementWithNoSeparateClosingTag()
{
string xml = "<Data><Foo attr='a' /><Bar /></Data>";
XmlReader reader = GetReader(xml);
reader.FastForwardToElement("Foo");
Assert.AreEqual("a", reader.ReadAttribute("attr"));
reader.FinishCurrentElement();
Assert.AreEqual("Bar", reader.CurrentNodeName);
}

[TestMethod]
public void TestFinishCurrentElementWithSeparateClosingTag()
{
string xml = "<Data><Foo><Bar /></Foo></Data>";
XmlReader reader = GetReader(xml);
reader.FastForwardToElement("Foo");
reader.MovePastElement("Foo");
Assert.AreEqual("Bar", reader.CurrentNodeName);
reader.FinishCurrentElement();
Assert.AreEqual("Data", reader.CurrentNodeName);
Assert.AreEqual(XmlNodeType.EndElement, reader.CurrentNodeType);
}


tags: ComputersAndTechnology
comments powered by Disqus