Exemple #1
0
            public override void AfterAct(ActBase act)
            {
                if (act.Exception != null)
                {
                    return;
                }

                if (_Event.WaitOne(_Timeout))
                {
                    // Everything is OK
                    return;
                }

                if (!act.Context.TryGetValue(_ContextHandleSet, out bool handleSet))
                {
                    // Not a context handle
                    throw new SmartTestException(Resource.TimeoutReached, _Timeout.TotalMilliseconds);
                }


                // context handle is set? => waiting for the wrong handle!
                if (handleSet)
                {
                    throw SmartTest.InconclusiveException(Resource.BadTest_UnexpectedContextSetHandle);
                }
                throw new SmartTestException(Resource.TimeoutReached, _Timeout.TotalMilliseconds);
            }
Exemple #2
0
        internal void AfterAct()
        {
            foreach (var assertion in _DoneAssertions)
            {
                try
                {
                    assertion.AfterAct(this);
                }
                catch (Exception e)
                {
                    if (Exception == null)
                    {
                        Exception = e;
                    }
                }
            }

            switch (Exception)
            {
            case null:
                return;

            case SmartTestException _:
                throw Exception;

            default:
                if (SmartTest.Inconclusive(Exception))
                {
                    throw Exception;
                }
                throw new SmartTestException("Unexpected error occurred!", Exception);
            }
        }
Exemple #3
0
 public WithinAssertion(long maximumMilliseconds)
 {
     if (maximumMilliseconds <= 0)
     {
         throw SmartTest.InconclusiveException(Resource.BadTest_NegativeTimeSpan, maximumMilliseconds);
     }
     _MaximumMilliseconds = maximumMilliseconds;
 }
        public void PathRange2()
        {
            // Act
            var result = SmartTest.Case(( Data d ) => d.Info.Range(10, 20).Range(30, 40), out var value);

            // Assert
            Assert.AreEqual("d.Info", result.ParameterName);
            Assert.IsTrue(10 <= value && value <= 20 || 30 <= value && value <= 40);
        }
Exemple #5
0
 /// <summary>
 ///     Sets the implicit wait handle of the <see cref="ActContext" />.
 /// </summary>
 /// <param name="context">The <see cref="ActContext" /> for which to set the implicit wait handle</param>
 /// <exception cref="BadTestException">
 ///     If the Wait assertion is not waiting the implicit wait handle (
 ///     <see cref="WaitContextHandle(SmartTests.SmartAssertPlaceHolder,System.TimeSpan)" /> nor
 ///     <see cref="WaitContextHandle(SmartTests.SmartAssertPlaceHolder,double)" /> is called).
 /// </exception>
 /// <example>
 ///     <para>
 ///         In this example, the <c>SetHandle</c> set the implicit wait handle expected by the
 ///         <see cref="WaitContextHandle(SmartTests.SmartAssertPlaceHolder,double)" /> Smart Assertion call.
 ///     </para>
 ///     <code>
 /// [Test]
 /// public void MyMethodTest()
 /// {
 ///   var mc = new MyClass(300);
 ///
 ///   RunTest( AnyValue.IsValid,
 ///            ctx => mc.Method( ctx.SetHandle ),
 ///            SmartAssert.Within( 100 ),
 ///            SmartAssert.WaitContextHandle( 1000 ) );
 ///
 ///   Assert.IsTrue( mc.Done ); // The method runs the parallel code (ctx.SetHandle) to its end.
 ///   Assert.IsNull( mc.Exception ); // There was no exception in the parallel code thread.
 /// }</code>
 /// </example>
 public static void SetHandle(this ActContext context)
 {
     context[_ContextHandleSet] = true;
     if (!context.TryGetValue(_ContextHandle, out EventWaitHandle obj))
     {
         throw SmartTest.InconclusiveException(Resource.BadTest_UnexpectedContextSetHandle);
     }
     obj.Set();
 }
        public void EnumPathValues()
        {
            // Act
            var result = SmartTest.Case(( Data d ) => d.Day.Values(DayOfWeek.Saturday, DayOfWeek.Sunday), out var value);

            // Assert
            Assert.AreEqual("d.Day", result.ParameterName);
            Assert.IsTrue(value == DayOfWeek.Saturday || value == DayOfWeek.Sunday);
        }
        public void PathRange()
        {
            // Act
            var result = SmartTest.Case(( Data d ) => d.Info.Range(10, 20), out var value);

            // Assert
            Assert.AreEqual("d.Info", result.ParameterName);
            Assert.GreaterOrEqual(value, 10);
            Assert.LessOrEqual(value, 20);
        }
        public void Range()
        {
            // Act
            var result = SmartTest.Case(( byte b ) => b.Range(10, 20), out var value);

            // Assert
            Assert.AreEqual("b", result.ParameterName);
            Assert.GreaterOrEqual(value, 10);
            Assert.LessOrEqual(value, 20);
        }
Exemple #9
0
        /// <summary>
        ///     Creates an instance of <see cref="AssignAct{T}" /> to represent an assignment of a property or indexer in the Act
        ///     part of your test.
        /// </summary>
        /// <param name="assignee">A lambda <see cref="Expression" /> of the assigned member.</param>
        /// <param name="value">The value to be assigned.</param>
        /// <remarks>
        ///     <para>DO NOT USE DIRECTLY.</para>
        ///     <para>Prefer using <see cref="SmartTest.Assign{T}" /> method.</para>
        /// </remarks>
        public AssignAct(Expression <Func <T> > assignee, T value)
        {
            _Assignee = assignee;
            _Value    = value;

            if (_Assignee.GetMemberContext(out var instance, out var member, out _Arguments))
            {
                Instance = instance;
                Field    = member as FieldInfo;
                if (Field != null)
                {
                    return;
                }

                Constructor = member as ConstructorInfo;
                if (Constructor != null)
                {
                    throw SmartTest.InconclusiveException(Resource.BadTest_NotWritablePropertyNorIndexer, member.GetFullName());
                }

                Method = member as MethodInfo;
                if (Method != null)
                {
                    if (!Method.IsSpecialName)
                    {
                        throw SmartTest.InconclusiveException(Resource.BadTest_NotWritablePropertyNorIndexer, member.GetFullName());
                    }
                    //An indexer?
                    foreach (var property in Method.DeclaringType.GetRuntimeProperties())
                    {
                        if (!Equals(property.GetMethod, Method))
                        {
                            continue;
                        }
                        Property = property;
                        Method   = property.SetMethod;
                        break;
                    }
                }
                else
                {
                    Property = (PropertyInfo)member;
                    Method   = Property.SetMethod;
                }
            }

            if (Property == null &&
                Field == null &&
                Method == null)
            {
                throw SmartTest.InconclusiveException(Resource.BadTest_NotWritablePropertyNorIndexer, member.GetFullName());
            }
        }
        private static void GetInstanceAndProperty <T>(Expression <Func <T> > expression, out object instance, out PropertyInfo property)
        {
            if (!expression.GetMemberContext(out instance, out var member))
            {
                throw SmartTest.InconclusiveException(Resource.BadTest_NotPropertyNorIndexer);
            }

            property = member as PropertyInfo;
            if (property == null)
            {
                throw SmartTest.InconclusiveException(Resource.BadTest_NotPropertyNorIndexer, member.GetFullName());
            }
        }
Exemple #11
0
        private void FillAct(LambdaExpression invocation)
        {
            if (invocation.GetMemberContext(out object instance, out MemberInfo member))
            {
                Instance    = instance;
                Constructor = member as ConstructorInfo;
                Method      = member as MethodInfo;
            }

            if (Method == null)
            {
                throw SmartTest.InconclusiveException();
            }
        }
Exemple #12
0
            public override void BeforeAct(ActBase act)
            {
                _Assignee = act as IAssignee;
                if (_Assignee == null)
                {
                    throw SmartTest.InconclusiveException(Resource.BadTest_NotAssignment);
                }

                _Value = _Assignee.AssignedValue;
                if (Equals(_Assignee.AssigneeValue, _Value))
                {
                    throw SmartTest.InconclusiveException(Resource.BadTest_UnexpectedValue, _Value);
                }
            }
            public override void BeforeAct(ActBase act)
            {
                if (_Instance == null)
                {
                    _Instance = act.Instance;
                    if (_IsImplicit)
                    {
                        if (act.Property == null)
                        {
                            throw SmartTest.InconclusiveException(Resource.BadTest_NotPropertyNorField, act.Member.Name, _Instance.GetType().GetFullName());
                        }
                        _Exceptions = new[] { act.Property.Name };
                    }
                }

                var propertiesVisibility = (Visibility)(_Kind & NotChangedKind.AllProperties);
                var instanceType         = _Instance.GetType();

                _Properties = propertiesVisibility != 0
                                  ? instanceType.GetProperties(propertiesVisibility)
                                  : new PropertyInfo[0];

                var fieldsVisibility = (Visibility)((int)(_Kind & NotChangedKind.AllFields) >> 4);

                _Fields = fieldsVisibility != 0
                              ? instanceType.GetFields(fieldsVisibility)
                              : new FieldInfo[0];

                if (_Exceptions != null)
                {
                    CheckExceptions();
                    _Properties = _Properties.Where(prop => !_Exceptions.Contains(prop.Name)).ToArray();
                    _Fields     = _Fields.Where(field => !_Exceptions.Contains(field.Name)).ToArray();
                }

                foreach (var property in _Properties)
                {
                    if (property.CanRead &&
                        property.GetMethod.GetParameters().Length == 0)
                    {
                        _PropertyValues[property] = property.GetValue(_Instance);
                    }
                }
                foreach (var field in _Fields)
                {
                    _FieldValues[field] = field.GetValue(_Instance);
                }
            }
            public override void BeforeAct(ActBase act)
            {
                if (_Instance == null)
                {
                    _Instance = act.Instance;
                }

                var instanceType = _Instance.GetType();

                Debug.Assert(instanceType != null);
                _Event = instanceType.GetRuntimeEvent(_EventName);
                if (_Event == null)
                {
                    throw SmartTest.InconclusiveException(Resource.BadTest_NotEvent, _EventName, instanceType.GetFullName());
                }

                _Event.AddEventHandler(_Instance, _RealDelegate);
            }
