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;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
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)
{
caseInsensitive = (Path.DirectorySeparatorChar == '\\');
}
protected override string GetStringRepresentation()
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(6, 3);
defaultInterpolatedStringHandler.AppendLiteral("<");
defaultInterpolatedStringHandler.AppendFormatted(DisplayName.ToLower());
defaultInterpolatedStringHandler.AppendLiteral(" \"");
defaultInterpolatedStringHandler.AppendFormatted(expected);
defaultInterpolatedStringHandler.AppendLiteral("\" ");
defaultInterpolatedStringHandler.AppendFormatted(caseInsensitive ? "ignorecase" : "respectcase");
defaultInterpolatedStringHandler.AppendLiteral(">");
return defaultInterpolatedStringHandler.ToStringAndClear();
}
protected string Canonicalize(string path)
{
if (Path.DirectorySeparatorChar != Path.AltDirectorySeparatorChar)
path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
string str = string.Empty;
string text = path;
foreach (char c in text) {
if (c != '\\' && c != '/')
break;
str += Path.DirectorySeparatorChar.ToString();
}
string[] array = path.Split(DirectorySeparatorChars, StringSplitOptions.RemoveEmptyEntries);
int num = 0;
bool flag = false;
string[] array2 = array;
foreach (string text2 in array2) {
if (!(text2 == ".") && (text2 == null || text2.Length != 0)) {
if (text2 == "..") {
flag = true;
if (num > 0)
num--;
} else {
if (flag)
array[num] = text2;
num++;
}
} else
flag = true;
}
return str + string.Join(Path.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;
}
}
}