private ConstraintResult sameNumberOfElementsAndConstraints(Array current, int numberOfConstraints) { _beingMatched = new MatchingLength(current, current.Length); var result = _beingMatched.ApplyTo(numberOfConstraints); return(result); }
public override ConstraintResult ApplyTo <TActual>(TActual actual) { if (!_isTrue) { return(new ConstraintResult(this, actual, true)); } return(_linkedConstraint.ApplyTo <TActual>(actual)); }
public void ApplyTo_FirstItemDoesNotMatchConstraint_SubsequentConstraintsNotEvaluated() { Constraint failing = Substitute.For <Constraint>(), notEvaluated = Substitute.For <Constraint>(); var subject = new ConstrainedEnumerable(failing, notEvaluated); failing.ApplyTo(-1).Returns(new ConstraintResult(null, null, false)); subject.ApplyTo(new[] { -1, 2 }); notEvaluated.DidNotReceive().ApplyTo(Arg.Any <int>()); }
/// <summary> /// Applies the constraint to an actual value, returning a ConstraintResult. /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>A ConstraintResult</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { _beingMatched = new TypeRevealingConstraint(typeof(IEnumerable)); ConstraintResult result = _beingMatched.ApplyTo(actual); if (result.IsSuccess) { var collection = (IEnumerable)actual; // ReSharper disable PossibleMultipleEnumeration ushort count = calculateCount(collection); _beingMatched = new CountConstraint(_countConstraint, collection); // ReSharper restore PossibleMultipleEnumeration result = _beingMatched.ApplyTo(count); } return(result); }
public void apply_SecondItemDoesNotMatchConstraint_SubsequentConstraintsNotEvaluated() { Constraint passing = Substitute.For <Constraint>(), failing = Substitute.For <Constraint>(), notEvaluated = Substitute.For <Constraint>(); var subject = new ConstrainedEnumerable(failing, notEvaluated); passing.ApplyTo(1).Returns(new ConstraintResult(null, null, false)); failing.ApplyTo(-2).Returns(new ConstraintResult(null, null, false)); subject.ApplyTo(new[] { 1, -2, 3 }); notEvaluated.DidNotReceive().ApplyTo(Arg.Any <int>()); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for success, false for failure</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { if (actual is string) { StringConstraint constraint = new SubstringConstraint((string)_expected); if (_ignoreCase) { constraint = constraint.IgnoreCase; } _realConstraint = constraint; } else { _realConstraint = new CollectionContainsConstraint(_expected); } return(_realConstraint.ApplyTo(actual)); }
/// <summary> /// Applies the constraint to an actual value, returning a ConstraintResult. /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>A ConstraintResult</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { // it is ok to iterate the collection as most of the times it will be a small controlled collection // ReSharper disable AssignNullToNotNullAttribute var collection = (actual as IEnumerable).Cast <object>().ToArray(); // ReSharper restore AssignNullToNotNullAttribute ConstraintResult result = sameNumberOfElementsAndConstraints(collection, _constraints.Length); if (result.IsSuccess) { for (int i = 0; i < collection.Length && result.IsSuccess; i++) { _beingMatched = new IndexedConstraint(collection, i, _constraints[i]); result = _beingMatched.ApplyTo(collection.GetValue(i)); } } return(result); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for success, false for failure</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { // NOTE: actual is string will fail for a null typed as string if (typeof(TActual) == typeof(string)) { realConstraint = new EmptyStringConstraint(); } else if (actual == null) { throw new System.ArgumentException("The actual value must be a string or a non-null IEnumerable or DirectoryInfo", "actual"); } else if (actual is System.IO.DirectoryInfo) { realConstraint = new EmptyDirectoryConstraint(); } else { realConstraint = new EmptyCollectionConstraint(); } return(realConstraint.ApplyTo(actual)); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for success, false for failure</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { if (actual == null) { throw new System.ArgumentException("The actual value must be a non-null string, IEnumerable or DirectoryInfo", "actual"); } if (actual is string) { realConstraint = new EmptyStringConstraint(); } else if (actual is System.IO.DirectoryInfo) { realConstraint = new EmptyDirectoryConstraint(); } else { realConstraint = new EmptyCollectionConstraint(); } return(realConstraint.ApplyTo(actual)); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for success, false for failure</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { if (actual is string) { StringConstraint constraint = new SubstringConstraint((string)_expected); if (_ignoreCase) { constraint = constraint.IgnoreCase; } _realConstraint = constraint; } else { var itemConstraint = new EqualConstraint(_expected); if (_ignoreCase) { itemConstraint = itemConstraint.IgnoreCase; } _realConstraint = new SomeItemsConstraint(itemConstraint); } return(_realConstraint.ApplyTo(actual)); }
/// <summary> /// Test whether the constraint is satisfied by a given value /// </summary> /// <param name="actual">The value to be tested</param> /// <returns>True for success, false for failure</returns> protected override ConstraintResult ApplyConstraint <TActual>(TActual actual) { if (actual is string && ExpectedValue is string) { StringConstraint constraint = new SubstringConstraint(ExpectedValue as string); if (_ignoreCase) { constraint = constraint.IgnoreCase; } _realConstraint = constraint; } else { var itemConstraint = new EqualConstraint <TExpected>(ExpectedValue); if (_ignoreCase) { itemConstraint = itemConstraint.IgnoreCase; } _realConstraint = new SomeItemsConstraint(itemConstraint); } return(_realConstraint.ApplyTo(actual)); }
public override ConstraintResult ApplyTo <TActual>(TActual actual) { return(_composed.ApplyTo(actual)); }
/// <summary> /// Checks the success of a constraint. /// </summary> /// <param name="subject">Constraint to be tested.</param> /// <param name="actual">The action to be tested.</param> /// <typeparam name="T">Type of the tested value.</typeparam> /// <returns><code>true</code> if the constraint matched, <code>false</code> otherwise.</returns> protected bool matches <T>(Constraint subject, ActualValueDelegate <T> actual) { ConstraintResult result = subject.ApplyTo(actual); return(result.IsSuccess); }
/// <summary> /// Gets the error message of a failing contraint. /// </summary> /// <param name="subject">Constraint to be tested.</param> /// <param name="actual">The action to be tested.</param> /// <typeparam name="T">Type of the tested value.</typeparam> /// <returns>The message of a failing contraint or <see cref="string.Empty"/> if is not failing.</returns> protected string getMessage <T>(Constraint subject, ActualValueDelegate <T> actual) { string message = getMessage(subject.ApplyTo(actual)); return(message); }
/// <summary> /// Gets the error message of a failing contraint. /// </summary> /// <param name="subject">Constraint to be tested.</param> /// <param name="actual">The value to be tested.</param> /// <typeparam name="T">Type of the tested value.</typeparam> /// <returns>The message of a failing contraint or <see cref="string.Empty"/> if is not failing.</returns> protected string getMessage <T>(Constraint subject, T actual) { string message = getMessage(subject.ApplyTo(actual)); return(message); }