コード例 #1
0
    public void ShouldDeliverErrorsWhenModelObservableValidationTriggers()
    {
        var viewModel = new IndeiTestViewModel();

        const string namesShouldMatchMessage = "names should match.";

        viewModel.ValidationRule(
            vm => vm.OtherName,
            viewModel.WhenAnyValue(
                m => m.Name,
                m => m.OtherName,
                (name, other) => name == other),
            namesShouldMatchMessage);

        Assert.False(viewModel.HasErrors);
        Assert.True(viewModel.ValidationContext.IsValid);
        Assert.Single(viewModel.ValidationContext.Validations);
        Assert.Empty(viewModel.GetErrors(nameof(viewModel.Name)).Cast <string>());
        Assert.Empty(viewModel.GetErrors(nameof(viewModel.OtherName)).Cast <string>());

        viewModel.Name      = "JoJo";
        viewModel.OtherName = "NoNo";

        Assert.True(viewModel.HasErrors);
        Assert.Empty(viewModel.GetErrors(nameof(viewModel.Name)).Cast <string>());
        Assert.Single(viewModel.GetErrors(nameof(viewModel.OtherName)).Cast <string>());
        Assert.Single(viewModel.ValidationContext.Text);
        Assert.Equal(namesShouldMatchMessage, viewModel.ValidationContext.Text.Single());
    }
コード例 #2
0
    public void ShouldFireErrorsChangedEventWhenValidationStateChanges()
    {
        var viewModel = new IndeiTestViewModel();

        DataErrorsChangedEventArgs arguments = null;

        viewModel.ErrorsChanged += (_, args) => arguments = args;

        using var firstValidation = new BasePropertyValidation <IndeiTestViewModel, string>(
                  viewModel,
                  vm => vm.Name,
                  s => !string.IsNullOrEmpty(s),
                  NameShouldNotBeEmptyMessage);

        viewModel.ValidationContext.Add(firstValidation);

        Assert.True(viewModel.HasErrors);
        Assert.False(viewModel.ValidationContext.IsValid);
        Assert.Single(viewModel.ValidationContext.Validations);
        Assert.Single(viewModel.GetErrors("Name").Cast <string>());

        viewModel.Name = "JoJo";

        Assert.False(viewModel.HasErrors);
        Assert.Empty(viewModel.GetErrors("Name").Cast <string>());
        Assert.NotNull(arguments);
        Assert.Equal("Name", arguments.PropertyName);
    }
コード例 #3
0
        public void ShouldDeliverErrorsWhenModelObservableValidationTriggers()
        {
            var viewModel = new IndeiTestViewModel();

            string namesShouldMatchMessage = "names should match.";
            var    validation = new ModelObservableValidation <IndeiTestViewModel, string>(
                viewModel,
                vm => vm.OtherName,
                vm => vm.WhenAnyValue(
                    m => m.Name,
                    m => m.OtherName,
                    (n, on) => new { n, on })
                .Select(bothNames => bothNames.n == bothNames.on),
                (_, isValid) => isValid ? string.Empty : namesShouldMatchMessage);

            viewModel.ValidationContext.Add(validation);

            Assert.False(viewModel.HasErrors);
            Assert.True(viewModel.ValidationContext.IsValid);
            Assert.Single(viewModel.ValidationContext.Validations);
            Assert.Empty(viewModel.GetErrors(nameof(viewModel.Name)).Cast <string>());
            Assert.Empty(viewModel.GetErrors(nameof(viewModel.OtherName)).Cast <string>());

            viewModel.Name      = "JoJo";
            viewModel.OtherName = "NoNo";

            Assert.True(viewModel.HasErrors);
            Assert.Empty(viewModel.GetErrors(nameof(viewModel.Name)).Cast <string>());
            Assert.Single(viewModel.GetErrors(nameof(viewModel.OtherName)).Cast <string>());
            Assert.Single(validation.Text);
            Assert.Equal(namesShouldMatchMessage, validation.Text.Single());
        }
