PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/Builders/TestCaseSourceProvider.cs

#
C# | 109 lines | 64 code | 14 blank | 31 comment | 4 complexity | 22f47e5f473a762b3074f926a798ed55 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.Reflection;
  9. using NUnit.Core.Extensibility;
  10. namespace NUnit.Core.Builders
  11. {
  12. /// <summary>
  13. /// TestCaseSourceProvider provides data for methods
  14. /// annotated with the TestCaseSourceAttribute.
  15. /// </summary>
  16. public class TestCaseSourceProvider : ITestCaseProvider2
  17. {
  18. #region Constants
  19. public const string SourceTypeProperty = "SourceType";
  20. public const string SourceNameProperty = "SourceName";
  21. #endregion
  22. #region ITestCaseProvider Members
  23. /// <summary>
  24. /// Determine whether any test cases are available for a parameterized method.
  25. /// </summary>
  26. /// <param name="method">A MethodInfo representing a parameterized test</param>
  27. /// <returns>True if any cases are available, otherwise false.</returns>
  28. public bool HasTestCasesFor(MethodInfo method)
  29. {
  30. return Reflect.HasAttribute(method, NUnitFramework.TestCaseSourceAttribute, false);
  31. }
  32. /// <summary>
  33. /// Return an IEnumerable providing test cases for use in
  34. /// running a parameterized test.
  35. /// </summary>
  36. /// <param name="method"></param>
  37. /// <returns></returns>
  38. public IEnumerable GetTestCasesFor(MethodInfo method)
  39. {
  40. return GetTestCasesFor(method, null);
  41. }
  42. #endregion
  43. #region ITestCaseProvider2 Members
  44. /// <summary>
  45. /// Determine whether any test cases are available for a parameterized method.
  46. /// </summary>
  47. /// <param name="method">A MethodInfo representing a parameterized test</param>
  48. /// <returns>True if any cases are available, otherwise false.</returns>
  49. public bool HasTestCasesFor(MethodInfo method, Test suite)
  50. {
  51. return HasTestCasesFor(method);
  52. }
  53. /// <summary>
  54. /// Return an IEnumerable providing test cases for use in
  55. /// running a parameterized test.
  56. /// </summary>
  57. /// <param name="method"></param>
  58. /// <returns></returns>
  59. public IEnumerable GetTestCasesFor(MethodInfo method, Test parentSuite)
  60. {
  61. ArrayList parameterList = new ArrayList();
  62. foreach (ProviderReference info in GetSourcesFor(method, parentSuite))
  63. {
  64. foreach (object o in info.GetInstance())
  65. parameterList.Add(o);
  66. }
  67. return parameterList;
  68. }
  69. #endregion
  70. #region Helper Methods
  71. private static IList GetSourcesFor(MethodInfo method, Test parent)
  72. {
  73. ArrayList sources = new ArrayList();
  74. TestFixture parentSuite = parent as TestFixture;
  75. foreach (Attribute sourceAttr in Reflect.GetAttributes(method, NUnitFramework.TestCaseSourceAttribute, false))
  76. {
  77. Type sourceType = Reflect.GetPropertyValue(sourceAttr, SourceTypeProperty) as Type;
  78. string sourceName = Reflect.GetPropertyValue(sourceAttr, SourceNameProperty) as string;
  79. if (sourceType == null)
  80. {
  81. if (parentSuite != null)
  82. sources.Add(new ProviderReference(parentSuite.FixtureType, parentSuite.arguments, sourceName));
  83. else
  84. sources.Add(new ProviderReference(method.ReflectedType, sourceName));
  85. }
  86. else
  87. {
  88. sources.Add(new ProviderReference(sourceType, sourceName));
  89. }
  90. }
  91. return sources;
  92. }
  93. #endregion
  94. }
  95. }