コード例 #1
0
ファイル: BaseDomainEntity.cs プロジェクト: gkowieski/vm
        /// <summary>
        /// Validates this instance.
        /// </summary>
        /// <param name="ruleset">The ruleset to test validity against.</param>
        /// <param name="results">An existing results collection to which the current validation results should be appended to.</param>
        /// <returns>A list of <see cref="ValidationResult" /> objects.</returns>
        public virtual ValidationResults Validate(
            string ruleset            = "",
            ValidationResults results = null)
        {
            var validator = Facility.ValidatorFactory
                            .CreateValidator(GetType(), ruleset);

            if (results == null)
            {
                results = validator.Validate(this);
            }
            else
            {
                validator.Validate(this, results);
            }

#if DEBUG
            if (!results.IsValid)
            {
                Debug.WriteLine($"{ToString()}\n{results.DumpString()}");
            }
#endif

            return(results);
        }
コード例 #2
0
        static void TestValidator <T>(
            TestContext testContext,
            ValidatorAttribute attribute,
            T value,
            bool isValid,
            bool negated)
        {
            var results = new ValidationResults();

            try
            {
                var wrappedAttribute = new PrivateObject(attribute);

                if (negated)
                {
                    wrappedAttribute.SetProperty("Negated", true);
                }

                var validator = wrappedAttribute.Invoke("DoCreateValidator", typeof(T)) as Validator;

                Assert.IsNotNull(validator);

                var validatorAccessor = new PrivateObject(validator);

                validatorAccessor.Invoke(
                    "DoValidate",
                    value,
                    null,
                    null,
                    results);

                Assert.AreEqual(isValid, results.IsValid);
            }
            catch (UnitTestAssertException)
            {
                throw;
            }
            catch (Exception x)
            {
                testContext.WriteLine("{0}", x.DumpString());
                throw;
            }
            finally
            {
                testContext.WriteLine("{0}", results.DumpString());
            }
        }