/// <summary> /// Ensures that a sequence ends with a particular set of values based on a specific comparer. /// </summary> public static void EndsWith <TValue>(IEnumerable <TValue> pSequence, IEnumerable <TValue> pEnd, IEqualityComparer <TValue> pComparer, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pEnd == null) { throw new ArgumentNullException("pEnd"); } if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (!pEnd.Any()) { throw new ArgumentException("Expected elements", "pEnd"); } if (pEnd.Count() > pSequence.Count()) { throw new ArgumentException("pEnd is larger than pSequence", "pEnd"); } pSequence = pSequence.Reverse(); pEnd = pEnd.Reverse(); var seqIter = pSequence.GetEnumerator(); var endIter = pEnd.GetEnumerator(); while (seqIter.MoveNext() && endIter.MoveNext()) { if (!IsEqual(seqIter.Current, endIter.Current, pComparer)) { throw pException ?? new AssertionException("Expected pSequence to end with the elements of pEnd"); } } }
/// <summary> /// Ensures that a sequence starts with a particular set of values based on a specific comparer. /// </summary> public static void StartsWith <TValue>(IEnumerable <TValue> pSequence, IEnumerable <TValue> pStart, IEqualityComparer <TValue> pComparer, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pStart == null) { throw new ArgumentNullException("pStart"); } if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (!pStart.Any()) { throw new ArgumentException("Expected elements", "pStart"); } if (pStart.Count() > pSequence.Count()) { throw new ArgumentException("pStart is larger than pSequence", "pStart"); } var seqIter = pSequence.GetEnumerator(); var startIter = pStart.GetEnumerator(); while (seqIter.MoveNext() && startIter.MoveNext()) { if (!IsEqual(seqIter.Current, startIter.Current, pComparer)) { throw pException ?? new AssertionException("Expected pSequence to begin with the elements of pStart"); } } }
/// <summary> /// Asserts that one or more elements of a sequence with one or more elements matches the predicate. /// </summary> public static void Any <TValue>(IEnumerable <TValue> pSequence, Func <TValue, bool> pPredicate, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pPredicate == null) { throw new ArgumentNullException("pPredicate"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (!pSequence.Any(pPredicate)) { throw pException ?? new AssertionException("Expected at least 1 element to match predicate"); } }
/// <summary> /// Asserts that an entire sequence with two or more elements has at least one duplicate pair that matches the predicate. /// </summary> public static void NotUnique <TValue, TCompare>(IEnumerable <TValue> pSequence, Func <TValue, TCompare> pPredicate, AssertionException pException = null) where TCompare : IEquatable <TCompare> { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pPredicate == null) { throw new ArgumentNullException("pPredicate"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (pSequence.Count() == 1) { throw new ArgumentException("Sequence cannot be assessed for duplicates if there is only a single element"); } if (pSequence.Distinct(new EqualityComparerStateless <TValue, TCompare>(pPredicate)).Count() == pSequence.Count()) { throw pException ?? new AssertionException("Expected at least one duplicate pair of elements based on the predicate"); } }
/// <summary> /// Asserts that an entire sequence with two or more elements has at least one duplicate pair based on a specific comparer. /// </summary> public static void NotUnique <TValue>(IEnumerable <TValue> pSequence, IEqualityComparer <TValue> pComparer, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (pSequence.Count() == 1) { throw new ArgumentException("Sequence cannot be assessed for duplicates if there is only a single element"); } if (pSequence.Distinct(pComparer).Count() == pSequence.Count()) { throw pException ?? new AssertionException("Expected at least one duplicate pair of elements"); } }
/// <summary> /// Asserts that a sequence does not contain a specific value with a specific comparer. /// </summary> public static void DoesNotContain <TValue>(IEnumerable <TValue> pSequence, TValue pValue, IEqualityComparer <TValue> pComparer, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (pSequence.Contains(pValue, pComparer)) { throw pException ?? new AssertionException("Expected value is present"); } }
/// <summary> /// Asserts that an entire sequence with two or more elements has no duplicates that match the predicate. /// </summary> public static void Unique <TValue>(IEnumerable <TValue> pSequence, Func <TValue, TValue, bool> pPredicate, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pPredicate == null) { throw new ArgumentNullException("pPredicate"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (pSequence.Count() == 1) { throw new ArgumentException("Sequence cannot be assessed for duplicates if there is only a single element"); } if (pSequence.Distinct(new EqualityComparerStateful <TValue>(pPredicate)).Count() != pSequence.Count()) { throw pException ?? new AssertionException("Expected all elements to be unique based on the predicate"); } }
/// <summary> /// Asserts that a sequence has a specified number of a specific element with a specific comparer. /// </summary> public static void Count <TValue>(IEnumerable <TValue> pSequence, int pCount, TValue pValue, IEqualityComparer <TValue> pComparer, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (pCount < 0) { throw new ArgumentException("Negative pCount values are not valid"); } if (pSequence.Count(e => IsEqual(e, pValue, pComparer)) != pCount) { throw pException ?? new AssertionException("Expected {0} elements in sequence", pCount); } }
/// <summary> /// Asserts that a sequence contains a specific value. /// </summary> public static void Contains <TValue>(IEnumerable <TValue> pSequence, TValue pValue, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (!pSequence.Contains(pValue)) { throw pException ?? new AssertionException("Expected value is missing"); } }
/// <summary> /// Asserts that the specified value compares either greater or equal to a specified lower bound. /// </summary> public static void GreaterThanEqual <TValue>(TValue pValue, TValue pLowerBound, AssertionException pException = null) where TValue : IComparable <TValue> { if (pLowerBound == null) { throw new ArgumentNullException("Lower bound must be populated to constrain a range"); } if (pValue == null) { throw new ArgumentNullException("Value must be populated to be in a range"); } if (pLowerBound.CompareTo(pValue) > 0) { throw pException ?? new AssertionException("Value fell below expected range: {0}", pValue); } }
/// <summary> /// Asserts that exactly a specified number of elements of a sequence with one or more elements matches the predicate. /// </summary> public static void Exactly <TValue>(IEnumerable <TValue> pSequence, int pCount, Func <TValue, bool> pPredicate, AssertionException pException = null) { if (pSequence == null) { throw new ArgumentNullException("pSequence"); } if (pPredicate == null) { throw new ArgumentNullException("pPredicate"); } if (!pSequence.Any()) { throw new ArgumentException("Expected elements", "pSequence"); } if (pCount < 0) { throw new ArgumentException("Negative pCount values are not valid"); } if (pSequence.Count(pPredicate) != pCount) { throw pException ?? new AssertionException("Expected {0} elements to match predicate", pCount); } }
/// <summary> /// Asserts that two specified objects are not equal given a specific comparer. /// </summary> public static void NotEqual(object pLeft, object pRight, IEqualityComparer pComparer, AssertionException pException = null) { if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (IsEqual(pLeft, pRight, pComparer)) { throw pException ?? new AssertionException("Expected unequal values"); } }
/// <summary> /// Asserts that two specified objects are equal given a specific comparer. /// </summary> public static void Equal <TValue>(TValue pLeft, TValue pRight, IEqualityComparer <TValue> pComparer, AssertionException pException = null) { if (pComparer == null) { throw new ArgumentNullException("pComparer"); } if (!IsEqual(pLeft, pRight, pComparer)) { throw pException ?? new AssertionException("Expected equal values"); } }
/// <summary> /// Asserts that the specified value compares strictly greater to a specified upper bound AND strictly less to a specified lower bound. /// </summary> public static void NotInRange <TValue>(TValue pValue, TValue pLowerBound, TValue pUpperBound, AssertionException pException = null) where TValue : IComparable <TValue> { if (pLowerBound == null || pUpperBound == null) { throw new ArgumentNullException("Lower bound and upper bound must be populated to constrain a range"); } if (pValue == null) { throw new ArgumentNullException("Value must be populated to be in a range"); } if (pLowerBound.CompareTo(pValue) <= 0 && pUpperBound.CompareTo(pValue) >= 0) { throw pException ?? new AssertionException("Value fell into range"); } }
/// <summary> /// Asserts that a floating-point value is within an delta of another floating point value. /// </summary> public static void IsWithinDelta(double pValue1, double pValue2, double pDelta, AssertionException pException = null) { if (Math.Abs(pValue1 - pValue2) > pDelta) { throw pException ?? new AssertionException("pValue was a floating point value"); } }
/// <summary> /// Asserts that a string produces the specified count of matches to the regex pattern. /// </summary> public static void MatchCount(string pRegexPattern, string pInput, int pCount, AssertionException pException = null) { if (pRegexPattern == null) { throw new ArgumentNullException("pRegexPattern"); } if (pInput == null) { throw new ArgumentNullException("pInput"); } if (pCount < 0) { throw new ArgumentException("Negative pCount values are not valid"); } if (Regex.Matches(pInput, pRegexPattern).Count != pCount) { throw pException ?? new AssertionException("Regex pattern was not matched the specified number of times: {0}", pCount); } }