protected void AssertCorrectPageLoaded <T>(T target) where T : BasePage { var isDisplayed = target.IsUrlDisplayed(); if (!isDisplayed) { assert.Fail("Expected URL {0} but was {1}", target.PageUrl, Driver.Url); } }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given type which matches the given predicate. The /// collection may or may not contain other values which do not /// match the given predicate. /// </summary> /// <typeparam name="T">The collection type.</typeparam> /// <param name="collection">The collection.</param> /// <param name="predicate">The item matching predicate.</param> /// <returns>The single item in the filtered collection.</returns> /// <exception cref="SingleException">Thrown when the filtered collection does /// not contain exactly one element.</exception> public static T Single <T>(this IAssert assert, IEnumerable <T> collection, Predicate <T> predicate) { Guard.ArgumentNotNull("collection", collection); Guard.ArgumentNotNull("predicate", predicate); int count = 0; T result = default(T); foreach (T item in collection) { if (predicate(item)) { result = item; ++count; } } if (count != 1) { assert.Fail(new SingleException(count)); } assert.Okay(); return(result); }
/// <summary> /// Verifies that the provided object raised INotifyPropertyChanged.PropertyChanged /// as a result of executing the given test code. /// </summary> /// <param name="object">The object which should raise the notification</param> /// <param name="propertyName">The property name for which the notification should be raised</param> /// <param name="testCode">The test code which should cause the notification to be raised</param> /// <exception cref="PropertyChangedException">Thrown when the notification is not raised</exception> public static void PropertyChanged(this IAssert assert, INotifyPropertyChanged @object, string propertyName, Action testCode) { Guard.ArgumentNotNull("object", @object); Guard.ArgumentNotNull("testCode", testCode); bool propertyChangeHappened = false; PropertyChangedEventHandler handler = (sender, args) => { if (propertyName.Equals(args.PropertyName, StringComparison.OrdinalIgnoreCase)) { propertyChangeHappened = true; } }; @object.PropertyChanged += handler; try { testCode(); if (!propertyChangeHappened) { assert.Fail(new PropertyChangedException(propertyName)); } assert.Okay(); } finally { @object.PropertyChanged -= handler; } }
/// <summary> /// Verifies that a string contains a given sub-string, using the given comparison type. /// </summary> /// <param name="expectedSubstring">The sub-string expected to be in the string</param> /// <param name="actualString">The string to be inspected</param> /// <param name="comparisonType">The type of string comparison to perform</param> /// <exception cref="ContainsException">Thrown when the sub-string is not present inside the string</exception> public static void Contains(this IAssert assert, string expectedSubstring, string actualString, StringComparison comparisonType) { if (actualString == null || actualString.IndexOf(expectedSubstring, comparisonType) < 0) { assert.Fail(new ContainsException(expectedSubstring, actualString)); } assert.Okay(); }
/// <summary> /// Verifies that two objects are the same instance. /// </summary> /// <param name="expected">The expected object instance</param> /// <param name="actual">The actual object instance</param> /// <exception cref="SameException">Thrown when the objects are not the same instance</exception> public static void Same(this IAssert assert, object expected, object actual) { if (!object.ReferenceEquals(expected, actual)) { assert.Fail(new SameException(expected, actual)); } assert.Okay(); }
/// <summary> /// Verifies that a string does not contain a given sub-string, using the current culture. /// </summary> /// <param name="expectedSubstring">The sub-string which is expected not to be in the string</param> /// <param name="actualString">The string to be inspected</param> /// <param name="comparisonType">The type of string comparison to perform</param> /// <exception cref="DoesNotContainException">Thrown when the sub-string is present inside the given string</exception> public static void DoesNotContain(this IAssert assert, string expectedSubstring, string actualString, StringComparison comparisonType) { if (actualString != null && actualString.IndexOf(expectedSubstring, comparisonType) >= 0) { assert.Fail(new DoesNotContainException(expectedSubstring)); } assert.Okay(); }
/// <summary> /// Verifies that an object reference is null. /// </summary> /// <param name="object">The object to be inspected</param> /// <exception cref="NullException">Thrown when the object reference is not null</exception> public static void Null(this IAssert assert, object @object) { if (@object != null) { assert.Fail(new NullException(@object)); } assert.Okay(); }
/// <summary> /// Verifies that a value is not within a given range, using a comparer. /// </summary> /// <typeparam name="T">The type of the value to be compared</typeparam> /// <param name="actual">The actual value to be evaluated</param> /// <param name="low">The (inclusive) low value of the range</param> /// <param name="high">The (inclusive) high value of the range</param> /// <param name="comparer">The comparer used to evaluate the value's range</param> /// <exception cref="NotInRangeException">Thrown when the value is in the given range</exception> public static void NotInRange <T>(this IAssert assert, T actual, T low, T high, IComparer <T> comparer) { Guard.ArgumentNotNull("comparer", comparer); if (comparer.Compare(low, actual) <= 0 && comparer.Compare(actual, high) <= 0) { assert.Fail(new NotInRangeException(actual, low, high)); } assert.Okay(); }
/// <summary> /// Verifies that two objects are not equal, using a custom equality comparer. /// </summary> /// <typeparam name="T">The type of the objects to be compared</typeparam> /// <param name="expected">The expected object</param> /// <param name="actual">The actual object</param> /// <param name="comparer">The comparer used to examine the objects</param> /// <exception cref="NotEqualException">Thrown when the objects are equal</exception> public static void NotEqual <T>(this IAssert assert, T expected, T actual, IEqualityComparer <T> comparer) { Guard.ArgumentNotNull("comparer", comparer); if (comparer.Equals(expected, actual)) { assert.Fail(new NotEqualException()); } assert.Okay(); }
/// <summary> /// Verifies that an object is exactly the given type (and not a derived type). /// </summary> /// <param name="expectedType">The type the object should be</param> /// <param name="object">The object to be evaluated</param> /// <exception cref="IsTypeException">Thrown when the object is not the given type</exception> public static void IsType(this IAssert assert, Type expectedType, object @object) { Guard.ArgumentNotNull("expectedType", expectedType); if (@object == null || !expectedType.Equals(@object.GetType())) { assert.Fail(new IsTypeException(expectedType, @object)); } assert.Okay(); }
/// <summary> /// Verifies that a block of code does not throw any exceptions. /// </summary> /// <param name="testCode">A delegate to the code to be tested</param> public static void DoesNotThrow(this IAssert assert, Func <object> testCode) { Exception ex = Record.Exception(testCode); if (ex != null) { assert.Fail(new DoesNotThrowException(ex)); } assert.Okay(); }
/// <summary> /// Verifies that the exact exception is thrown (and not a derived exception type). /// Generally used to test property accessors. /// </summary> /// <param name="exceptionType">The type of the exception expected to be thrown</param> /// <param name="testCode">A delegate to the code to be tested</param> /// <returns>The exception that was thrown, when successful</returns> /// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception> public static Exception Throws(this IAssert assert, Type exceptionType, Func <object> testCode) { Guard.ArgumentNotNull("exceptionType", exceptionType); Exception exception = Record.Exception(testCode); if (exception == null) { assert.Fail(new ThrowsException(exceptionType)); } if (!exceptionType.Equals(exception.GetType())) { assert.Fail(new ThrowsException(exceptionType, exception)); } assert.Okay(); return(exception); }
public static void Empty(this IAssert assert, IEnumerable collection) { Guard.ArgumentNotNull("collection", collection); #pragma warning disable 168 foreach (object @object in collection) { assert.Fail(new EmptyException()); } #pragma warning restore 168 assert.Okay(); }
/// <summary> /// Verifies that two <see cref="decimal"/> values are equal, within the number of decimal /// places given by <paramref name="precision"/>. /// </summary> /// <param name="expected">The expected value</param> /// <param name="actual">The value to be compared against</param> /// <param name="precision">The number of decimal places (valid values: 0-15)</param> /// <exception cref="EqualException">Thrown when the values are not equal</exception> public static void Equal(this IAssert assert, decimal expected, decimal actual, int precision) { var expectedRounded = Math.Round(expected, precision); var actualRounded = Math.Round(actual, precision); if (!GetEqualityComparer <decimal>().Equals(expectedRounded, actualRounded)) { assert.Fail(new EqualException( String.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", expectedRounded, expected), String.Format(CultureInfo.CurrentCulture, "{0} (rounded from {1})", actualRounded, actual) )); } assert.Okay(); }
public static void That(this IAssert assert, ActualValueDelegate actual, IResolveConstraint expression, string message = null) { Constraint constraint = expression.Resolve(); if (constraint.Matches(actual)) { assert.Okay(); return; } using (MessageWriter writer = new TextMessageWriter(message)) { constraint.WriteMessageTo(writer); assert.Fail(writer.ToString(), ExcludeFromStack); } }
/// <summary> /// Verifies that a collection does not contain a given object, using an equality comparer. /// </summary> /// <typeparam name="T">The type of the object to be compared</typeparam> /// <param name="expected">The object that is expected not to be in the collection</param> /// <param name="collection">The collection to be inspected</param> /// <param name="comparer">The comparer used to equate objects in the collection with the expected object</param> /// <exception cref="DoesNotContainException">Thrown when the object is present inside the container</exception> public static void DoesNotContain <T>(this IAssert assert, T expected, IEnumerable <T> collection, IEqualityComparer <T> comparer) { Guard.ArgumentNotNull("comparer", comparer); if (collection != null) { foreach (T item in collection) { if (comparer.Equals(expected, item)) { assert.Fail(new DoesNotContainException(expected)); } } } assert.Okay(); }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given value. The collection may or may not /// contain other values. /// </summary> /// <param name="collection">The collection.</param> /// <param name="expected">The value to find in the collection.</param> /// <returns>The single item in the collection.</returns> /// <exception cref="SingleException">Thrown when the collection does not contain /// exactly one element.</exception> public static void Single(this IAssert assert, IEnumerable collection, object expected) { Guard.ArgumentNotNull("collection", collection); int count = 0; foreach (object item in collection) { if (Object.Equals(item, expected)) { ++count; } } if (count != 1) { assert.Fail(new SingleException(count, expected)); } assert.Okay(); }
/// <summary> /// Verifies that the given collection contains only a single /// element of the given type. /// </summary> /// <param name="collection">The collection.</param> /// <returns>The single item in the collection.</returns> /// <exception cref="SingleException">Thrown when the collection does not contain /// exactly one element.</exception> public static object Single(this IAssert assert, IEnumerable collection) { Guard.ArgumentNotNull("collection", collection); int count = 0; object result = null; foreach (object item in collection) { result = item; ++count; } if (count != 1) { assert.Fail(new SingleException(count)); } assert.Okay(); return(result); }
public static void Fail(string message, params object[] parameters) => _assert.Fail(message, parameters);
public static void Fail(string message) { assert.Fail(message); }
/// <summary> /// Verifies that two strings are equivalent. /// </summary> /// <param name="expected">The expected string value.</param> /// <param name="actual">The actual string value.</param> /// <param name="ignoreCase">If set to <c>true</c>, ignores cases differences. The invariant culture is used.</param> /// <param name="ignoreLineEndingDifferences">If set to <c>true</c>, treats \r\n, \r, and \n as equivalent.</param> /// <param name="ignoreWhiteSpaceDifferences">If set to <c>true</c>, treats spaces and tabs (in any non-zero quantity) as equivalent.</param> /// <exception cref="EqualException">Thrown when the strings are not equivalent.</exception> public static void Equal(this IAssert assert, string expected, string actual, bool ignoreCase = false, bool ignoreLineEndingDifferences = false, bool ignoreWhiteSpaceDifferences = false) { // Start out assuming the one of the values is null int expectedIndex = -1; int actualIndex = -1; int expectedLength = 0; int actualLength = 0; if (expected == null) { if (actual == null) { return; } } else if (actual != null) { // Walk the string, keeping separate indices since we can skip variable amounts of // data based on ignoreLineEndingDifferences and ignoreWhiteSpaceDifferences. expectedIndex = 0; actualIndex = 0; expectedLength = expected.Length; actualLength = actual.Length; while (expectedIndex < expectedLength && actualIndex < actualLength) { char expectedChar = expected[expectedIndex]; char actualChar = actual[actualIndex]; if (ignoreLineEndingDifferences && IsLineEnding(expectedChar) && IsLineEnding(actualChar)) { expectedIndex = SkipLineEnding(expected, expectedIndex); actualIndex = SkipLineEnding(actual, actualIndex); } else if (ignoreWhiteSpaceDifferences && IsWhiteSpace(expectedChar) && IsWhiteSpace(actualChar)) { expectedIndex = SkipWhitespace(expected, expectedIndex); actualIndex = SkipWhitespace(actual, actualIndex); } else { if (ignoreCase) { expectedChar = Char.ToUpperInvariant(expectedChar); actualChar = Char.ToUpperInvariant(actualChar); } if (expectedChar != actualChar) { break; } expectedIndex++; actualIndex++; } } } if (expectedIndex < expectedLength || actualIndex < actualLength) { assert.Fail(new EqualException(expected, actual, expectedIndex, actualIndex)); } assert.Okay(); }