Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [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();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement