private SortOrderInfo?GetSortOrderInfo(string currentSortExpression) { var sortExp = string.IsNullOrEmpty(currentSortExpression) ? this.DefaultOrdering : currentSortExpression; if (string.IsNullOrEmpty(sortExp)) { return(null); } var separatorIndex = sortExp.IndexOf('.'); if (separatorIndex >= 0) { sortExp = sortExp.Substring(separatorIndex + 1); } var result = new SortOrderInfo { Direction = OrderDirection.Asc, PropertyName = sortExp }; if (sortExp.EndsWith(" DESC")) { result.PropertyName = sortExp.Remove(sortExp.LastIndexOf(" DESC")); result.Direction = OrderDirection.Desc; } else if (sortExp.EndsWith(" ASC")) { result.PropertyName = sortExp.Remove(sortExp.LastIndexOf(" ASC")); result.Direction = OrderDirection.Asc; } return(result); }
public ContentComparer(SortOrderInfo?orderInfo) { _orderInfo = orderInfo.HasValue ? orderInfo.Value : new SortOrderInfo { Direction = OrderDirection.Asc, PropertyName = "Name" }; }
/// <summary> /// Verifies that the sequence of values are sorted in the specified order. /// </summary> /// <typeparam name="T">The type of value.</typeparam> /// <param name="values">The sequence of values to be tested.</param> /// <param name="sortOrder">The expected sort order.</param> /// <param name="comparer">A comparison function to be used to compare two elements of the sequence, or null to use a default one.</param> /// <param name="messageFormat">The custom assertion message format, or null if none.</param> /// <param name="messageArgs">The custom assertion message arguments, or null if none.</param> /// <exception cref="AssertionException">Thrown if the verification failed unless the current <see cref="AssertionContext.AssertionFailureBehavior" /> indicates otherwise.</exception> public static void Sorted <T>(IEnumerable <T> values, SortOrder sortOrder, Comparison <T> comparer, string messageFormat, params object[] messageArgs) { AssertionHelper.Verify(() => { bool first = true; var previous = default(T); var sortInfo = SortOrderInfo.FromSortOrder(sortOrder); int delta; int index = 0; if (comparer == null) { comparer = ComparisonSemantics.Default.Compare; } foreach (T value in values) { if (!first) { try { delta = comparer(value, previous); } catch (InvalidOperationException exception) { return(new AssertionFailureBuilder( "Expected the elements to be sorted in a specific order but no implicit ordering comparison can be found for the subject type.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Type", typeof(T)) .AddException(exception) .ToAssertionFailure()); } if (!sortInfo.VerifyDeltaSign(delta)) { return(new AssertionFailureBuilder( "Expected the elements to be sorted in a specific order but the sequence of values mismatches at one position at least.") .SetMessage(messageFormat, messageArgs) .AddRawLabeledValue("Expected Sort Order", sortInfo.Description) .AddRawLabeledValue("Sequence", values) .AddRawLabeledValue("Failing Position", index) .ToAssertionFailure()); } } previous = value; first = false; index++; } return(null); }); }