ValueGenerator<T>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
internal abstract class ValueGenerator<[System.Runtime.CompilerServices.Nullable(2)] T> : ValueGenerator
{
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class ComparableStep<[System.Runtime.CompilerServices.Nullable(0)] TStep> : Step where TStep : IComparable<TStep>
{
private readonly TStep _step;
private readonly Func<T, TStep, T> _apply;
public override bool IsPositive => Comparer<TStep>.Default.Compare(default(TStep), _step) < 0;
public override bool IsNegative => Comparer<TStep>.Default.Compare(_step, default(TStep)) < 0;
public ComparableStep(TStep value, Func<T, TStep, T> apply)
{
if (apply == null)
throw new ArgumentNullException("apply");
_step = value;
_apply = apply;
}
public override T Apply(T value)
{
return _apply(value, _step);
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
public new abstract class Step : ValueGenerator.Step
{
[System.Runtime.CompilerServices.NullableContext(1)]
public abstract T Apply(T value);
}
private static Exception CreateNotSupportedException(string description)
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(62, 2);
defaultInterpolatedStringHandler.AppendFormatted<Type>(typeof(T));
defaultInterpolatedStringHandler.AppendLiteral(" is using the default value generator which does not support ");
defaultInterpolatedStringHandler.AppendFormatted(description);
defaultInterpolatedStringHandler.AppendLiteral(".");
return new NotSupportedException(defaultInterpolatedStringHandler.ToStringAndClear());
}
public virtual int Compare(T x, T y)
{
if (!typeof(IComparable<T>).IsAssignableFrom(typeof(T)))
throw CreateNotSupportedException("comparisons");
return Comparer<T>.Default.Compare(x, y);
}
public IEnumerable<T> GenerateRange(T start, T end, [System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0
})] Step step)
{
int startToEnd = this.Compare(start, end);
if (startToEnd != 0) {
if ((startToEnd < 0 && !step.IsPositive) || (startToEnd > 0 && !step.IsNegative))
throw new ArgumentException("Step must be in the direction of the end.");
T current = start;
while (true) {
yield return current;
T val;
try {
val = step.Apply(current);
} catch (OverflowException) {
yield break;
}
if (startToEnd < 0) {
if (this.Compare(val, end) > 0)
yield break;
if (this.Compare(val, current) <= 0)
throw new InvalidOperationException("The step must strictly increase.");
} else {
if (this.Compare(val, end) < 0)
yield break;
if (this.Compare(val, current) >= 0)
break;
}
current = val;
}
throw new InvalidOperationException("The step must strictly decrease.");
}
yield return start;
}
public sealed override IEnumerable GenerateRange(object start, object end, ValueGenerator.Step step)
{
return GenerateRange((T)start, (T)end, (Step)step);
}
public sealed override ValueGenerator.Step CreateStep(object value)
{
if (TryCreateStep(value, out ValueGenerator.Step step))
return step;
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(24, 1);
defaultInterpolatedStringHandler.AppendLiteral("creating a step of type ");
defaultInterpolatedStringHandler.AppendFormatted<Type>(value.GetType());
throw CreateNotSupportedException(defaultInterpolatedStringHandler.ToStringAndClear());
}
public override bool TryCreateStep(object value, [System.Runtime.CompilerServices.Nullable(2)] [NotNullWhen(true)] out ValueGenerator.Step step)
{
Guard.ArgumentNotNull(value, "value");
step = null;
return false;
}
}
}