示例#1
0
        public void When_no_exception_should_be_thrown_after_wait_time_the_func_result_should_be_returned()
        {
            // Arrange
            var clock        = new FakeClock();
            var timer        = clock.StartTimer();
            var waitTime     = 100.Milliseconds();
            var pollInterval = 10.Milliseconds();

            Func <int> throwShorterThanWaitTime = () =>
            {
                if (timer.Elapsed <= waitTime.Divide(2))
                {
                    throw new ArgumentException("An exception was forced");
                }

                return(42);
            };

            // Act
            AndWhichConstraint <FunctionAssertions <int>, int> act =
                throwShorterThanWaitTime.Should(clock).NotThrowAfter(waitTime, pollInterval);

            // Assert
            act.Subject.Should().Be(42);
        }
示例#2
0
        public void When_no_exception_should_be_thrown_after_wait_time_the_func_result_should_be_returned()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var watch        = Stopwatch.StartNew();
            var waitTime     = 100.Milliseconds();
            var pollInterval = 10.Milliseconds();

            Func <int> throwShorterThanWaitTime = () =>
            {
                if (watch.Elapsed <= (waitTime.Milliseconds / 2).Milliseconds())
                {
                    throw new ArgumentException("An exception was forced");
                }

                return(42);
            };

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            AndWhichConstraint <FunctionAssertions <int>, int> act =
                throwShorterThanWaitTime.Should().NotThrowAfter(waitTime, pollInterval);

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.Subject.Should().Be(42);
        }
        WithValueAt <TAssertion, TValue>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, int index, string because = "", params object[] reasonArgs)
            where TValue : IAnalysisValue
        {
            var testInfo = andWhichConstraint.Which.Should().HaveValueAt <TValue>(index, because, reasonArgs).Which;

            return(new AndWhichConstraint <TAssertion, AnalysisValueTestInfo <TValue> >(andWhichConstraint.And, testInfo));
        }
示例#4
0
        internal static void ContainMappingsFor(this AndWhichConstraint <ObjectAssertions, IEntityMapping> assertion, Type type)
        {
            var entityMapping = assertion.Subject;
            var allTypes = new[] { type }.Union(type.GetInterfaces());

            entityMapping.ContainClassesFor(allTypes);
            entityMapping.ContainPropertiesFor(allTypes);
        }
