예제 #1
0
        public void CallWithThrow()
        {
            string exMessage       = "Employee not in correct age";
            var    validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null),
                new ValidationRule <Employee>(e => e.Age == 40).AddException(new ArgumentException(exMessage)),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000)),
                new ValidationRule <Employee>(e => !e.IsFullTime)
            };

            try
            {
                var orc = new ImmutableObjectRuleChecker <Employee>(validationRules);
                orc.GetValidationResults(new Employee());
            }
            catch (ArgumentException aex)
            {
                Assert.Equal(aex.Message, exMessage);
            }
            catch
            {
                Assert.True(false); //fail it we have some other exception
            }
        }
예제 #2
0
        public void CallWithBoolResult()
        {
            var validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null),
                new ValidationRule <Employee>(e => e.Age == 30),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000))
            };
            var  orc     = new ImmutableObjectRuleChecker <Employee>(validationRules);
            bool isValid = orc.CheckAll(new Employee());

            Assert.True(isValid);
        }
예제 #3
0
        public void CallWithNull()
        {
            var validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000)),
                new ValidationRule <Employee>(e => !e.IsFullTime)
            };
            var  orc     = new ImmutableObjectRuleChecker <Employee>(validationRules);
            bool isValid = orc.CheckAll(null);

            Assert.False(isValid);
        }
예제 #4
0
        public void CallWithBlocking()
        {
            var validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null).MakeBlocking(),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000)),
                new ValidationRule <Employee>(e => !e.IsFullTime)
            };

            var orc     = new ImmutableObjectRuleChecker <Employee>(validationRules);
            var results = orc.GetValidationResults(null);

            Assert.True(results.Count == 1);
        }
예제 #5
0
        public void ReferenceEqualsTest()
        {
            var validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null),
                new ValidationRule <Employee>(e => e.IsFullTime),
                new ValidationRule <Employee>(e => e.Person != null)
            };
            var orc = new ImmutableObjectRuleChecker <Employee>(validationRules);

            var orc2 = orc.IsNotValidWhen(e => e.SalaryAmount == 2000);

            Assert.NotSame(orc, orc2);
        }
예제 #6
0
        public void CallWithResultsNoExceptions()
        {
            var validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null),
                new ValidationRule <Employee>(e => e.Age == 30),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000)),
                new ValidationRule <Employee>(e => !e.IsFullTime)
            };
            var orc     = new ImmutableObjectRuleChecker <Employee>(validationRules);
            var results = orc.GetValidationResults(new Employee());

            Assert.True(results.Count == 4);
            Assert.True(results.Where(r => !r.IsSuccess).ToList().Count == 1);
        }
예제 #7
0
        public void CallWithBlockingAndThrow()
        {
            Employee employee        = null;
            var      validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null).MakeBlocking().AddException(new ArgumentNullException(nameof(employee))),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000)),
                new ValidationRule <Employee>(e => !e.IsFullTime)
            };

            var orc     = new ImmutableObjectRuleChecker <Employee>(validationRules);
            var results = orc.GetValidationResults(null);

            Assert.True(results.Count == 1);
            Assert.True(results.FirstOrDefault()?.Exception != null);
        }
예제 #8
0
        public void CallWithResultsAndExceptions()
        {
            string exMessage       = "Invalid employee contract";
            var    validationRules = new List <ValidationRule <Employee> >
            {
                new ValidationRule <Employee>(e => e != null),
                new ValidationRule <Employee>(e => e.Age == 30),
                new ValidationRule <Employee>(e => !(e.SalaryAmount > 2000)),
                new ValidationRule <Employee>(e => !e.IsFullTime).AddException(new ArgumentException(exMessage))
            };

            var orc     = new ImmutableObjectRuleChecker <Employee>(validationRules);
            var results = orc.GetValidationResults(new Employee());

            Assert.True(results.Count == 4);
            var faultedRes = results.Where(r => !r.IsSuccess).ToList();

            Assert.True(faultedRes.Count == 1);
            var faulted = faultedRes.FirstOrDefault(fr => fr.Exception != null);

            Assert.Equal(exMessage, faulted.Exception.Message);
            Assert.True(faulted.Exception is ArgumentException);
        }