PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/framework/Attributes/TestFixtureAttribute.cs

#
C# | 171 lines | 106 code | 20 blank | 45 comment | 18 complexity | 6a2f29671887262e93c836b479414b42 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. namespace NUnit.Framework
  7. {
  8. using System;
  9. using System.Collections;
  10. /// <example>
  11. /// [TestFixture]
  12. /// public class ExampleClass
  13. /// {}
  14. /// </example>
  15. [AttributeUsage(AttributeTargets.Class, AllowMultiple=true, Inherited=true)]
  16. public class TestFixtureAttribute : Attribute
  17. {
  18. private string description;
  19. private object[] arguments;
  20. private bool isIgnored;
  21. private string ignoreReason;
  22. private string category;
  23. #if NET_2_0
  24. private Type[] typeArgs;
  25. private bool argsSeparated;
  26. #endif
  27. /// <summary>
  28. /// Default constructor
  29. /// </summary>
  30. public TestFixtureAttribute() : this( null ) { }
  31. /// <summary>
  32. /// Construct with a object[] representing a set of arguments.
  33. /// In .NET 2.0, the arguments may later be separated into
  34. /// type arguments and constructor arguments.
  35. /// </summary>
  36. /// <param name="arguments"></param>
  37. public TestFixtureAttribute(params object[] arguments)
  38. {
  39. this.arguments = arguments == null
  40. ? new object[0]
  41. : arguments;
  42. for (int i = 0; i < this.arguments.Length; i++)
  43. if (arguments[i] is SpecialValue && (SpecialValue)arguments[i] == SpecialValue.Null)
  44. arguments[i] = null;
  45. }
  46. /// <summary>
  47. /// Descriptive text for this fixture
  48. /// </summary>
  49. public string Description
  50. {
  51. get { return description; }
  52. set { description = value; }
  53. }
  54. /// <summary>
  55. /// Gets and sets the category for this fixture.
  56. /// May be a comma-separated list of categories.
  57. /// </summary>
  58. public string Category
  59. {
  60. get { return category; }
  61. set { category = value; }
  62. }
  63. /// <summary>
  64. /// Gets a list of categories for this fixture
  65. /// </summary>
  66. public IList Categories
  67. {
  68. get { return category == null ? null : category.Split(','); }
  69. }
  70. /// <summary>
  71. /// The arguments originally provided to the attribute
  72. /// </summary>
  73. public object[] Arguments
  74. {
  75. get
  76. {
  77. #if NET_2_0
  78. if (!argsSeparated)
  79. SeparateArgs();
  80. #endif
  81. return arguments;
  82. }
  83. }
  84. /// <summary>
  85. /// Gets or sets a value indicating whether this <see cref="TestFixtureAttribute"/> should be ignored.
  86. /// </summary>
  87. /// <value><c>true</c> if ignore; otherwise, <c>false</c>.</value>
  88. public bool Ignore
  89. {
  90. get { return isIgnored; }
  91. set { isIgnored = value; }
  92. }
  93. /// <summary>
  94. /// Gets or sets the ignore reason. May set Ignored as a side effect.
  95. /// </summary>
  96. /// <value>The ignore reason.</value>
  97. public string IgnoreReason
  98. {
  99. get { return ignoreReason; }
  100. set
  101. {
  102. ignoreReason = value;
  103. isIgnored = ignoreReason != null && ignoreReason != string.Empty;
  104. }
  105. }
  106. #if NET_2_0
  107. /// <summary>
  108. /// Get or set the type arguments. If not set
  109. /// explicitly, any leading arguments that are
  110. /// Types are taken as type arguments.
  111. /// </summary>
  112. public Type[] TypeArgs
  113. {
  114. get
  115. {
  116. if (!argsSeparated)
  117. SeparateArgs();
  118. return typeArgs;
  119. }
  120. set
  121. {
  122. typeArgs = value;
  123. argsSeparated = true;
  124. }
  125. }
  126. private void SeparateArgs()
  127. {
  128. int cnt = 0;
  129. if (arguments != null)
  130. {
  131. foreach (object o in arguments)
  132. if (o is Type) cnt++;
  133. else break;
  134. typeArgs = new Type[cnt];
  135. for (int i = 0; i < cnt; i++)
  136. typeArgs[i] = (Type)arguments[i];
  137. if (cnt > 0)
  138. {
  139. object[] args = new object[arguments.Length - cnt];
  140. for (int i = 0; i < args.Length; i++)
  141. args[i] = arguments[cnt + i];
  142. arguments = args;
  143. }
  144. }
  145. else
  146. typeArgs = new Type[0];
  147. argsSeparated = true;
  148. }
  149. #endif
  150. }
  151. }