PathConstraint
PathConstraint serves as the abstract base of constraints
that operate on paths and provides several helper methods.
using NUnit.Framework.Internal;
using System;
using System.IO;
namespace NUnit.Framework.Constraints
{
public abstract class PathConstraint : StringConstraint
{
private const char WindowsDirectorySeparatorChar = '\\';
private const char NonWindowsDirectorySeparatorChar = '/';
private static readonly char[] DirectorySeparatorChars = new char[2] {
'\\',
'/'
};
public PathConstraint RespectCase {
get {
caseInsensitive = false;
return this;
}
}
protected PathConstraint(string expected)
: base(expected)
{
base.expected = expected;
caseInsensitive = (Path.DirectorySeparatorChar == '\\');
}
protected override string GetStringRepresentation()
{
return string.Format("<{0} \"{1}\" {2}>", DisplayName.ToLower(), expected, caseInsensitive ? "ignorecase" : "respectcase");
}
protected string Canonicalize(string path)
{
if (Path.DirectorySeparatorChar != Path.AltDirectorySeparatorChar)
path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string text = "";
string text2 = path;
char directorySeparatorChar;
foreach (char c in text2) {
if (c != '\\' && c != '/')
break;
string str = text;
directorySeparatorChar = Path.DirectorySeparatorChar;
text = str + directorySeparatorChar.ToString();
}
string[] array = path.Split(DirectorySeparatorChars, StringSplitOptions.RemoveEmptyEntries);
int num = 0;
bool flag = false;
string[] array2 = array;
foreach (string text3 in array2) {
if (!(text3 == ".") && (text3 == null || text3.Length != 0)) {
if (text3 == "..") {
flag = true;
if (num > 0)
num--;
} else {
if (flag)
array[num] = text3;
num++;
}
} else
flag = true;
}
string str2 = text;
directorySeparatorChar = Path.DirectorySeparatorChar;
return str2 + string.Join(directorySeparatorChar.ToString(), array, 0, num);
}
protected bool IsSubPath(string path1, string path2)
{
int length = path1.Length;
int length2 = path2.Length;
if (length >= length2)
return false;
if (!StringUtil.StringsEqual(path1, path2.Substring(0, length), caseInsensitive))
return false;
if (path2[length - 1] != Path.DirectorySeparatorChar)
return path2[length] == Path.DirectorySeparatorChar;
return true;
}
}
}