Пример #1
0
        private static void AssertNullCheckForEveryPossibleArgumentOf(IConstraintsViolations violations,
                                                                      IConstructorWrapper constructor,
                                                                      FallbackTypeGenerator fallbackTypeGenerator)
        {
            for (int i = 0; i < constructor.GetParametersCount(); ++i)
            {
                var parameters = constructor.GenerateAnyParameterValues(Any.Instance);
                if (SmartType.ForTypeOf(parameters[i]).CanBeAssignedNullValue())
                {
                    parameters[i] = null;

                    try
                    {
                        fallbackTypeGenerator.GenerateInstance(parameters);
                        violations.Add("Not guarded against nulls: " + constructor + ", Not guarded parameter: " +
                                       constructor.GetDescriptionForParameter(i));
                    }
                    catch (TargetInvocationException exception)
                    {
                        if (exception.InnerException.GetType() == typeof(ArgumentNullException))
                        {
                            //do nothing, this is the expected case
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            if (!typeof(T).IsValueType)
            {
                foreach (var factory in _equalInstances.Concat(_otherInstances))
                {
                    var instance1 = factory();
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(instance1.Equals(null),
                                                                             "a.Equals(null) should return false", violations),
                                                    "a.Equals(null) should return false", violations);

                    var equatableEquals = SmartType.ForTypeOf(instance1).EquatableEquality();
                    if (equatableEquals.HasValue)
                    {
                        RecordedAssertions.DoesNotThrow(() =>
                                                        RecordedAssertions.False((bool)equatableEquals.Value().Evaluate(instance1, null),
                                                                                 "a.Equals(null) should return false", violations),
                                                        "a.Equals(null) should return false", violations);
                    }
                }
            }
        }