<PackageReference Include="NUnit" Version="3.0.1" />

CollectionOrderedConstraint

CollectionOrderedConstraint is used to test whether a collection is ordered.
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace NUnit.Framework.Constraints { public class CollectionOrderedConstraint : CollectionConstraint { private ComparisonAdapter comparer = ComparisonAdapter.Default; private string comparerName; private string propertyName; private bool descending; public CollectionOrderedConstraint Descending { get { descending = true; return this; } } public override string Description { get { string text = (propertyName == null) ? "collection ordered" : ("collection ordered by " + MsgUtils.FormatValue(propertyName)); if (descending) text += ", descending"; return text; } } public CollectionOrderedConstraint() { base.DisplayName = "Ordered"; } public CollectionOrderedConstraint Using(IComparer comparer) { this.comparer = ComparisonAdapter.For(comparer); comparerName = comparer.GetType().FullName; return this; } public CollectionOrderedConstraint Using<T>(IComparer<T> comparer) { this.comparer = ComparisonAdapter.For(comparer); comparerName = comparer.GetType().FullName; return this; } public CollectionOrderedConstraint Using<T>(Comparison<T> comparer) { this.comparer = ComparisonAdapter.For(comparer); comparerName = comparer.GetType().FullName; return this; } public CollectionOrderedConstraint By(string propertyName) { this.propertyName = propertyName; return this; } protected override bool Matches(IEnumerable actual) { object obj = null; int num = 0; foreach (object item in actual) { object obj2 = item; if (item == null) throw new ArgumentNullException("actual", "Null value at index " + num.ToString()); if (propertyName != null) { obj2 = item.GetType().GetProperty(propertyName).GetValue(item, null); if (obj2 == null) throw new ArgumentNullException("actual", "Null property value at index " + num.ToString()); } if (obj != null) { int num2 = comparer.Compare(obj, obj2); if (descending && num2 < 0) return false; if (!descending && num2 > 0) return false; } obj = obj2; num++; } return true; } protected override string GetStringRepresentation() { StringBuilder stringBuilder = new StringBuilder("<ordered"); if (propertyName != null) stringBuilder.Append("by " + propertyName); if (descending) stringBuilder.Append(" descending"); if (comparerName != null) stringBuilder.Append(" " + comparerName); stringBuilder.Append(">"); return stringBuilder.ToString(); } } }