Skip to content

Instantly share code, notes, and snippets.

@vkhorikov
Created July 31, 2015 14:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkhorikov/1e9e2e0db8986526d3bc to your computer and use it in GitHub Desktop.
Save vkhorikov/1e9e2e0db8986526d3bc to your computer and use it in GitHub Desktop.
[Fact]
public void Parser_parses_xml_in_correct_order()
{
// Arrange : input values
string xml = "<outer><inner /></outer>";
var parser = new Parser();
// Arrange : record expectations
var mocks = new MockRepository();
IHandler handler = mocks.CreateMock<IHandler>();
handler.StartDocument();
handler.StartElement("outer");
handler.StartElement("inner");
handler.EndElement("inner");
handler.EndElement("outer");
handler.EndDocument();
mocks.ReplayAll();
// Act
parser.ParseXml(xml, handler);
// Assert
mocks.VerifyAll();
}
[Fact]
public void Parser_parses_xml_in_correct_order2()
{
// Arrange
string xml = "<outer><inner /></outer>";
var parser = new Parser();
var handlerStub = new HandlerStub();
// Act
parser.ParseXml(xml, handlerStub);
// Assert
handlerStub.WasCalled()
.WithStartDocument()
.WithStartElement("outer")
.WithStartElement("inner")
.WithEndElement("inner")
.WithEndElement("outer")
.WithEndDocument();
}
public class HandlerStub : IHandler
{
private int _currentCursorIndex;
private readonly List<Tuple<Action, string>> _actionsCalled = new List<Tuple<Action, string>>();
public void StartDocument()
{
_actionsCalled.Add(new Tuple<Action, string>(Action.DocumentStarted, null));
}
public void StartElement(string elementName)
{
_actionsCalled.Add(new Tuple<Action, string>(Action.ElementStarted, elementName));
}
public void EndElement(string elementName)
{
_actionsCalled.Add(new Tuple<Action, string>(Action.ElementEnded, elementName));
}
public void EndDocument()
{
_actionsCalled.Add(new Tuple<Action, string>(Action.DocumentEnded, null));
}
public HandlerStub WasCalled()
{
// Reseting the cursor position
_currentCursorIndex = 0;
return this;
}
public HandlerStub WithStartDocument()
{
Assert.Equal(Action.DocumentStarted, _actionsCalled[_currentCursorIndex].Item1);
_currentCursorIndex++;
return this;
}
public HandlerStub WithStartElement(string elementName)
{
Tuple<Action, string> action = _actionsCalled[_currentCursorIndex];
Assert.Equal(Action.ElementStarted, action.Item1);
Assert.Equal(elementName, action.Item2);
_currentCursorIndex++;
return this;
}
public HandlerStub WithEndElement(string elementName)
{
Tuple<Action, string> action = _actionsCalled[_currentCursorIndex];
Assert.Equal(Action.ElementEnded, action.Item1);
Assert.Equal(elementName, action.Item2);
_currentCursorIndex++;
return this;
}
public HandlerStub WithEndDocument()
{
Assert.Equal(Action.DocumentEnded, _actionsCalled[_currentCursorIndex].Item1);
_currentCursorIndex++;
return this;
}
internal enum Action
{
DocumentStarted,
ElementStarted,
ElementEnded,
DocumentEnded
}
}
public interface IHandler
{
void StartDocument();
void StartElement(string elementName);
void EndElement(string elementName);
void EndDocument();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment