Skip to content

Commit 40e7c5b

Browse files
committed
Add tests for RentPropertyAs.
1 parent ec14c65 commit 40e7c5b

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

tests/UnityMvvmToolkit.Test.Integration/BindingContextObjectProviderTests.cs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,88 @@ public void RentProperty_ShouldThrow_WhenConverterIsNotSet()
361361
.WithMessage($"Property value converter from '{typeof(int)}' to '{typeof(string)}' not found.");
362362
}
363363

364+
[Fact]
365+
public void RentPropertyAs_ShouldReturnProperty_WhenDataIsValid()
366+
{
367+
// Arrange
368+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
369+
var bindingContext = new ParentBindingContext();
370+
371+
var nestedPropertyBindingData =
372+
nameof(ParentBindingContext.NestedPropertyBindingContext).ToPropertyBindingData();
373+
374+
// Act
375+
var nestedProperty =
376+
objectProvider.RentPropertyAs<IBindingContext>(bindingContext, nestedPropertyBindingData);
377+
378+
// Assert
379+
nestedProperty
380+
.Should()
381+
.NotBeNull()
382+
.And
383+
.BeAssignableTo<IProperty<IBindingContext>>()
384+
.And
385+
.BeAssignableTo<IReadOnlyProperty<IBindingContext>>();
386+
}
387+
388+
[Fact]
389+
public void RentPropertyAs_ShouldReturnValueTypeProperty_WhenDataIsValid()
390+
{
391+
// Arrange
392+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
393+
var bindingContext = new ParentBindingContext();
394+
395+
var floatPropertyBindingData =
396+
nameof(ParentBindingContext.FloatPropertyBindingContext).ToPropertyBindingData();
397+
398+
// Act
399+
var floatProperty = objectProvider.RentPropertyAs<float>(bindingContext, floatPropertyBindingData);
400+
401+
// Assert
402+
floatProperty
403+
.Should()
404+
.NotBeNull()
405+
.And
406+
.BeAssignableTo<IProperty<float>>()
407+
.And
408+
.BeAssignableTo<IReadOnlyProperty<float>>();
409+
}
410+
411+
[Fact]
412+
public void RentPropertyAs_ShouldThrow_WhenTargetTypeIsNotAssignableFromSourceType()
413+
{
414+
// Arrange
415+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
416+
var bindingContext = new ParentBindingContext();
417+
418+
var nestedPropertyBindingData = nameof(ParentBindingContext.NestedProperty).ToPropertyBindingData();
419+
420+
// Assert
421+
objectProvider
422+
.Invoking(sut => sut.RentPropertyAs<IBindingContext>(bindingContext, nestedPropertyBindingData))
423+
.Should()
424+
.Throw<InvalidCastException>()
425+
.WithMessage($"Can not cast the '{typeof(NestedClass)}' to the '{typeof(IBindingContext)}'.");
426+
}
427+
428+
[Fact]
429+
public void RentPropertyAs_ShouldThrow_WheTryingToCastValueTypes()
430+
{
431+
// Arrange
432+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
433+
var bindingContext = new ParentBindingContext();
434+
435+
var floatPropertyBindingData = nameof(ParentBindingContext.FloatPropertyBindingContext).ToPropertyBindingData();
436+
437+
// Assert
438+
objectProvider
439+
.Invoking(sut => sut.RentPropertyAs<int>(bindingContext, floatPropertyBindingData))
440+
.Should()
441+
.Throw<InvalidOperationException>()
442+
.WithMessage(
443+
$"{nameof(ObjectWrapperHandler.GetPropertyAs)} is not supported for value types. Use {typeof(PropertyValueConverter<,>).Name} instead.");
444+
}
445+
364446
[Fact]
365447
public void RentReadOnlyProperty_ShouldReturnProperty_WhenDataIsValid()
366448
{
@@ -418,6 +500,30 @@ public void RentReadOnlyProperty_ShouldConvertPropertyValue_WhenSourceAndTargetT
418500
boolProperty.Value.Should().Be(resultBoolValue);
419501
}
420502

503+
[Fact]
504+
public void RentReadOnlyPropertyAs_ShouldReturnProperty_WhenDataIsValid()
505+
{
506+
// Arrange
507+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
508+
var bindingContext = new ParentBindingContext();
509+
510+
var nestedReadOnlyPropertyBindingData =
511+
nameof(ParentBindingContext.NestedReadOnlyPropertyBindingContext).ToPropertyBindingData();
512+
513+
// Act
514+
var nestedReadOnlyProperty =
515+
objectProvider.RentReadOnlyPropertyAs<IBindingContext>(bindingContext, nestedReadOnlyPropertyBindingData);
516+
517+
// Assert
518+
nestedReadOnlyProperty
519+
.Should()
520+
.NotBeNull()
521+
.And
522+
.BeAssignableTo<IReadOnlyProperty<IBindingContext>>()
523+
.And
524+
.NotBeAssignableTo<IProperty<IBindingContext>>();
525+
}
526+
421527
[Fact]
422528
public void RentReadOnlyProperty_ShouldThrow_WhenPropertyTypeIsWrong()
423529
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using UnityMvvmToolkit.Core.Interfaces;
2+
3+
namespace UnityMvvmToolkit.Test.Integration.TestBindingContext;
4+
5+
public class NestedBindingContext : IBindingContext
6+
{
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace UnityMvvmToolkit.Test.Integration.TestBindingContext;
2+
3+
public class NestedClass
4+
{
5+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using UnityMvvmToolkit.Core;
2+
using UnityMvvmToolkit.Core.Interfaces;
3+
4+
namespace UnityMvvmToolkit.Test.Integration.TestBindingContext;
5+
6+
public class ParentBindingContext : IBindingContext
7+
{
8+
public ParentBindingContext()
9+
{
10+
FloatPropertyBindingContext = new Property<float>();
11+
NestedProperty = new Property<NestedClass>();
12+
13+
NestedPropertyBindingContext = new Property<NestedBindingContext>(new NestedBindingContext());
14+
NestedReadOnlyPropertyBindingContext = new ReadOnlyProperty<NestedBindingContext>(new NestedBindingContext());
15+
}
16+
17+
public IProperty<float> FloatPropertyBindingContext { get; }
18+
public IProperty<NestedClass> NestedProperty { get; }
19+
public IProperty<NestedBindingContext> NestedPropertyBindingContext { get; }
20+
public IReadOnlyProperty<NestedBindingContext> NestedReadOnlyPropertyBindingContext { get; }
21+
}

0 commit comments

Comments
 (0)