File: TestName.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (204 lines) | stat: -rw-r--r-- 5,286 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// ****************************************************************
// Copyright 2007, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************
using System;

namespace NUnit.Core
{
	/// <summary>
	/// TestName encapsulates all info needed to identify and
	/// locate a test that has been loaded by a runner. It consists
	/// of a three components: the simple name of the test, an int
	/// id that is unique to a given tree of tests and an int 
	/// runner id that identifies the particular runner that
	/// holds the test instance.
	/// </summary>
	[Serializable]
	public class TestName : ICloneable
	{
		#region Fields
		/// <summary>
		/// ID that uniquely identifies the test
		/// </summary>
		private TestID testID;

		private int runnerID;

		/// <summary>
		/// The simple name of the test, without qualification
		/// </summary>
		private string name;

		/// <summary>
		/// The fully qualified name of the test
		/// </summary>
		private string fullName;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or sets the TestID that uniquely identifies this test
		/// </summary>
		public TestID TestID
		{
			get { return testID; }
			set { testID = value; }
		}

		/// <summary>
		/// Gets the ID for the runner that created the test from
		/// the TestID, or returns -1 if the TestID is null.
		/// </summary>
		public int RunnerID
		{
			get { return runnerID; }
			set { runnerID = value; }
		}

		/// <summary>
		/// Gets or sets the simple name of the test
		/// </summary>
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		/// <summary>
		/// Gets or sets the full (qualified) name of the test
		/// </summary>
		public string FullName
		{
			get { return fullName; }
			set { fullName = value; }
		}

		/// <summary>
		/// Get the string representation of this test name, incorporating all
		/// the components of the name.
		/// </summary>
		public string UniqueName
		{
			get
			{
				if ( this.testID == null )
					return string.Format( "[{0}]{1}", this.runnerID, this.fullName );
				else
					return string.Format( "[{0}-{1}]{2}", this.RunnerID, this.testID, this.fullName );
			}
		}
		#endregion

		#region Static Methods
        /// <summary>
        /// Parse a string representation of a TestName,
        /// returning a TestName.
        /// </summary>
        /// <param name="s">The string to parse</param>
        /// <returns>A TestName</returns>
		public static TestName Parse( string s )
		{
			if ( s == null ) throw new ArgumentNullException( "s", "Cannot parse a null string" );

			TestName testName = new TestName();
			testName.FullName = testName.Name = s;

			if ( s.StartsWith( "[" ) )
			{
				int rbrack = s.IndexOf( "]" );
				if ( rbrack < 0 || rbrack == s.Length - 1 )
					throw new FormatException( "Invalid TestName format: " + s );

				testName.FullName = testName.Name = s.Substring( rbrack + 1 );

				int dash = s.IndexOf( "-" );
				if ( dash < 0 || dash > rbrack )
					testName.RunnerID = Int32.Parse( s.Substring( 1, rbrack - 1 ) );
				else
				{
					testName.RunnerID = Int32.Parse( s.Substring( 1, dash - 1 ) );
					testName.TestID = TestID.Parse( s.Substring( dash + 1, rbrack - dash - 1 ) );
				}
			}

			return testName;
		}
		#endregion

		#region Object Overrides
		/// <summary>
		/// Compares two TestNames for equality
		/// </summary>
		/// <param name="obj">the other TestID</param>
		/// <returns>True if the two TestIDs are equal</returns>
		public override bool Equals(object obj)
		{
			TestName other = obj as TestName;
			if ( other == null )
				return base.Equals (obj);

			return this.TestID == other.testID
				&& this.runnerID == other.runnerID 
				&& this.fullName == other.fullName;
		}

		/// <summary>
		/// Calculates a hashcode for this TestID
		/// </summary>
		/// <returns>The hash code.</returns>
		public override int GetHashCode()
		{
			return unchecked( this.testID.GetHashCode() + this.fullName.GetHashCode() );
		}

		/// <summary>
		/// Override ToString() to display the UniqueName
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return this.UniqueName;
		}
		#endregion

		#region Operator Overrides
        /// <summary>
        /// Override the == operator
        /// </summary>
        /// <param name="name1"></param>
        /// <param name="name2"></param>
        /// <returns></returns>
		public static bool operator ==( TestName name1, TestName name2 )
		{
			if ( Object.Equals( name1, null ) )
				return Object.Equals( name2, null );

			return name1.Equals( name2 );
		}

        /// <summary>
        /// Override the != operator
        /// </summary>
        /// <param name="name1"></param>
        /// <param name="name2"></param>
        /// <returns></returns>
		public static bool operator !=( TestName name1, TestName name2 )
		{
			return name1 == name2 ? false : true;
		}
		#endregion

		#region ICloneable Implementation
		/// <summary>
		/// Returns a duplicate of this TestName
		/// </summary>
		/// <returns></returns>
		public object Clone()
		{
			return this.MemberwiseClone();
		}
		#endregion
	}
}