PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Attributes/ValuesAttribute.cs

#
C# | 130 lines | 67 code | 17 blank | 46 comment | 23 complexity | f97034591d7c0f104d2da7a77039a3fb 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. namespace NUnit.Framework
  10. {
  11. /// <summary>
  12. /// Abstract base class for attributes that apply to parameters
  13. /// and supply data for the parameter.
  14. /// </summary>
  15. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
  16. public abstract class ParameterDataAttribute : Attribute
  17. {
  18. /// <summary>
  19. /// Gets the data to be provided to the specified parameter
  20. /// </summary>
  21. public abstract IEnumerable GetData(ParameterInfo parameter);
  22. }
  23. /// <summary>
  24. /// ValuesAttribute is used to provide literal arguments for
  25. /// an individual parameter of a test.
  26. /// </summary>
  27. [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
  28. public class ValuesAttribute : ParameterDataAttribute
  29. {
  30. /// <summary>
  31. /// The collection of data to be returned. Must
  32. /// be set by any derived attribute classes.
  33. /// We use an object[] so that the individual
  34. /// elements may have their type changed in GetData
  35. /// if necessary.
  36. /// </summary>
  37. // TODO: This causes a lot of boxing so we should eliminate it.
  38. protected object[] data;
  39. /// <summary>
  40. /// Construct with one argument
  41. /// </summary>
  42. /// <param name="arg1"></param>
  43. public ValuesAttribute(object arg1)
  44. {
  45. data = new object[] { arg1 };
  46. }
  47. /// <summary>
  48. /// Construct with two arguments
  49. /// </summary>
  50. /// <param name="arg1"></param>
  51. /// <param name="arg2"></param>
  52. public ValuesAttribute(object arg1, object arg2)
  53. {
  54. data = new object[] { arg1, arg2 };
  55. }
  56. /// <summary>
  57. /// Construct with three arguments
  58. /// </summary>
  59. /// <param name="arg1"></param>
  60. /// <param name="arg2"></param>
  61. /// <param name="arg3"></param>
  62. public ValuesAttribute(object arg1, object arg2, object arg3)
  63. {
  64. data = new object[] { arg1, arg2, arg3 };
  65. }
  66. /// <summary>
  67. /// Construct with an array of arguments
  68. /// </summary>
  69. /// <param name="args"></param>
  70. public ValuesAttribute(params object[] args)
  71. {
  72. data = args;
  73. }
  74. /// <summary>
  75. /// Get the collection of values to be used as arguments
  76. /// </summary>
  77. public override IEnumerable GetData(ParameterInfo parameter)
  78. {
  79. Type targetType = parameter.ParameterType;
  80. for (int i = 0; i < data.Length; i++)
  81. {
  82. object arg = data[i];
  83. if (arg == null)
  84. continue;
  85. if (arg.GetType().FullName == "NUnit.Framework.SpecialValue" &&
  86. arg.ToString() == "Null")
  87. {
  88. data[i] = null;
  89. continue;
  90. }
  91. if (targetType.IsAssignableFrom(arg.GetType()))
  92. continue;
  93. if (arg is DBNull)
  94. {
  95. data[i] = null;
  96. continue;
  97. }
  98. bool convert = false;
  99. if (targetType == typeof(short) || targetType == typeof(byte) || targetType == typeof(sbyte))
  100. convert = arg is int;
  101. else
  102. if (targetType == typeof(decimal))
  103. convert = arg is double || arg is string || arg is int;
  104. else
  105. if (targetType == typeof(DateTime) || targetType == typeof(TimeSpan))
  106. convert = arg is string;
  107. if (convert)
  108. data[i] = Convert.ChangeType(arg, targetType, System.Globalization.CultureInfo.InvariantCulture);
  109. }
  110. return data;
  111. }
  112. }
  113. }