@@ -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 {
0 commit comments