コード例 #4
0
    public void ShouldSynchronizeNotifyDataErrorInfoWithValidationContext()
    {
        var viewModel = new IndeiTestViewModel();
        var view      = new IndeiTestView(viewModel);

        using var firstValidation = new BasePropertyValidation <IndeiTestViewModel, string>(
                  viewModel,
                  vm => vm.Name,
                  s => !string.IsNullOrEmpty(s),
                  NameShouldNotBeEmptyMessage);

        viewModel.ValidationContext.Add(firstValidation);
        view.Bind(view.ViewModel, vm => vm.Name, v => v.NameLabel);
        view.BindValidation(view.ViewModel, vm => vm.Name, v => v.NameErrorLabel);

        // Verify the initial state.
        Assert.True(viewModel.HasErrors);
        Assert.False(viewModel.ValidationContext.IsValid);
        Assert.Single(viewModel.ValidationContext.Validations);
        Assert.Equal(NameShouldNotBeEmptyMessage, viewModel.GetErrors("Name").Cast <string>().First());
        Assert.Equal(NameShouldNotBeEmptyMessage, view.NameErrorLabel);

        // Send INotifyPropertyChanged.
        viewModel.Name = "JoJo";

        // Verify the changed state.
        Assert.False(viewModel.HasErrors);
        Assert.True(viewModel.ValidationContext.IsValid);
        Assert.Empty(viewModel.GetErrors("Name").Cast <string>());
        Assert.Empty(view.NameErrorLabel);

        // Send INotifyPropertyChanged.
        viewModel.Name = string.Empty;

        // Verify the changed state.
        Assert.True(viewModel.HasErrors);
        Assert.False(viewModel.ValidationContext.IsValid);
        Assert.Single(viewModel.ValidationContext.Validations);
        Assert.Equal(NameShouldNotBeEmptyMessage, viewModel.GetErrors("Name").Cast <string>().First());
        Assert.Equal(NameShouldNotBeEmptyMessage, view.NameErrorLabel);
    }
コード例 #5
0
    public void ShouldSendPropertyChangeNotificationsForCorrectProperties()
    {
        var viewModel = new IndeiTestViewModel();

        viewModel.ValidationRule(
            m => m.Name,
            m => m is not null,
            "Name shouldn't be null.");

        viewModel.ValidationRule(
            m => m.OtherName,
            m => m is not null,
            "Other name shouldn't be null.");

        Assert.Single(viewModel.GetErrors(nameof(viewModel.Name)));
        Assert.Single(viewModel.GetErrors(nameof(viewModel.OtherName)));

        var arguments = new List <DataErrorsChangedEventArgs>();

        viewModel.ErrorsChanged += (_, args) => arguments.Add(args);
        viewModel.Name           = "Josuke";
        viewModel.OtherName      = "Jotaro";

        Assert.Equal(2, arguments.Count);
        Assert.Equal(nameof(viewModel.Name), arguments[0].PropertyName);
        Assert.Equal(nameof(viewModel.OtherName), arguments[1].PropertyName);
        Assert.False(viewModel.HasErrors);

        viewModel.Name      = null;
        viewModel.OtherName = null;

        Assert.Equal(4, arguments.Count);
        Assert.Equal(nameof(viewModel.Name), arguments[2].PropertyName);
        Assert.Equal(nameof(viewModel.OtherName), arguments[3].PropertyName);
        Assert.True(viewModel.HasErrors);
    }
コード例 #6
0
    public void ShouldMarkPropertiesAsInvalidOnInit()
    {
        var viewModel = new IndeiTestViewModel();
        var view      = new IndeiTestView(viewModel);

        using var firstValidation = new BasePropertyValidation <IndeiTestViewModel, string>(
                  viewModel,
                  vm => vm.Name,
                  s => !string.IsNullOrEmpty(s),
                  NameShouldNotBeEmptyMessage);

        viewModel.ValidationContext.Add(firstValidation);
        view.Bind(view.ViewModel, vm => vm.Name, v => v.NameLabel);
        view.BindValidation(view.ViewModel, vm => vm.Name, v => v.NameErrorLabel);

        // Verify validation context behavior.
        Assert.False(viewModel.ValidationContext.IsValid);
        Assert.Single(viewModel.ValidationContext.Validations);
        Assert.Equal(NameShouldNotBeEmptyMessage, view.NameErrorLabel);

        // Verify INotifyDataErrorInfo behavior.
        Assert.True(viewModel.HasErrors);
        Assert.Equal(NameShouldNotBeEmptyMessage, viewModel.GetErrors("Name").Cast <string>().First());
    }