示例#5
0
        public static AndWhichConstraint <TAssertions, TMember> OfMemberType <TMember, TAssertions>
            (this AndWhichConstraint <TAssertions, TMember> constraint, PythonMemberType memberType,
            string because = "", params object[] reasonArgs)
            where TMember : IPythonType
        {
            Execute.Assertion.ForCondition(constraint.Which.MemberType == memberType)
            .BecauseOf(because, reasonArgs)
            .FailWith($"Expected {AssertionsUtilities.GetQuotedName(constraint.Which)} to have type '{memberType}', but found '{constraint.Which.MemberType}'");

            return(new AndWhichConstraint <TAssertions, TMember>(constraint.And, constraint.Which));
        }
        /// <summary>
        /// Asserts that the last recorded message matches the <paramref name="predicate"/>
        /// </summary>
        public static AndWhichConstraint <ReactiveAssertions <TPayload>, IEnumerable <TPayload> > WithLastMessage <TPayload>(
            this AndWhichConstraint <ReactiveAssertions <TPayload>, IEnumerable <TPayload> > recorderConstraint, Expression <Func <TPayload, bool> > predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            bool match = predicate.Compile().Invoke(recorderConstraint.GetLastMessage());

            Execute.Assertion
            .ForCondition(match)
            .FailWith("Expected the last message from {0} to match {1}, but it did not.", recorderConstraint.And.Subject, predicate.Body);

            return(recorderConstraint);
        }
        public void When_many_objects_are_provided_accessing_which_should_throw_a_descriptive_exception()
        {
            // Arrange
            var continuation = new AndWhichConstraint <StringCollectionAssertions, string>(null, new[] { "hello", "world" });

            // Act
            Action act = () =>
            {
                _ = continuation.Which;
            };

            // Assert
            act.Should().Throw <XunitException>()
            .WithMessage(
                "More than one object found.  FluentAssertions cannot determine which object is meant.*")
            .WithMessage("*Found objects:*\"hello\"*\"world\"");
        }
        /// <summary>
        /// Asserts that the recorded messages contain at lease one item which matches the <paramref name="predicate"/>
        /// </summary>
        public static AndWhichConstraint <ReactiveAssertions <TPayload>, IEnumerable <TPayload> > WithMessage <TPayload>(
            this AndWhichConstraint <ReactiveAssertions <TPayload>, IEnumerable <TPayload> > recorderConstraint, Expression <Func <TPayload, bool> > predicate)
        {
            if (predicate is null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            var  compiledPredicate = predicate.Compile();
            bool match             = recorderConstraint.Subject.Any(compiledPredicate);

            Execute.Assertion
            .ForCondition(match)
            .FailWith("Expected at least one message from {0} to match {1}, but found none.", recorderConstraint.And.Subject, predicate.Body);

            return(recorderConstraint);
        }
        public void When_many_objects_are_provided_accesing_which_should_throw_a_descriptive_exception()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var continuation = new AndWhichConstraint<StringCollectionAssertions, string>(null, new[] {"hello", "world"});

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            Action act = () =>
            {
                var item = continuation.Which;
            };

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            act.ShouldThrow<AssertFailedException>()
                .WithMessage(
                    "More than one object found.  FluentAssertions cannot determine which object is meant.*")
                .WithMessage("*Found objects:\r\n\t\"hello\"\r\n\t\"world\"");
        }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> OfResolvedType <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, string className, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Which.Should().HaveResolvedClassName(className, because, reasonArgs);
     return(andWhichConstraint);
 }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> OfTypes <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, params string[] classNames)
 => andWhichConstraint.OfTypes(classNames, string.Empty);
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> OfTypes <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, IEnumerable <string> classNames, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Which.Should().HaveClassNames(classNames, because, reasonArgs);
     return(andWhichConstraint);
 }
 public static AndWhichConstraint <TAssertion, IVariable> WithNoTypes <TAssertion>(
     this AndWhichConstraint <TAssertion, IVariable> andWhichConstraint, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Subject.Value.Should().HaveNoType(because, reasonArgs);
     return(andWhichConstraint);
 }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithShortDescriptions <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, params string[] descriptions)
 {
     andWhichConstraint.Which.Should().HaveShortDescriptions(descriptions);
     return(andWhichConstraint);
 }
 protected abstract void ValidateArgumentValue(AndWhichConstraint <ObjectAssertions, TArgumentValue> argumentValueAssertions);
        public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithVariable <TAssertion, TScope>(this AndWhichConstraint <TAssertion, TScope> constraint, string functionName, string because = "", params object[] reasonArgs)
            where TScope : IScope
        {
            var functionScope = constraint.Which.Should().HaveVariable(functionName, because, reasonArgs).Which;

            return(new AndWhichConstraint <TAssertion, VariableDefTestInfo>(constraint.And, functionScope));
        }
        public static AndWhichConstraint <TAssertion, AnalysisValueTestInfo <TAnalysisValue> > WithMemberOfType <TAssertion, TAnalysisValue>(this AndWhichConstraint <TAssertion, AnalysisValueTestInfo <TAnalysisValue> > constraint, string name, PythonMemberType memberType)
            where TAnalysisValue : IAnalysisValue
        {
            var member = new AnalysisValueAssertions <TAnalysisValue>(constraint.Which).HaveMember <IAnalysisValue>(name).Which;

            new AnalysisValueAssertions <IAnalysisValue>(member).HaveMemberType(memberType);
            return(constraint);
        }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithShortDescriptions <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, IEnumerable <string> descriptions, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Which.Should().HaveShortDescriptions(descriptions, because, reasonArgs);
     return(andWhichConstraint);
 }
