RangeAttribute
Supplies a range of values to an individual parameter of a parameterized test.
using NUnit.Framework.Constraints;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
namespace NUnit.Framework
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = true, Inherited = false)]
public class RangeAttribute : NUnitAttribute, IParameterDataSource
{
private readonly object _from;
private readonly object _to;
private readonly object _step;
public RangeAttribute(int from, int to)
: this(from, to, (from <= to) ? 1 : (-1))
{
}
public RangeAttribute(int from, int to, int step)
{
Guard.ArgumentValid((step > 0 && to >= from) || (step < 0 && to <= from), "Step must be positive with to >= from or negative with to <= from", "step");
_from = from;
_to = to;
_step = step;
}
[CLSCompliant(false)]
public RangeAttribute(uint from, uint to)
: this(from, to, 1)
{
}
[CLSCompliant(false)]
public RangeAttribute(uint from, uint to, uint step)
{
Guard.ArgumentValid(step != 0, "Step must be greater than zero", "step");
Guard.ArgumentValid(to >= from, "Value of to must be greater than or equal to from", "to");
_from = from;
_to = to;
_step = step;
}
public RangeAttribute(long from, long to)
: this(from, to, (from > to) ? (-1) : 1)
{
}
public RangeAttribute(long from, long to, long step)
{
Guard.ArgumentValid((step > 0 && to >= from) || (step < 0 && to <= from), "Step must be positive with to >= from or negative with to <= from", "step");
_from = from;
_to = to;
_step = step;
}
[CLSCompliant(false)]
public RangeAttribute(ulong from, ulong to)
: this(from, to, 1)
{
}
[CLSCompliant(false)]
public RangeAttribute(ulong from, ulong to, ulong step)
{
Guard.ArgumentValid(step != 0, "Step must be greater than zero", "step");
Guard.ArgumentValid(to >= from, "Value of to must be greater than or equal to from", "to");
_from = from;
_to = to;
_step = step;
}
public RangeAttribute(double from, double to, double step)
{
Guard.ArgumentValid((step > 0 && to >= from) || (step < 0 && to <= from), "Step must be positive with to >= from or negative with to <= from", "step");
_from = from;
_to = to;
_step = step;
}
public RangeAttribute(float from, float to, float step)
{
Guard.ArgumentValid((step > 0 && to >= from) || (step < 0 && to <= from), "Step must be positive with to >= from or negative with to <= from", "step");
_from = from;
_to = to;
_step = step;
}
public IEnumerable GetData(IParameterInfo parameter)
{
object start = ParamAttributeTypeConversions.Convert(_from, parameter.ParameterType);
object end = ParamAttributeTypeConversions.Convert(_to, parameter.ParameterType);
ValueGenerator valueGenerator = ValueGenerator.Create(parameter.ParameterType);
if (!valueGenerator.TryCreateStep(_step, out ValueGenerator.Step step)) {
if (!ParamAttributeTypeConversions.TryConvert(_step, parameter.ParameterType, out object convertedValue))
convertedValue = _step;
step = valueGenerator.CreateStep(convertedValue);
}
return valueGenerator.GenerateRange(start, end, step);
}
public override string ToString()
{
DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(8, 3);
defaultInterpolatedStringHandler.AppendFormatted(MsgUtils.FormatValue(_from));
defaultInterpolatedStringHandler.AppendLiteral(" .. ");
defaultInterpolatedStringHandler.AppendFormatted(MsgUtils.FormatValue(_step));
defaultInterpolatedStringHandler.AppendLiteral(" .. ");
defaultInterpolatedStringHandler.AppendFormatted(MsgUtils.FormatValue(_to));
return defaultInterpolatedStringHandler.ToStringAndClear();
}
}
}