Skip to content

Commit

Permalink
Fix #16 and added test. (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmartinezm authored and glennawatson committed Apr 1, 2019
1 parent f1263e6 commit f85c741
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
35 changes: 34 additions & 1 deletion src/ReactiveUI.Validation.Tests/ValidationContextTests.cs
Expand Up @@ -136,7 +136,6 @@ public void TwoValidationPropertiesInSamePropertyThrowsExceptionTest()
// View bindings
view.Bind(view.ViewModel, vm => vm.Name, v => v.NameLabel);

// TODO: add Assert.Throws to custom exception wrapping this call
// View validations bindings
var ex = Assert.Throws<MultipleValidationNotSupportedException>(() =>
{
Expand All @@ -154,5 +153,39 @@ public void TwoValidationPropertiesInSamePropertyThrowsExceptionTest()
$"Property {nameof(viewModel.Name)} has more than one validation rule associated. Consider using {nameof(ValidationBindingEx)} methods.";
Assert.Equal(expectedError, ex.Message);
}

/// <summary>
/// Verifies that validations registered with different lambda names are retrieved successfully.
/// </summary>
[Fact]
public void RegisterValidationsWithDifferentLambdaNameWorksTest()
{
const string validName = "valid";
const int minimumLength = 5;

var viewModel = new TestViewModel { Name = validName };
var view = new TestView(viewModel);

var firstValidation = new BasePropertyValidation<TestViewModel, string>(
viewModel,
viewModelProperty => viewModelProperty.Name,
s => !string.IsNullOrEmpty(s),
s => $"Name {s} isn't valid");

// Add validations
viewModel.ValidationContext.Add(firstValidation);

// View bindings
view.Bind(view.ViewModel, vm => vm.Name, v => v.NameLabel);

// This was throwing exception due to naming problems with lambdas expressions
view.BindValidation(
view.ViewModel,
vm => vm.Name,
v => v.NameErrorLabel);

Assert.True(viewModel.ValidationContext.IsValid);
Assert.Equal(1, viewModel.ValidationContext.Validations.Count);
}
}
}
Expand Up @@ -120,7 +120,7 @@ public void Dispose()
/// <returns>Returns true if it contains the property, otherwise false.</returns>
public bool ContainsProperty<TProp>(Expression<Func<TViewModel, TProp>> property, bool exclusively = false)
{
var propertyName = property.Body.ToString();
var propertyName = property.Body.GetMemberInfo().ToString();

return exclusively
? _propertyNames.Contains(propertyName) && _propertyNames.Count == 1
Expand All @@ -134,7 +134,7 @@ public bool ContainsProperty<TProp>(Expression<Func<TViewModel, TProp>> property
/// <param name="property">ViewModel property.</param>
protected void AddProperty<TProp>(Expression<Func<TViewModel, TProp>> property)
{
var propertyName = property.Body.ToString();
var propertyName = property.Body.GetMemberInfo().ToString();
_propertyNames.Add(propertyName);
}

Expand Down

0 comments on commit f85c741

Please sign in to comment.