Esempio n. 1
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();

            for (var i = 0; i < _activator.GetConstructorParametersCount(); ++i)
            {
                var currentParamIndex = i;

                if (ArgumentIsPartOfValueIdentity(i))
                {
                    var instance2 = _activator.CreateInstanceAsValueObjectWithModifiedParameter(i);

                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(instance1.Equals(instance2),
                                                                             "a.Equals(b) should return false if both are created with different argument" + currentParamIndex, violations),
                                                    "a.Equals(b) should return false if both are created with different argument" + currentParamIndex, violations);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(instance2.Equals(instance1),
                                                                             "b.Equals(a) should return false if both are created with different argument" + currentParamIndex, violations),
                                                    "b.Equals(a) should return false if both are created with different argument" + currentParamIndex, violations);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(instance1.Equals(instance2),
                                                                             "(object)a.Equals((object)b) should return false if both are created with different argument" + currentParamIndex, violations),
                                                    "(object)a.Equals((object)b) should return false if both are created with different argument" + currentParamIndex, violations);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(instance2.Equals(instance1),
                                                                             "(object)b.Equals((object)a) should return false if both are created with different argument" + currentParamIndex, violations),
                                                    "(object)b.Equals((object)a) should return false if both are created with different argument" + currentParamIndex, violations);
                }
            }
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            RecordedAssertions.DoesNotThrow(() =>
            {
                var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();
                var instance2 = _activator.CreateInstanceAsValueObjectWithPreviousParameters();

                //TODO how to test equatable???
                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.True(instance1.Equals(instance2),
                                                                        "a.Equals(b) should return true if both are created with the same arguments", violations),
                                                "a.Equals(b) should return true if both are created with the same arguments", violations);
                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.True(instance2.Equals(instance1),
                                                                        "b.Equals(a) should return true if both are created with the same arguments", violations),
                                                "b.Equals(a) should return true if both are created with the same arguments", violations);

                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.True(instance1.Equals(instance2),
                                                                        "(object)a.Equals((object)b) should return true if both are created with the same arguments", violations),
                                                "(object)a.Equals((object)b) should return true if both are created with the same arguments", violations);
                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.True(instance2.Equals(instance1),
                                                                        "(object)b.Equals((object)a) should return true if both are created with the same arguments", violations),
                                                "(object)b.Equals((object)a) should return true if both are created with the same arguments", violations);
            }, "Should be able to create an object of type " + _activator.TargetType, violations);
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            RecordedAssertions.DoesNotThrow(() =>
            {
                var equatableEquality = TypeOf <T> .EquatableEquality();
                if (equatableEquality.HasValue)
                {
                    foreach (var factory1 in _equalInstances)
                    {
                        foreach (var factory2 in _equalInstances)
                        {
                            RecordedAssertions.DoesNotThrow(() =>
                                                            RecordedAssertions.True((bool)equatableEquality.Value().Evaluate(factory1(), factory2()),
                                                                                    "a.Equals(T b) should return true for equal objects", violations),
                                                            "a.Equals(T b) should return true for equal objects", violations);
                            RecordedAssertions.DoesNotThrow(() =>
                                                            RecordedAssertions.True((bool)equatableEquality.Value().Evaluate(factory2(), factory1()),
                                                                                    "b.Equals<T>(a) should return true for equal objects", violations),
                                                            "b.Equals(T a) should return true for equal objects", violations);

                            RecordedAssertions.DoesNotThrow(() =>
                                                            RecordedAssertions.True(factory1().Equals(factory2()),
                                                                                    "a.Equals(object b) should return true for equal objects", violations),
                                                            "a.Equals(object b) should return true for equal objects", violations);
                            RecordedAssertions.DoesNotThrow(() =>
                                                            RecordedAssertions.True(factory2().Equals(factory1()),
                                                                                    "b.Equals(object a) should return true for equal objects", violations),
                                                            "b.Equals(object a) should return true for equal objects", violations);
                        }
                    }
                }
            }, "Should be able to create an object of type " + typeof(T), violations);
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            foreach (var factory in _equalInstances.Concat(_otherInstances))
            {
                RecordedAssertions.DoesNotThrow(() =>
                {
                    var instance1 = factory();

                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.True(instance1.Equals(instance1),
                                                                            "a.Equals(object a) should return true", violations),
                                                    "a.Equals(object a) should return true", violations);
                }, "Should be able to create an object of type " + typeof(T), violations);

                var equatableEquals = TypeOf <T> .EquatableEquality();

                if (equatableEquals.HasValue)
                {
                    var instance1 = factory();
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.True((bool)equatableEquals.Value().Evaluate(instance1, instance1),
                                                                            "a.Equals(a) should return true", violations),
                                                    "a.Equals(a) should return true", violations);
                }
            }
        }