Exemple #15
0
            public override void BeforeAct(ActBase act)
            {
                var implicitSource = _Instance == null && _PropertyNames == null;

                if (_Instance == null)
                {
                    _Instance = act.Instance as INotifyPropertyChanged;
                }
                if (_Instance == null)
                {
                    throw SmartTest.InconclusiveException(Resource.BadTest_NotINotifyPropertyChanged, act.Instance?.GetType().FullName);
                }
                if (_CheckValue && implicitSource)
                {
                    var assignedAct = act as IAssignee;
                    if (assignedAct == null)
                    {
                        throw SmartTest.InconclusiveException(Resource.BadTest_NotAssignment);
                    }
                    _Value = assignedAct.AssignedValue;
                }


                if (_ExpectedRaised &&
                    _PropertyNames == null)
                {
                    if (act.Property == null)
                    {
                        throw SmartTest.InconclusiveException(Resource.BadTest_NotProperty, act.Method.GetFullName());
                    }
                    _PropertyNames = new List <string>
                    {
                        act.Property.Name
                    };
                }

                if (implicitSource &&
                    _PropertyNames != null)
                {
                    _PropertyNameVerifications = true;
                }

                _Instance.PropertyChanged += InstanceOnPropertyChanged;
            }
Exemple #16
0
        private static void GetInstanceAndProperty <T>(Expression <Func <T> > expression, out INotifyPropertyChanged sender, out PropertyInfo property)
        {
            if (!expression.GetMemberContext(out var instance, out var member))
            {
                throw SmartTest.InconclusiveException(Resource.BadTest_NotPropertyNorIndexer);
            }

            sender = instance as INotifyPropertyChanged;
            if (sender == null)
            {
                throw SmartTest.InconclusiveException(Resource.BadTest_NotINotifyPropertyChanged, instance.GetType().GetFullName());
            }

            property = member as PropertyInfo;
            if (property == null)
            {
                throw SmartTest.InconclusiveException(Resource.BadTest_NotPropertyNorIndexer, member.GetFullName());
            }
        }
            private void CheckExceptions()
            {
                var names = _Properties.Select(prop => prop.Name).ToList();

                names.AddRange(_Fields.Select(field => field.Name));

                var message = new StringBuilder();

                foreach (var exception in _Exceptions)
                {
                    if (!names.Contains(exception))
                    {
                        message.AppendLine();
                        message.AppendFormat(Resource.BadTest_NotPropertyNorField, exception, _Instance.GetType().GetFullName());
                    }
                }

                if (message.Length > 0)
                {
                    throw SmartTest.InconclusiveException(message.ToString(Environment.NewLine.Length, message.Length - Environment.NewLine.Length));
                }
            }
 /// <inheritdoc />
 protected override string ToString(double value) => SmartTest.ToString(value);
Exemple #19
0
 /// <inheritdoc />
 protected override string ToString(long value) => SmartTest.ToString(value);
 /// <inheritdoc />
 protected override string ToString(DateTime value) => SmartTest.ToString(value);
Exemple #21
0
 public override string ToString() =>
 Unknown
         ? "?"
         : Symbol?.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat)
 ?? SmartTest.ToString(Constant);
Exemple #22
0
 /// <inheritdoc />
 protected override string ToString(decimal value) => SmartTest.ToString(value);
 /// <inheritdoc />
 protected override string ToString(ushort value) => SmartTest.ToString(value);
Exemple #24
0
 /// <inheritdoc />
 protected override string ToString(float value) => SmartTest.ToString(value);