EmptyConstraint
EmptyConstraint tests a whether a string or collection is empty,
postponing the decision about which test is applied until the
type of the actual argument is known.
using System;
using System.Collections;
using System.IO;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Constraints
{
[NullableContext(1)]
[Nullable(0)]
public class EmptyConstraint : Constraint
{
[Nullable(2)]
private Constraint _realConstraint;
public override string Description {
get {
if (_realConstraint != null)
return _realConstraint.Description;
return "<empty>";
}
}
public override ConstraintResult ApplyTo<[Nullable(2)] TActual>(TActual actual)
{
ref TActual reference = ref actual;
TActual val = default(TActual);
object obj;
if (val == null) {
val = reference;
ref reference = ref val;
if (val == null) {
obj = null;
goto IL_0031;
}
}
obj = reference.GetType();
goto IL_0031;
IL_0031:
if (obj == null)
obj = typeof(TActual);
Type type = (Type)obj;
if (type == typeof(string))
_realConstraint = new EmptyStringConstraint();
else if (((object)actual) is Guid || type == typeof(Guid?)) {
_realConstraint = new EmptyGuidConstraint();
} else if (((object)actual) is DirectoryInfo) {
_realConstraint = new EmptyDirectoryConstraint();
} else if (((object)actual) is ICollection) {
_realConstraint = new EmptyCollectionConstraint();
} else if (((object)actual) is IEnumerable) {
_realConstraint = new EmptyCollectionConstraint();
} else {
if (actual == null || !CountZeroConstraint.HasCountProperty(type)) {
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(139, 1);
defaultInterpolatedStringHandler.AppendLiteral("The actual value must be not-null, a string, Guid, have an int Count property, IEnumerable or DirectoryInfo. The value passed was of type ");
defaultInterpolatedStringHandler.AppendFormatted(type);
defaultInterpolatedStringHandler.AppendLiteral(".");
throw new ArgumentException(defaultInterpolatedStringHandler.ToStringAndClear(), "actual");
}
_realConstraint = new CountZeroConstraint();
}
return _realConstraint.ApplyTo(actual);
}
public EmptyConstraint()
: base(Array.Empty<object>())
{
}
}
}