Esempio n. 5
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();
            var instance3 = _activator.CreateInstanceAsValueObjectWithPreviousParameters();

            for (var i = 0; i < _activator.GetConstructorParametersCount(); ++i)
            {
                if (ArgumentIsPartOfValueIdentity(i))
                {
                    var instance2 = _activator.CreateInstanceAsValueObjectWithModifiedParameter(i);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.NotEqual(instance1.GetHashCode(), instance2.GetHashCode(),
                                                                                "b.GetHashCode() and b.GetHashCode() should return different values when both are created with different argument" + i, violations),
                                                    "b.GetHashCode() and b.GetHashCode() should return different values when both are created with different argument" + i, violations);
                }
            }

            RecordedAssertions.DoesNotThrow(() =>
                                            RecordedAssertions.Equal(instance1.GetHashCode(), instance3.GetHashCode(),
                                                                     "a.GetHashCode() and b.GetHashCode() should return same values when both are created with same arguments", violations),
                                            "a.GetHashCode() and b.GetHashCode() should return same values when both are created with same arguments", violations);
            RecordedAssertions.DoesNotThrow(() =>
                                            RecordedAssertions.Equal(instance1.GetHashCode(), instance3.GetHashCode(),
                                                                     "a.GetHashCode() and b.GetHashCode() should return same values when both are created with same arguments", violations),
                                            "a.GetHashCode() and b.GetHashCode() should return same values when both are created with same arguments", violations);
        }
Esempio n. 6
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            foreach (var factory1 in _equalInstances)
            {
                foreach (var factory2 in _otherInstances)
                {
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(factory1().Equals(factory2()),
                                                                             "a.Equals(object b) should return false if they are not equal", violations),
                                                    "a.Equals(object b) should return false if they are not equal", violations);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(factory2().Equals(factory1()),
                                                                             "b.Equals(object a) should return false if they are not equal", violations),
                                                    "b.Equals(object a) should return false if they are not equal", violations);

                    var equatableEquals = TypeOf <T> .EquatableEquality();

                    if (equatableEquals.HasValue)
                    {
                        RecordedAssertions.DoesNotThrow(() =>
                                                        RecordedAssertions.False((bool)equatableEquals.Value().Evaluate(factory1(), factory2()),
                                                                                 "a.Equals(T b) should return false if they are not equal", violations),
                                                        "a.Equals(T b) should return false if they are not equal", violations);
                        RecordedAssertions.DoesNotThrow(() =>
                                                        RecordedAssertions.False((bool)equatableEquals.Value().Evaluate(factory2(), factory1()),
                                                                                 "b.Equals(T a) should return false if they are not equal", violations),
                                                        "b.Equals(T a) should return false if they are not equal", violations);
                    }
                }
            }
        }
Esempio n. 7
0
        public void ShouldNotThrowExceptionWhenNoViolationsHaveBeenAdded()
        {
            //GIVEN
            var violations = new ConstraintsViolations();

            //WHEN - THEN
            new Action(violations.AssertNone).Should().NotThrow();
        }
Esempio n. 8
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();

            RecordedAssertions.DoesNotThrow(() =>
                                            RecordedAssertions.True(Are.EqualInTermsOfEqualityOperator(_activator.TargetType, instance1, instance1),
                                                                    "a == a should return true", violations), "a == a should return true", violations);
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();

            RecordedAssertions.DoesNotThrow(() =>
                                            RecordedAssertions.False(instance1.Equals(null),
                                                                     "a.Equals(null) should return false", violations),
                                            "a.Equals(null) should return false", violations);
        }
Esempio n. 10
0
 public void CheckAndRecord(ConstraintsViolations violations)
 {
     RecordedAssertions.DoesNotThrow(() =>
                                     RecordedAssertions.True(_type.IsSealed,
                                                             $"{_type.Name} must be sealed, or derivatives will be able to override GetHashCode() with mutable code",
                                                             violations),
                                     $"{_type.Name} must be sealed, or derivatives will be able to override GetHashCode() with mutable code",
                                     violations);
 }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var properties = SmartType.For(_type).GetAllPublicInstanceProperties();

            foreach (var item in properties.Where(item => item.HasPublicSetter()))
            {
                violations.Add(item.ShouldNotBeMutableButIs());
            }
        }
Esempio n. 12
0
        public void ShouldThrowExceptionWhenAtLeastOneViolationWasAdded()
        {
            //GIVEN
            var violations = new ConstraintsViolations();

            violations.Add(Any.String());

            //WHEN - THEN
            new Action(violations.AssertNone).Should().ThrowExactly <XunitException>();
        }
Esempio n. 13
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var constructors          = SmartType.For(_type).GetAllPublicConstructors();
            var fallbackTypeGenerator = new FallbackTypeGenerator(_type);

            foreach (var constructor in constructors)
            {
                AssertNullCheckForEveryPossibleArgumentOf(violations, constructor, fallbackTypeGenerator);
            }
        }
Esempio n. 14
0
        public void ShouldThrowExceptionWhenAtLeastOneViolationWasAdded()
        {
            //GIVEN
            var violations = new ConstraintsViolations();

            violations.Add(Any.String());

            //WHEN - THEN
            Assert.Throws <AssertionException>(violations.AssertNone);
        }
 public void CheckAndRecord(ConstraintsViolations violations)
 {
     foreach (var factory in _equalInstances.Concat(_otherInstances))
     {
         var instance = factory();
         RecordedAssertions.DoesNotThrow(() =>
                                         RecordedAssertions.False(Are.NotEqualInTermsOfInEqualityOperator(typeof(T), instance, instance),
                                                                  "a != a should return false", violations),
                                         "a != a should return false", violations);
     }
 }
Esempio n. 16
0
 public static void DoesNotThrow(Action action, string message, ConstraintsViolations errors)
 {
     try
     {
         action();
     }
     catch (Exception e)
     {
         errors.Add(message + ", but instead caught: " + e);
     }
 }
Esempio n. 17
0
        private void CheckImmutability(ConstraintsViolations violations, Type type)
        {
            var fields        = SmartType.For(type).GetAllInstanceFields().ToList();
            var fieldWrappers = fields
                                .Where(item => item.IsNotDeveloperDefinedReadOnlyField());

            foreach (var item in fieldWrappers)
            {
                violations.Add(item.ShouldNotBeMutableButIs());
            }
        }
 public void CheckAndRecord(ConstraintsViolations violations)
 {
     RecordedAssertions.DoesNotThrow(() =>
     {
         var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();
         RecordedAssertions.DoesNotThrow(() =>
                                         RecordedAssertions.True(instance1.Equals(instance1),
                                                                 "a.Equals(a) should return true", violations),
                                         "a.Equals(a) should return true", violations);
     }, "Should be able to create an object of type " + _activator.TargetType, violations);
 }
