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