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

/src/NUnit/interfaces/TestName.cs

#
C# | 204 lines | 107 code | 23 blank | 74 comment | 19 complexity | 0c6a3268893ce6ae54a62190050afae4 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2007, 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. namespace NUnit.Core
  8. {
  9. /// <summary>
  10. /// TestName encapsulates all info needed to identify and
  11. /// locate a test that has been loaded by a runner. It consists
  12. /// of a three components: the simple name of the test, an int
  13. /// id that is unique to a given tree of tests and an int
  14. /// runner id that identifies the particular runner that
  15. /// holds the test instance.
  16. /// </summary>
  17. [Serializable]
  18. public class TestName : ICloneable
  19. {
  20. #region Fields
  21. /// <summary>
  22. /// ID that uniquely identifies the test
  23. /// </summary>
  24. private TestID testID;
  25. private int runnerID;
  26. /// <summary>
  27. /// The simple name of the test, without qualification
  28. /// </summary>
  29. private string name;
  30. /// <summary>
  31. /// The fully qualified name of the test
  32. /// </summary>
  33. private string fullName;
  34. #endregion
  35. #region Properties
  36. /// <summary>
  37. /// Gets or sets the TestID that uniquely identifies this test
  38. /// </summary>
  39. public TestID TestID
  40. {
  41. get { return testID; }
  42. set { testID = value; }
  43. }
  44. /// <summary>
  45. /// Gets the ID for the runner that created the test from
  46. /// the TestID, or returns -1 if the TestID is null.
  47. /// </summary>
  48. public int RunnerID
  49. {
  50. get { return runnerID; }
  51. set { runnerID = value; }
  52. }
  53. /// <summary>
  54. /// Gets or sets the simple name of the test
  55. /// </summary>
  56. public string Name
  57. {
  58. get { return name; }
  59. set { name = value; }
  60. }
  61. /// <summary>
  62. /// Gets or sets the full (qualified) name of the test
  63. /// </summary>
  64. public string FullName
  65. {
  66. get { return fullName; }
  67. set { fullName = value; }
  68. }
  69. /// <summary>
  70. /// Get the string representation of this test name, incorporating all
  71. /// the components of the name.
  72. /// </summary>
  73. public string UniqueName
  74. {
  75. get
  76. {
  77. if ( this.testID == null )
  78. return string.Format( "[{0}]{1}", this.runnerID, this.fullName );
  79. else
  80. return string.Format( "[{0}-{1}]{2}", this.RunnerID, this.testID, this.fullName );
  81. }
  82. }
  83. #endregion
  84. #region Static Methods
  85. /// <summary>
  86. /// Parse a string representation of a TestName,
  87. /// returning a TestName.
  88. /// </summary>
  89. /// <param name="s">The string to parse</param>
  90. /// <returns>A TestName</returns>
  91. public static TestName Parse( string s )
  92. {
  93. if ( s == null ) throw new ArgumentNullException( "s", "Cannot parse a null string" );
  94. TestName testName = new TestName();
  95. testName.FullName = testName.Name = s;
  96. if ( s.StartsWith( "[" ) )
  97. {
  98. int rbrack = s.IndexOf( "]" );
  99. if ( rbrack < 0 || rbrack == s.Length - 1 )
  100. throw new FormatException( "Invalid TestName format: " + s );
  101. testName.FullName = testName.Name = s.Substring( rbrack + 1 );
  102. int dash = s.IndexOf( "-" );
  103. if ( dash < 0 || dash > rbrack )
  104. testName.RunnerID = Int32.Parse( s.Substring( 1, rbrack - 1 ) );
  105. else
  106. {
  107. testName.RunnerID = Int32.Parse( s.Substring( 1, dash - 1 ) );
  108. testName.TestID = TestID.Parse( s.Substring( dash + 1, rbrack - dash - 1 ) );
  109. }
  110. }
  111. return testName;
  112. }
  113. #endregion
  114. #region Object Overrides
  115. /// <summary>
  116. /// Compares two TestNames for equality
  117. /// </summary>
  118. /// <param name="obj">the other TestID</param>
  119. /// <returns>True if the two TestIDs are equal</returns>
  120. public override bool Equals(object obj)
  121. {
  122. TestName other = obj as TestName;
  123. if ( other == null )
  124. return base.Equals (obj);
  125. return this.TestID == other.testID
  126. && this.runnerID == other.runnerID
  127. && this.fullName == other.fullName;
  128. }
  129. /// <summary>
  130. /// Calculates a hashcode for this TestID
  131. /// </summary>
  132. /// <returns>The hash code.</returns>
  133. public override int GetHashCode()
  134. {
  135. return unchecked( this.testID.GetHashCode() + this.fullName.GetHashCode() );
  136. }
  137. /// <summary>
  138. /// Override ToString() to display the UniqueName
  139. /// </summary>
  140. /// <returns></returns>
  141. public override string ToString()
  142. {
  143. return this.UniqueName;
  144. }
  145. #endregion
  146. #region Operator Overrides
  147. /// <summary>
  148. /// Override the == operator
  149. /// </summary>
  150. /// <param name="name1"></param>
  151. /// <param name="name2"></param>
  152. /// <returns></returns>
  153. public static bool operator ==( TestName name1, TestName name2 )
  154. {
  155. if ( Object.Equals( name1, null ) )
  156. return Object.Equals( name2, null );
  157. return name1.Equals( name2 );
  158. }
  159. /// <summary>
  160. /// Override the != operator
  161. /// </summary>
  162. /// <param name="name1"></param>
  163. /// <param name="name2"></param>
  164. /// <returns></returns>
  165. public static bool operator !=( TestName name1, TestName name2 )
  166. {
  167. return name1 == name2 ? false : true;
  168. }
  169. #endregion
  170. #region ICloneable Implementation
  171. /// <summary>
  172. /// Returns a duplicate of this TestName
  173. /// </summary>
  174. /// <returns></returns>
  175. public object Clone()
  176. {
  177. return this.MemberwiseClone();
  178. }
  179. #endregion
  180. }
  181. }