PageRenderTime 93ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/interfaces/Extensibility/ParameterSet.cs

#
C# | 324 lines | 198 code | 35 blank | 91 comment | 25 complexity | c64c3dd023361262f4b1cfd29d39e70b MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org
  5. // ****************************************************************
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Specialized;
  9. using System.Reflection;
  10. namespace NUnit.Core.Extensibility
  11. {
  12. /// <summary>
  13. /// ParameterSet encapsulates method arguments and
  14. /// other selected parameters needed for constructing
  15. /// a parameterized test case.
  16. /// </summary>
  17. public class ParameterSet : NUnit.Framework.ITestCaseData
  18. {
  19. #region Constants
  20. private static readonly string DESCRIPTION = "_DESCRIPTION";
  21. private static readonly string IGNOREREASON = "_IGNOREREASON";
  22. private static readonly string CATEGORIES = "_CATEGORIES";
  23. #endregion
  24. #region Instance Fields
  25. private RunState runState;
  26. private Exception providerException;
  27. private object[] arguments;
  28. private object[] originalArguments;
  29. private System.Type expectedExceptionType;
  30. private string expectedExceptionName;
  31. private string expectedMessage;
  32. private string matchType;
  33. private object result;
  34. private string testName;
  35. private string ignoreReason;
  36. private bool isIgnored;
  37. private bool hasExpectedResult;
  38. /// <summary>
  39. /// A dictionary of properties, used to add information
  40. /// to tests without requiring the class to change.
  41. /// </summary>
  42. private IDictionary properties;
  43. #endregion
  44. #region Properties
  45. /// <summary>
  46. /// The RunState for this set of parameters.
  47. /// </summary>
  48. public RunState RunState
  49. {
  50. get { return runState; }
  51. set { runState = value; }
  52. }
  53. /// <summary>
  54. /// The reason for not running the test case
  55. /// represented by this ParameterSet
  56. /// </summary>
  57. public string NotRunReason
  58. {
  59. get { return (string) Properties[IGNOREREASON]; }
  60. }
  61. /// <summary>
  62. /// Holds any exception thrown by the parameter provider
  63. /// </summary>
  64. public Exception ProviderException
  65. {
  66. get { return providerException; }
  67. }
  68. /// <summary>
  69. /// The arguments to be used in running the test,
  70. /// which must match the method signature.
  71. /// </summary>
  72. public object[] Arguments
  73. {
  74. get { return arguments; }
  75. set
  76. {
  77. arguments = value;
  78. if (originalArguments == null)
  79. originalArguments = value;
  80. }
  81. }
  82. /// <summary>
  83. /// The original arguments supplied by the user,
  84. /// used for display purposes.
  85. /// </summary>
  86. public object[] OriginalArguments
  87. {
  88. get { return originalArguments; }
  89. }
  90. /// <summary>
  91. /// The Type of any exception that is expected.
  92. /// </summary>
  93. public System.Type ExpectedException
  94. {
  95. get { return expectedExceptionType; }
  96. set { expectedExceptionType = value; }
  97. }
  98. /// <summary>
  99. /// The FullName of any exception that is expected
  100. /// </summary>
  101. public string ExpectedExceptionName
  102. {
  103. get { return expectedExceptionName; }
  104. set { expectedExceptionName = value; }
  105. }
  106. /// <summary>
  107. /// The Message of any exception that is expected
  108. /// </summary>
  109. public string ExpectedMessage
  110. {
  111. get { return expectedMessage; }
  112. set { expectedMessage = value; }
  113. }
  114. /// <summary>
  115. /// Gets or sets the type of match to be performed on the expected message
  116. /// </summary>
  117. public string MatchType
  118. {
  119. get { return matchType; }
  120. set { matchType = value; }
  121. }
  122. /// <summary>
  123. /// The expected result of the test, which
  124. /// must match the method return type.
  125. /// </summary>
  126. public object Result
  127. {
  128. get { return result; }
  129. set
  130. {
  131. result = value;
  132. hasExpectedResult = true;
  133. }
  134. }
  135. /// <summary>
  136. /// Returns true if an expected result has been
  137. /// specified for this parameter set.
  138. /// </summary>
  139. public bool HasExpectedResult
  140. {
  141. get { return hasExpectedResult; }
  142. }
  143. /// <summary>
  144. /// A description to be applied to this test case
  145. /// </summary>
  146. public string Description
  147. {
  148. get { return (string) Properties[DESCRIPTION]; }
  149. set
  150. {
  151. if (value != null)
  152. Properties[DESCRIPTION] = value;
  153. else
  154. Properties.Remove(DESCRIPTION);
  155. }
  156. }
  157. /// <summary>
  158. /// A name to be used for this test case in lieu
  159. /// of the standard generated name containing
  160. /// the argument list.
  161. /// </summary>
  162. public string TestName
  163. {
  164. get { return testName; }
  165. set { testName = value; }
  166. }
  167. /// <summary>
  168. /// Gets or sets a value indicating whether this <see cref="ParameterSet"/> is ignored.
  169. /// </summary>
  170. /// <value><c>true</c> if ignored; otherwise, <c>false</c>.</value>
  171. public bool Ignored
  172. {
  173. get { return isIgnored; }
  174. set { isIgnored = value; }
  175. }
  176. /// <summary>
  177. /// Gets or sets the ignore reason.
  178. /// </summary>
  179. /// <value>The ignore reason.</value>
  180. public string IgnoreReason
  181. {
  182. get { return ignoreReason; }
  183. set { ignoreReason = value; }
  184. }
  185. /// <summary>
  186. /// Gets a list of categories associated with this test.
  187. /// </summary>
  188. public IList Categories
  189. {
  190. get
  191. {
  192. if (Properties[CATEGORIES] == null)
  193. Properties[CATEGORIES] = new ArrayList();
  194. return (IList)Properties[CATEGORIES];
  195. }
  196. }
  197. /// <summary>
  198. /// Gets the property dictionary for this test
  199. /// </summary>
  200. public IDictionary Properties
  201. {
  202. get
  203. {
  204. if (properties == null)
  205. properties = new ListDictionary();
  206. return properties;
  207. }
  208. }
  209. #endregion
  210. #region Constructors
  211. /// <summary>
  212. /// Construct a non-runnable ParameterSet, specifying
  213. /// the provider excetpion that made it invalid.
  214. /// </summary>
  215. public ParameterSet(Exception exception)
  216. {
  217. this.runState = RunState.NotRunnable;
  218. this.providerException = exception;
  219. }
  220. /// <summary>
  221. /// Construct an empty parameter set, which
  222. /// defaults to being Runnable.
  223. /// </summary>
  224. public ParameterSet()
  225. {
  226. this.runState = RunState.Runnable;
  227. }
  228. #endregion
  229. #region Static Methods
  230. /// <summary>
  231. /// Constructs a ParameterSet from another object, accessing properties
  232. /// by reflection. The object must expose at least an Arguments property
  233. /// in order for the test to be runnable.
  234. /// </summary>
  235. /// <param name="source"></param>
  236. public static ParameterSet FromDataSource(object source)
  237. {
  238. ParameterSet parms = new ParameterSet();
  239. parms.Arguments = GetParm(source, PropertyNames.Arguments) as object[];
  240. parms.ExpectedException = GetParm(source, PropertyNames.ExpectedException) as Type;
  241. if (parms.ExpectedException != null)
  242. parms.ExpectedExceptionName = parms.ExpectedException.FullName;
  243. else
  244. parms.ExpectedExceptionName = GetParm(source, PropertyNames.ExpectedExceptionName) as string;
  245. parms.ExpectedMessage = GetParm(source, PropertyNames.ExpectedMessage) as string;
  246. object matchEnum = GetParm(source, PropertyNames.MatchType);
  247. if ( matchEnum != null )
  248. parms.MatchType = matchEnum.ToString();
  249. parms.Result = GetParm(source, PropertyNames.Result);
  250. parms.Description = GetParm(source, PropertyNames.Description) as string;
  251. parms.TestName = GetParm(source, PropertyNames.TestName) as string;
  252. object objIgnore = GetParm(source, PropertyNames.Ignored);
  253. if ( objIgnore != null )
  254. parms.Ignored = (bool)objIgnore;
  255. parms.IgnoreReason = GetParm(source, PropertyNames.IgnoreReason) as string;
  256. // Some sources may also implement Properties and/or Categories
  257. bool gotCategories = false;
  258. IDictionary props = GetParm(source, PropertyNames.Properties) as IDictionary;
  259. if ( props != null )
  260. foreach (string key in props.Keys)
  261. {
  262. parms.Properties.Add(key, props[key]);
  263. if (key == CATEGORIES) gotCategories = true;
  264. }
  265. // Some sources implement Categories. They may have been
  266. // provided as properties or they may be separate.
  267. if (!gotCategories)
  268. {
  269. IList categories = GetParm(source, PropertyNames.Categories) as IList;
  270. if (categories != null)
  271. foreach (string cat in categories)
  272. parms.Categories.Add(cat);
  273. }
  274. return parms;
  275. }
  276. private static object GetParm(object source, string name)
  277. {
  278. Type type = source.GetType();
  279. PropertyInfo prop = type.GetProperty(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
  280. if (prop != null)
  281. return prop.GetValue(source, null);
  282. FieldInfo field = type.GetField(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField);
  283. if (field != null)
  284. return field.GetValue(source);
  285. return null;
  286. }
  287. #endregion
  288. }
  289. }