PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/v0.3/src/NBehave.Framework/Story.cs

#
C# | 215 lines | 166 code | 49 blank | 0 comment | 8 complexity | 7f6058d40242363b7b6cfd9578f7fd8f MD5 | raw file
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace NBehave.Framework
  5. {
  6. public class Story
  7. {
  8. private readonly string _title = null;
  9. private readonly IMessageProvider _messageProvider = null;
  10. private readonly Hashtable _actions = null;
  11. private List<Scenario> _scenarios = null;
  12. private LinkedList<ScenarioResults> _scenarioResults = null;
  13. private bool _isDryRun;
  14. public static event EventHandler<EventArgs<Story>> StoryCreated;
  15. protected static void OnStoryCreated(EventArgs<Story> e)
  16. {
  17. EventHandler<EventArgs<Story>> handler = StoryCreated;
  18. if (handler != null)
  19. handler(null, e);
  20. }
  21. public Story(string title) : this(title, MessageProviderRegistry.GetInstance()) { }
  22. public Story(string title, IMessageProvider messageProvider)
  23. {
  24. _title = title;
  25. _messageProvider = messageProvider;
  26. _scenarios = new List<Scenario>();
  27. _scenarioResults = new LinkedList<ScenarioResults>();
  28. _actions = new Hashtable();
  29. OnStoryCreated(new EventArgs<Story>(this));
  30. if (!string.IsNullOrEmpty(_title))
  31. {
  32. _messageProvider.AddMessage("Story: " + title);
  33. }
  34. }
  35. public string Title
  36. {
  37. get { return _title; }
  38. }
  39. public bool IsDryRun
  40. {
  41. get { return _isDryRun; }
  42. set { _isDryRun = value; }
  43. }
  44. public AsAFragment AsA(string role)
  45. {
  46. return new AsAFragment(role, this);
  47. }
  48. public Scenario WithScenario(string title)
  49. {
  50. Scenario scenario = new Scenario(title, this);
  51. AddScenario(scenario);
  52. AddMessage("");
  53. AddMessage(string.Format("\tScenario {0}: {1}", _scenarios.Count, scenario.Title));
  54. return scenario;
  55. }
  56. public void CompileResults(StoryResults results)
  57. {
  58. foreach (ScenarioResults result in _scenarioResults)
  59. {
  60. results.AddResult(result);
  61. }
  62. }
  63. internal void PendLastScenarioResults(string reason)
  64. {
  65. _scenarioResults.Last.Value.Pend(reason);
  66. }
  67. internal void AddMessage(string message)
  68. {
  69. _messageProvider.AddMessage(message);
  70. }
  71. private void InvokeActionBase(string type, string message, object originalAction, Action actionCallback, string outputMessage, params object[] messageParameters)
  72. {
  73. List<object> fullMessageParameters = new List<object>();
  74. fullMessageParameters.Add(type);
  75. fullMessageParameters.Add(message);
  76. fullMessageParameters.AddRange(messageParameters);
  77. if (!IsDryRun)
  78. {
  79. try
  80. {
  81. actionCallback();
  82. }
  83. catch (Exception ex)
  84. {
  85. _scenarioResults.Last.Value.Fail(ex);
  86. AddMessage(string.Format(outputMessage + " - FAILED", fullMessageParameters.ToArray()));
  87. throw;
  88. }
  89. }
  90. AddMessage(string.Format(outputMessage, fullMessageParameters.ToArray()));
  91. CatalogAction(message, originalAction);
  92. }
  93. internal void InvokeAction(string type, string message, Action action)
  94. {
  95. InvokeActionBase(type, message, action, delegate { action(); }, "{0} {1}");
  96. }
  97. internal void InvokeAction<TArg0>(string type, string message, Action<TArg0> action, TArg0 arg0)
  98. {
  99. InvokeActionBase(type, message, action, delegate { action(arg0); }, "{0} {1}: {2}", new object[] {arg0});
  100. }
  101. internal void InvokeAction<TArg0, TArg1>(string type, string message, Action<TArg0, TArg1> action, TArg0 arg0, TArg1 arg1)
  102. {
  103. InvokeActionBase(type, message, action, delegate { action(arg0, arg1); }, "{0} {1}: ({2}, {3})", new object[] { arg0, arg1 });
  104. }
  105. internal void InvokeAction<TArg0, TArg1, TArg2>(string type, string message, Action<TArg0, TArg1, TArg2> action, TArg0 arg0, TArg1 arg1, TArg2 arg2)
  106. {
  107. InvokeActionBase(type, message, action, delegate { action(arg0, arg1, arg2); }, "{0} {1}: ({2}, {3}, {4})", new object[] { arg0, arg1, arg2 });
  108. }
  109. internal void InvokeAction<TArg0, TArg1, TArg2, TArg3>(string type, string message, Action<TArg0, TArg1, TArg2, TArg3> action, TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3)
  110. {
  111. InvokeActionBase(type, message, action, delegate { action(arg0, arg1, arg2, arg3); }, "{0} {1}: ({2}, {3}, {4}, {5})", new object[] { arg0, arg1, arg2, arg3 });
  112. }
  113. internal void InvokeActionFromCatalog(string type, string message)
  114. {
  115. ValidateActionExists(message);
  116. Action action = (Action)GetActionFromCatalog(message);
  117. InvokeAction(type, message, action);
  118. }
  119. internal void InvokeActionFromCatalog<TArg0>(string type, string message, TArg0 arg0)
  120. {
  121. ValidateActionExists(message);
  122. Action<TArg0> action = (Action<TArg0>)GetActionFromCatalog(message);
  123. InvokeAction(type, message, action, arg0);
  124. }
  125. internal void InvokeActionFromCatalog<TArg0, TArg1>(string type, string message, TArg0 arg0, TArg1 arg1)
  126. {
  127. ValidateActionExists(message);
  128. Action<TArg0, TArg1> action = (Action<TArg0, TArg1>)GetActionFromCatalog(message);
  129. InvokeAction(type, message, action, arg0, arg1);
  130. }
  131. internal void InvokeActionFromCatalog<TArg0, TArg1, TArg2>(string type, string message, TArg0 arg0, TArg1 arg1, TArg2 arg2)
  132. {
  133. ValidateActionExists(message);
  134. Action<TArg0, TArg1, TArg2> action = (Action<TArg0, TArg1, TArg2>)GetActionFromCatalog(message);
  135. InvokeAction(type, message, action, arg0, arg1, arg2);
  136. }
  137. internal void InvokeActionFromCatalog<TArg0, TArg1, TArg2, TArg3>(string type, string message, TArg0 arg0, TArg1 arg1, TArg2 arg2, TArg3 arg3)
  138. {
  139. ValidateActionExists(message);
  140. Action<TArg0, TArg1, TArg2, TArg3> action = (Action<TArg0, TArg1, TArg2, TArg3>)GetActionFromCatalog(message);
  141. InvokeAction(type, message, action, arg0, arg1, arg2, arg3);
  142. }
  143. private void CatalogAction(string message, object action)
  144. {
  145. if (_actions.ContainsKey(message))
  146. return;
  147. _actions.Add(message, action);
  148. }
  149. private void ValidateActionExists(string message)
  150. {
  151. if (!_actions.ContainsKey(message) && ! IsDryRun)
  152. throw new ActionMissingException(string.Format("Action missing for action '{0}'.", message));
  153. }
  154. private object GetActionFromCatalog(string message)
  155. {
  156. if (IsDryRun)
  157. return null;
  158. return _actions[message];
  159. }
  160. private void AddScenario(Scenario scenario)
  161. {
  162. _scenarios.Add(scenario);
  163. _scenarioResults.AddLast(new ScenarioResults(Title, scenario.Title));
  164. }
  165. }
  166. }