// ****************************************************************
// 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
{
///
/// 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.
///
[Serializable]
public class TestName : ICloneable
{
#region Fields
///
/// ID that uniquely identifies the test
///
private TestID testID;
private int runnerID;
///
/// The simple name of the test, without qualification
///
private string name;
///
/// The fully qualified name of the test
///
private string fullName;
#endregion
#region Properties
///
/// Gets or sets the TestID that uniquely identifies this test
///
public TestID TestID
{
get { return testID; }
set { testID = value; }
}
///
/// Gets the ID for the runner that created the test from
/// the TestID, or returns -1 if the TestID is null.
///
public int RunnerID
{
get { return runnerID; }
set { runnerID = value; }
}
///
/// Gets or sets the simple name of the test
///
public string Name
{
get { return name; }
set { name = value; }
}
///
/// Gets or sets the full (qualified) name of the test
///
public string FullName
{
get { return fullName; }
set { fullName = value; }
}
///
/// Get the string representation of this test name, incorporating all
/// the components of the name.
///
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
///
/// Parse a string representation of a TestName,
/// returning a TestName.
///
/// The string to parse
/// A TestName
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
///
/// Compares two TestNames for equality
///
/// the other TestID
/// True if the two TestIDs are equal
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;
}
///
/// Calculates a hashcode for this TestID
///
/// The hash code.
public override int GetHashCode()
{
return unchecked( this.testID.GetHashCode() + this.fullName.GetHashCode() );
}
///
/// Override ToString() to display the UniqueName
///
///
public override string ToString()
{
return this.UniqueName;
}
#endregion
#region Operator Overrides
///
/// Override the == operator
///
///
///
///
public static bool operator ==( TestName name1, TestName name2 )
{
if ( Object.Equals( name1, null ) )
return Object.Equals( name2, null );
return name1.Equals( name2 );
}
///
/// Override the != operator
///
///
///
///
public static bool operator !=( TestName name1, TestName name2 )
{
return name1 == name2 ? false : true;
}
#endregion
#region ICloneable Implementation
///
/// Returns a duplicate of this TestName
///
///
public object Clone()
{
return this.MemberwiseClone();
}
#endregion
}
}