PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/Builders/ValueSourceProvider.cs

#
C# | 108 lines | 62 code | 13 blank | 33 comment | 7 complexity | 8268544904d9ddafed8ecbda70593831 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.Reflection;
  8. using System.Collections;
  9. using NUnit.Core.Extensibility;
  10. namespace NUnit.Core.Builders
  11. {
  12. /// <summary>
  13. /// ValueSourceProvider supplies data items for individual parameters
  14. /// from named data sources in the test class or a separate class.
  15. /// </summary>
  16. public class ValueSourceProvider : IDataPointProvider2
  17. {
  18. #region Constants
  19. public const string SourcesAttribute = "NUnit.Framework.ValueSourceAttribute";
  20. public const string SourceTypeProperty = "SourceType";
  21. public const string SourceNameProperty = "SourceName";
  22. #endregion
  23. #region IDataPointProvider Members
  24. /// <summary>
  25. /// Determine whether any data sources are available for a parameter.
  26. /// </summary>
  27. /// <param name="parameter">A ParameterInfo test parameter</param>
  28. /// <returns>True if any data is available, otherwise false.</returns>
  29. public bool HasDataFor(ParameterInfo parameter)
  30. {
  31. return Reflect.HasAttribute(parameter, SourcesAttribute, false);
  32. }
  33. /// <summary>
  34. /// Return an IEnumerable providing test data for use with
  35. /// one parameter of a parameterized test.
  36. /// </summary>
  37. /// <param name="parameter"></param>
  38. /// <returns></returns>
  39. public IEnumerable GetDataFor(ParameterInfo parameter)
  40. {
  41. return GetDataFor(parameter, null);
  42. }
  43. #endregion
  44. #region IDataPointProvider2 Members
  45. /// <summary>
  46. /// Determine whether any data sources are available for a parameter.
  47. /// </summary>
  48. /// <param name="parameter">A ParameterInfo test parameter</param>
  49. /// <param name="parentSuite">The test suite for which the test is being built</param>
  50. /// <returns>True if any data is available, otherwise false.</returns>
  51. public bool HasDataFor(ParameterInfo parameter, Test parentSuite)
  52. {
  53. return HasDataFor(parameter);
  54. }
  55. /// <summary>
  56. /// Return an IEnumerable providing test data for use with
  57. /// one parameter of a parameterized test.
  58. /// </summary>
  59. /// <param name="parameter"></param>
  60. /// <param name="parentSuite">The test suite for which the test is being built</param>
  61. /// <returns></returns>
  62. public IEnumerable GetDataFor(ParameterInfo parameter, Test parentSuite)
  63. {
  64. ArrayList parameterList = new ArrayList();
  65. foreach (ProviderReference providerRef in GetSourcesFor(parameter, parentSuite))
  66. {
  67. IEnumerable instance = providerRef.GetInstance();
  68. if (instance != null)
  69. foreach (object o in instance)
  70. parameterList.Add(o);
  71. }
  72. return parameterList;
  73. }
  74. #endregion
  75. #region Helper Methods
  76. private static IList GetSourcesFor(ParameterInfo parameter, Test parent)
  77. {
  78. ArrayList sources = new ArrayList();
  79. TestFixture parentSuite = parent as TestFixture;
  80. foreach (Attribute sourceAttr in Reflect.GetAttributes(parameter, SourcesAttribute, false))
  81. {
  82. Type sourceType = Reflect.GetPropertyValue(sourceAttr, SourceTypeProperty) as Type;
  83. string sourceName = Reflect.GetPropertyValue(sourceAttr, SourceNameProperty) as string;
  84. if (sourceType != null)
  85. sources.Add(new ProviderReference(sourceType, sourceName));
  86. else if (parentSuite != null)
  87. sources.Add(new ProviderReference(parentSuite.FixtureType, parentSuite.arguments, sourceName));
  88. else
  89. sources.Add(new ProviderReference(parameter.Member.ReflectedType, sourceName));
  90. }
  91. return sources;
  92. }
  93. #endregion
  94. }
  95. }