Esempio n. 19
0
        public static void TypeAdheresTo(IEnumerable <IConstraint> constraints)
        {
            var violations = ConstraintsViolations.Empty();

            foreach (var constraint in constraints)
            {
                RecordedAssertions.DoesNotThrow(() => constraint.CheckAndRecord(violations),
                                                "Did not expect exception", violations);
            }

            violations.AssertNone();
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            if (_type.IsAbstract)
            {
                violations.Add("SmartType " + _type + " is abstract but abstract classes cannot be value objects");
            }

            if (_type.IsInterface)
            {
                violations.Add("SmartType " + _type + " is an interface but interfaces cannot be value objects");
            }
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();
            var instance2 = _activator.CreateInstanceAsValueObjectWithPreviousParameters();

            RecordedAssertions.DoesNotThrow(() =>
                                            RecordedAssertions.False(Are.NotEqualInTermsOfInEqualityOperator(_activator.TargetType, instance1, instance2),
                                                                     "a != b should return false if both are created with the same arguments", violations),
                                            "a != b should return false if both are created with the same arguments", violations);
            RecordedAssertions.DoesNotThrow(() =>
                                            RecordedAssertions.False(Are.NotEqualInTermsOfInEqualityOperator(_activator.TargetType, instance2, instance1),
                                                                     "b != a should return false if both are created with the same arguments", violations),
                                            "b != a should return false if both are created with the same arguments", violations);
        }
Esempio n. 22
0
        public void ShouldThrowExceptionContainingAllViolationMessagesWhenMoreThanOneViolationWasAdded()
        {
            //GIVEN
            var violations = new ConstraintsViolations();
            var violation1 = Any.String();
            var violation2 = Any.String();
            var violation3 = Any.String();

            violations.Add(violation1);
            violations.Add(violation2);
            violations.Add(violation3);

            //WHEN - THEN
            new Action(violations.AssertNone).Should().Throw <Exception>()
            .And.Message.Should().ContainAll(violation1, violation2, violation3);
        }
Esempio n. 23
0
 public void CheckAndRecord(ConstraintsViolations violations)
 {
     foreach (var factory1 in _equalInstances)
     {
         foreach (var factory2 in _otherInstances)
         {
             RecordedAssertions.DoesNotThrow(() =>
                                             RecordedAssertions.True(Are.NotEqualInTermsOfInEqualityOperator(typeof(T), factory1(), factory2()),
                                                                     "a != b should return true if they are not equal", violations),
                                             "a != b should return true if they are not equal", violations);
             RecordedAssertions.DoesNotThrow(() =>
                                             RecordedAssertions.True(Are.NotEqualInTermsOfInEqualityOperator(typeof(T), factory1(), factory2()),
                                                                     "b != a should return true if if they are not equal", violations),
                                             "b != a should return true if if they are not equal", violations);
         }
     }
 }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            foreach (var factory in _equalInstances)
            {
                var instance1 = factory();
                var instance2 = factory();

                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.False(Are.NotEqualInTermsOfInEqualityOperator(typeof(T), instance1, instance2),
                                                                         "a != b should return false for equal values", violations),
                                                "a != b should return false for equal values", violations);
                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.False(Are.NotEqualInTermsOfInEqualityOperator(typeof(T), instance2, instance1),
                                                                         "b != a should return false for equal values", violations),
                                                "b != a should return false for equal values", violations);
            }
        }
Esempio n. 25
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            if (!typeof(T).IsValueType)
            {
                foreach (var factory in _equalInstances.Concat(_otherInstances))
                {
                    var instance1 = factory();
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(Are.EqualInTermsOfEqualityOperator(typeof(T), instance1, null),
                                                                             "a == null should return false", violations),
                                                    "a == null should return false", violations);

                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.False(Are.EqualInTermsOfEqualityOperator(typeof(T), null, instance1),
                                                                             "null == a should return false", violations),
                                                    "null == a should return false", violations);
                }
            }
        }
 public void CheckAndRecord(ConstraintsViolations violations)
 {
     foreach (var factory1 in _equalInstances)
     {
         foreach (var factory2 in _otherInstances)
         {
             RecordedAssertions.DoesNotThrow(() =>
                                             RecordedAssertions.False(Are.EqualInTermsOfEqualityOperator(typeof(T), factory1(), factory2()),
                                                                      "a == b should return false for not equal values", violations),
                                             "a == b should return false for not equal values", violations
                                             );
             RecordedAssertions.DoesNotThrow(() =>
                                             RecordedAssertions.False(Are.EqualInTermsOfEqualityOperator(typeof(T), factory2(), factory1()),
                                                                      "b == a should return false for not equal values", violations),
                                             "b == a should return false for not equal values", violations
                                             );
         }
     }
 }
Esempio n. 27
0
        public void ShouldThrowExceptionContainingAllViolationMessagesWhenMoreThanOneViolationWasAdded()
        {
            //GIVEN
            var violations = new ConstraintsViolations();
            var violation1 = Any.String();
            var violation2 = Any.String();
            var violation3 = Any.String();

            violations.Add(violation1);
            violations.Add(violation2);
            violations.Add(violation3);

            //WHEN - THEN
            var exception = Assert.Throws <AssertionException>(violations.AssertNone);

            StringAssert.Contains(violation1, exception.Message);
            StringAssert.Contains(violation2, exception.Message);
            StringAssert.Contains(violation3, exception.Message);
        }
Esempio n. 28
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            foreach (var factory1 in _equalInstances)
            {
                foreach (var factory2 in _equalInstances)
                {
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.True(Are.EqualInTermsOfEqualityOperator(typeof(T), factory1(), factory2()),
                                                                            "a == b should return true if both are created with the same arguments that define equality", violations),
                                                    "a == b should return true if both are created with the same arguments that define equality", violations
                                                    );

                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.True(Are.EqualInTermsOfEqualityOperator(typeof(T), factory2(), factory1()),
                                                                            "b == a should return true if both are created with the same arguments that define equality", violations),
                                                    "b == a should return true if both are created with the same arguments that define equality", violations
                                                    );
                }
            }
        }
Esempio n. 29
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            foreach (var factory1 in _equalInstances)
            {
                foreach (var factory2 in _otherInstances)
                {
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.NotEqual(factory1().GetHashCode(), factory2().GetHashCode(),
                                                                                "b.GetHashCode() and b.GetHashCode() should return different values for not equal objects", violations),
                                                    "b.GetHashCode() and b.GetHashCode() should return different values for not equal objects", violations);
                }
            }

            foreach (var factory in _equalInstances.Concat(_otherInstances))
            {
                var instance = factory();
                RecordedAssertions.DoesNotThrow(() =>
                                                RecordedAssertions.Equal(instance.GetHashCode(), instance.GetHashCode(),
                                                                         "a.GetHashCode() should consistently return the same value", violations),
                                                "a.GetHashCode() should consistently return the same value", violations);
            }

            foreach (var factory1 in _equalInstances)
            {
                foreach (var factory2 in _equalInstances)
                {
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.Equal(factory1().GetHashCode(), factory2().GetHashCode(),
                                                                             "a.GetHashCode() and b.GetHashCode() should be equal for equal objects",
                                                                             violations),
                                                    "a.GetHashCode() and b.GetHashCode() should be equal for equal objects",
                                                    violations);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.Equal(factory1().GetHashCode(), factory2().GetHashCode(),
                                                                             "a.GetHashCode() and b.GetHashCode() should be equal for equal objects",
                                                                             violations),
                                                    "a.GetHashCode() and b.GetHashCode() should be equal for equal objects",
                                                    violations);
                }
            }
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var instance1 = _activator.CreateInstanceAsValueObjectWithFreshParameters();

            for (var i = 0; i < _activator.GetConstructorParametersCount(); ++i)
            {
                if (ArgumentIsPartOfValueIdentity(i))
                {
                    var instance2 = _activator.CreateInstanceAsValueObjectWithModifiedParameter(i);

                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.True(Are.NotEqualInTermsOfInEqualityOperator(_activator.TargetType, instance1, instance2),
                                                                            "a != b should return true if both are created with different argument" + i, violations),
                                                    "a != b should return true if both are created with different argument" + i, violations);
                    RecordedAssertions.DoesNotThrow(() =>
                                                    RecordedAssertions.True(Are.NotEqualInTermsOfInEqualityOperator(_activator.TargetType, instance1, instance2),
                                                                            "b != a should return true if both are created with different argument" + i, violations),
                                                    "b != a should return true if both are created with different argument" + i, violations);
                }
            }
        }