示例#19
0
 protected override void ValidateArgumentValue(AndWhichConstraint <ObjectAssertions, IEnumArgumentValue <CustomerStatus> > argumentValueAssertions)
 {
     argumentValueAssertions.Which.Value.Should().Be(ArgumentValue);
     argumentValueAssertions.Which.Should().BeAssignableTo <IEnumArgumentValue>()
     .Which.Value.Should().Be(ArgumentValue);
 }
 public static AndWhichConstraint <TAssertions, OverloadResultTestInfo> WithSingleReturnType <TAssertions>(this AndWhichConstraint <TAssertions, OverloadResultTestInfo> constraint, string type, string because = "", params object[] reasonArgs)
 {
     constraint.Which.Should().HaveSingleReturnType(type, because, reasonArgs);
     return(constraint);
 }
 WithValueAt <TValue>(this AndWhichConstraint <ModuleAnalysisAssertions, VariableDefTestInfo> constraint, int index, string because = "", params object[] reasonArgs)
     where TValue : IAnalysisValue
 => constraint.WithValueAt <ModuleAnalysisAssertions, TValue>(index, because, reasonArgs);
 public static AndWhichConstraint <TAssertion, AnalysisValueTestInfo <TAnalysisValue> > OfPythonMemberType <TAssertion, TAnalysisValue>(this AndWhichConstraint <TAssertion, AnalysisValueTestInfo <TAnalysisValue> > constraint, PythonMemberType memberType)
     where TAnalysisValue : IAnalysisValue
 {
     new AnalysisValueAssertions <TAnalysisValue>(constraint.Which).HaveMemberType(memberType);
     return(constraint);
 }
 WithValue <TValue>(this AndWhichConstraint <FunctionScopeAssertions, VariableDefTestInfo> constraint, string because = "", params object[] reasonArgs)
     where TValue : IAnalysisValue
 => constraint.WithValue <FunctionScopeAssertions, TValue>(because, reasonArgs);
 WithMethodResolutionOrder <TAssertion>(this AndWhichConstraint <TAssertion, AnalysisValueTestInfo <IClassInfo> > constraint, params string[] classNames)
 {
     constraint.Which.Should().HaveMethodResolutionOrder(classNames);
     return(constraint);
 }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithNoTypes <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Which.Should().HaveNoTypes(because, reasonArgs);
     return(andWhichConstraint);
 }
        public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithParameter <TAssertion, TScope>(this AndWhichConstraint <TAssertion, TScope> constraint, string parameterName, string because = "", params object[] reasonArgs)
            where TScope : IFunctionScope
        {
            var variableDefinition = constraint.Which.Should().HaveParameter(parameterName, because, reasonArgs).Which;

            return(new AndWhichConstraint <TAssertion, VariableDefTestInfo>(constraint.And, variableDefinition));
        }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> OfResolvedTypes <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, params BuiltinTypeId[] typeIds)
 => andWhichConstraint.OfResolvedTypes(typeIds, string.Empty);
 public static AndWhichConstraint <TAssertion, IVariable> OfType <TAssertion>(
     this AndWhichConstraint <TAssertion, IVariable> andWhichConstraint, BuiltinTypeId typeId, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Subject.Value.Should().HaveType(typeId, because, reasonArgs);
     return(andWhichConstraint);
 }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithMergedTypes <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, params BuiltinTypeId[] typeIds)
 {
     andWhichConstraint.Which.Should().HaveMergedTypes(typeIds);
     return(andWhichConstraint);
 }
        /// <summary>
        /// Asserts that the dictionary contains the specified value. Values are compared using
        /// their <see cref="object.Equals(object)" /> implementation.
        /// </summary>
        /// <param name="expected">The expected value</param>
        /// <param name="because">
        /// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
        /// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
        /// </param>
        /// <param name="reasonArgs">
        /// Zero or more objects to format using the placeholders in <see cref="because" />.
        /// </param>
        public AndWhichConstraint <GenericDictionaryAssertions <TKey, TValue>, TValue> ContainValue(TValue expected,
                                                                                                    string because = "", params object[] reasonArgs)
        {
            AndWhichConstraint <GenericDictionaryAssertions <TKey, TValue>, IEnumerable <TValue> > innerConstraint =
                ContainValuesAndWhich(new[] { expected }, because, reasonArgs);

            return
                (new AndWhichConstraint
                 <GenericDictionaryAssertions <TKey, TValue>, TValue>(
                     innerConstraint.And, innerConstraint.Which));
        }
 public static AndWhichConstraint <TAssertion, VariableDefTestInfo> WithMergedTypes <TAssertion>(this AndWhichConstraint <TAssertion, VariableDefTestInfo> andWhichConstraint, IEnumerable <BuiltinTypeId> typeIds, string because = "", params object[] reasonArgs)
 {
     andWhichConstraint.Which.Should().HaveMergedTypes(typeIds, because, reasonArgs);
     return(andWhichConstraint);
 }