Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. [Fact]
  2. public void Parser_parses_xml_in_correct_order()
  3. {
  4. // Arrange : input values
  5. string xml = "<outer><inner /></outer>";
  6. var parser = new Parser();
  7.  
  8. // Arrange : record expectations
  9. var mocks = new MockRepository();
  10. IHandler handler = mocks.CreateMock<IHandler>();
  11. handler.StartDocument();
  12. handler.StartElement("outer");
  13. handler.StartElement("inner");
  14. handler.EndElement("inner");
  15. handler.EndElement("outer");
  16. handler.EndDocument();
  17. mocks.ReplayAll();
  18.  
  19. // Act
  20. parser.ParseXml(xml, handler);
  21.  
  22. // Assert
  23. mocks.VerifyAll();
  24. }
  25.  
  26.  
  27. [Fact]
  28. public void Parser_parses_xml_in_correct_order2()
  29. {
  30. // Arrange
  31. string xml = "<outer><inner /></outer>";
  32. var parser = new Parser();
  33. var handlerStub = new HandlerStub();
  34.  
  35. // Act
  36. parser.ParseXml(xml, handlerStub);
  37.  
  38. // Assert
  39. handlerStub.WasCalled()
  40. .WithStartDocument()
  41. .WithStartElement("outer")
  42. .WithStartElement("inner")
  43. .WithEndElement("inner")
  44. .WithEndElement("outer")
  45. .WithEndDocument();
  46. }
  47.  
  48.  
  49. public class HandlerStub : IHandler
  50. {
  51. private int _currentCursorIndex;
  52. private readonly List<Tuple<Action, string>> _actionsCalled = new List<Tuple<Action, string>>();
  53.  
  54. public void StartDocument()
  55. {
  56. _actionsCalled.Add(new Tuple<Action, string>(Action.DocumentStarted, null));
  57. }
  58.  
  59. public void StartElement(string elementName)
  60. {
  61. _actionsCalled.Add(new Tuple<Action, string>(Action.ElementStarted, elementName));
  62. }
  63.  
  64. public void EndElement(string elementName)
  65. {
  66. _actionsCalled.Add(new Tuple<Action, string>(Action.ElementEnded, elementName));
  67. }
  68.  
  69. public void EndDocument()
  70. {
  71. _actionsCalled.Add(new Tuple<Action, string>(Action.DocumentEnded, null));
  72. }
  73.  
  74. public HandlerStub WasCalled()
  75. {
  76. // Reseting the cursor position
  77. _currentCursorIndex = 0;
  78. return this;
  79. }
  80.  
  81. public HandlerStub WithStartDocument()
  82. {
  83. Assert.Equal(Action.DocumentStarted, _actionsCalled[_currentCursorIndex].Item1);
  84.  
  85. _currentCursorIndex++;
  86. return this;
  87. }
  88.  
  89. public HandlerStub WithStartElement(string elementName)
  90. {
  91. Tuple<Action, string> action = _actionsCalled[_currentCursorIndex];
  92. Assert.Equal(Action.ElementStarted, action.Item1);
  93. Assert.Equal(elementName, action.Item2);
  94.  
  95. _currentCursorIndex++;
  96. return this;
  97. }
  98.  
  99. public HandlerStub WithEndElement(string elementName)
  100. {
  101. Tuple<Action, string> action = _actionsCalled[_currentCursorIndex];
  102. Assert.Equal(Action.ElementEnded, action.Item1);
  103. Assert.Equal(elementName, action.Item2);
  104.  
  105. _currentCursorIndex++;
  106. return this;
  107. }
  108.  
  109. public HandlerStub WithEndDocument()
  110. {
  111. Assert.Equal(Action.DocumentEnded, _actionsCalled[_currentCursorIndex].Item1);
  112.  
  113. _currentCursorIndex++;
  114. return this;
  115. }
  116.  
  117. internal enum Action
  118. {
  119. DocumentStarted,
  120. ElementStarted,
  121. ElementEnded,
  122. DocumentEnded
  123. }
  124. }
  125.  
  126. public interface IHandler
  127. {
  128. void StartDocument();
  129. void StartElement(string elementName);
  130. void EndElement(string elementName);
  131. void EndDocument();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement