public void Validate_ErrorsShouldContainsErrorMessages_WhenValidationIsNotValid()
        {
            var v = new ReactiveValidatableObject <string>();

            v.Validations.Add(new ActionValidationRule <string>(arg => { return(false); }, "You have entered an invalid email"));
            Assert.Equal(0, v.Errors.Count);
            v.Validate();
            v.Errors.CollectionChanged += (sender, e) =>
            {
                Assert.Equal(1, v.Errors.Count);
                Assert.True(v.Errors.Contains("You have entered an invalid email"));
            };
        }
        public void Validate_IsValidShouldBeTrue_WhenValidEmailIsGiven()
        {
            var v = new ReactiveValidatableObject <string>();

            v.Validations.Add(new IsNotNullOrEmptyRule <string>("Email must not be empty"));

            v.Validations.Add(new EmailRule <string>()
            {
                ValidationMessage = "You have entered an invalid email"
            });

            Assert.True(v.IsValid);
            v.Value = "*****@*****.**";
            v.Validate();
            Assert.True(v.IsValid);
        }
        public async Task ValidateAsync_IsValidShouldBeFalse_When_AsyncValidationDelegateReturnsFalse()
        {
            var v = new ReactiveValidatableObject <string>();

            v.Validations.Add(new ActionAsyncValidationRule <string>(arg => { return(Task.FromResult(false)); }, "You have entered an invalid email"));

            Assert.True(v.IsValid);
            v.Value = "*****@*****.**";
            await v.ValidateAsync();

            v.Errors.CollectionChanged += (sender, e) =>
            {
                Assert.False(v.IsValid);
                Assert.True(v.Errors.Contains("You have entered an invalid email"));
            };
        }
        public void IsValid_ShouldValidate_WhenValueIsChanged()
        {
            var scheduler = new TestScheduler();

            scheduler.With((TestScheduler sched) =>
            {
                var v = new ReactiveValidatableObject <string>();

                var isExecuted = false;
                v.Validations.Add(new IsNotNullOrEmptyRule <string>(c =>
                {
                    isExecuted = true;
                    return(false);
                }, "Email must not be empty"));

                v.Validations.Add(new EmailRule <string>()
                {
                    ValidationMessage = "You have entered an invalid email"
                });

                v.Validations.Add(new ActionAsyncValidationRule <string>(arg => { return(Task.FromResult(false)); }, "You executed async method"));

                v.Value = "t";
                Assert.True(v.IsValid);

                sched.AdvanceByMs(40);
                v.Value = "test";
                Assert.True(v.IsValid);
                sched.AdvanceByMs(200);
                v.Value = "test";
                Assert.True(isExecuted);
                Assert.False(v.IsValid);
                sched.AdvanceByMs(300);
                isExecuted = false;
                v.Value    = "test";
                Assert.False(isExecuted);

                Assert.Equal("You have entered an invalid email", v.Errors[0]);
                Assert.True(v.Errors.Contains("You executed async method"));
            });
        }
        public void Validate_ShouldSetIsValid_WhenExecuted()
        {
            var v = new ReactiveValidatableObject <string>();

            v.Validations.Add(new IsNotNullOrEmptyRule <string>("Email must not be empty"));

            v.Validations.Add(new EmailRule <string>()
            {
                ValidationMessage = "You have entered an invalid email"
            });

            Assert.True(v.IsValid);
            v.Validate();

            v.Errors.CollectionChanged += (sender, e) =>
            {
                Assert.False(v.IsValid);
                Assert.Equal(2, v.Errors.Count);
                Assert.Equal("Email must not be empty", v.Errors[0]);
            };
        }