1+ using FluentAssertions ;
2+ using UnityMvvmToolkit . Core ;
3+ using UnityMvvmToolkit . Core . Converters . ParameterValueConverters ;
4+ using UnityMvvmToolkit . Core . Converters . PropertyValueConverters ;
5+ using UnityMvvmToolkit . Core . Extensions ;
6+ using UnityMvvmToolkit . Core . Interfaces ;
7+ using UnityMvvmToolkit . UnitTests . TestBindingContext ;
8+
9+ namespace UnityMvvmToolkit . UnitTests ;
10+
11+ public class BindingContextObjectProviderTests
12+ {
13+ [ Fact ]
14+ public void RentProperty_ShouldReturnProperty_WhenDataIsValid ( )
15+ {
16+ // Arrange
17+ const int countValue = 69 ;
18+ const string titleValue = "TestTitle" ;
19+
20+ var objectProvider = new BindingContextObjectProvider ( Array . Empty < IValueConverter > ( ) ) ;
21+ var bindingContext = new MyBindingContext ( titleValue )
22+ {
23+ Count = countValue ,
24+ } ;
25+
26+ IProperty < int > countProperty ;
27+ var countPropertyBindingData = nameof ( MyBindingContext . Count ) . ToPropertyBindingData ( ) ;
28+
29+ IReadOnlyProperty < string > titleProperty ;
30+ var titlePropertyBindingData = nameof ( MyBindingContext . Title ) . ToPropertyBindingData ( ) ;
31+
32+ // Act
33+ countProperty = objectProvider . RentProperty < int > ( bindingContext , countPropertyBindingData ) ;
34+ titleProperty = objectProvider . RentReadOnlyProperty < string > ( bindingContext , titlePropertyBindingData ) ;
35+
36+ // Assert
37+ countProperty
38+ . Should ( ) . NotBeNull ( ) . And . BeAssignableTo < IProperty < int > > ( ) . And . BeAssignableTo < IReadOnlyProperty < int > > ( ) ;
39+
40+ titleProperty
41+ . Should ( ) . NotBeNull ( ) . And . BeAssignableTo < IReadOnlyProperty < string > > ( ) ;
42+
43+ countProperty . Value . Should ( ) . Be ( countValue ) ;
44+ titleProperty . Value . Should ( ) . Be ( titleValue ) ;
45+ }
46+
47+ [ Fact ]
48+ public void RentPropertyWithConverter_ShouldReturnProperty_WhenDataIsValid ( )
49+ {
50+ // Arrange
51+ const int countValue = 69 ;
52+
53+ var objectProvider = new BindingContextObjectProvider ( new IValueConverter [ ]
54+ {
55+ new IntToStrConverter ( )
56+ } ) ;
57+
58+ var bindingContext = new MyBindingContext
59+ {
60+ Count = countValue ,
61+ } ;
62+
63+ IProperty < string > countProperty ;
64+ var countPropertyBindingData = nameof ( MyBindingContext . Count ) . ToPropertyBindingData ( ) ;
65+
66+ // Act
67+ countProperty = objectProvider . RentProperty < string > ( bindingContext , countPropertyBindingData ) ;
68+
69+ // Assert
70+ countProperty
71+ . Should ( )
72+ . NotBeNull ( )
73+ . And
74+ . BeAssignableTo < IProperty < string > > ( )
75+ . And
76+ . BeAssignableTo < IReadOnlyProperty < string > > ( ) ;
77+
78+ countProperty . Value . Should ( ) . Be ( countValue . ToString ( ) ) ;
79+ }
80+
81+ [ Fact ]
82+ public void GetCommand_ShouldReturnCommand_WhenDataIsValid ( )
83+ {
84+ // Arrange
85+ var objectProvider = new BindingContextObjectProvider ( Array . Empty < IValueConverter > ( ) ) ;
86+ var bindingContext = new MyBindingContext ( ) ;
87+
88+ ICommand incrementCommand ;
89+ ICommand decrementCommand ;
90+
91+ // Act
92+ incrementCommand =
93+ objectProvider . GetCommand < ICommand > ( bindingContext , nameof ( MyBindingContext . IncrementCommand ) ) ;
94+
95+ decrementCommand =
96+ objectProvider . GetCommand < ICommand > ( bindingContext , nameof ( MyBindingContext . DecrementCommand ) ) ;
97+
98+ // Assert
99+ incrementCommand . Should ( ) . NotBeNull ( ) . And . BeAssignableTo < IBaseCommand > ( ) ;
100+ decrementCommand . Should ( ) . NotBeNull ( ) . And . BeAssignableTo < IBaseCommand > ( ) ;
101+ }
102+
103+ [ Fact ]
104+ public void RentCommandWrapper_ShouldReturnCommand_WhenDataIsValid ( )
105+ {
106+ // Arrange
107+ var objectProvider = new BindingContextObjectProvider ( new IValueConverter [ ]
108+ {
109+ new ParameterToIntConverter ( )
110+ } ) ;
111+
112+ var bindingContext = new MyBindingContext ( ) ;
113+
114+ IBaseCommand setValueCommand ;
115+ var setValueCommandBindingData = $ "{ nameof ( MyBindingContext . SetValueCommand ) } , 5". ToCommandBindingData ( 0 ) ;
116+
117+ // Act
118+ setValueCommand = objectProvider . RentCommandWrapper ( bindingContext , setValueCommandBindingData ) ;
119+
120+ // Assert
121+ setValueCommand . Should ( ) . NotBeNull ( ) ;
122+ }
123+
124+ [ Fact ]
125+ public void RentPropertyWithConverter_ShouldThrow_WhenConverterIsNotSet ( )
126+ {
127+ // Arrange
128+ const int countValue = 69 ;
129+
130+ var objectProvider = new BindingContextObjectProvider ( Array . Empty < IValueConverter > ( ) ) ;
131+ var bindingContext = new MyBindingContext
132+ {
133+ Count = countValue ,
134+ } ;
135+
136+ var countPropertyBindingData = nameof ( MyBindingContext . Count ) . ToPropertyBindingData ( ) ;
137+
138+ // Assert
139+ objectProvider
140+ . Invoking ( objProvider => objProvider . RentProperty < string > ( bindingContext , countPropertyBindingData ) )
141+ . Should ( )
142+ . Throw < NullReferenceException > ( )
143+ . WithMessage ( $ "Property value converter from '{ typeof ( int ) } ' to '{ typeof ( string ) } ' not found.") ;
144+ }
145+
146+ [ Fact ]
147+ public void RentCommandWrapper_ShouldThrow_WhenConverterIsNotSet ( )
148+ {
149+ // Arrange
150+ var objectProvider = new BindingContextObjectProvider ( Array . Empty < IValueConverter > ( ) ) ;
151+ var bindingContext = new MyBindingContext ( ) ;
152+
153+ var setValueCommandBindingData = $ "{ nameof ( MyBindingContext . SetValueCommand ) } , 5". ToCommandBindingData ( 0 ) ;
154+
155+ // Assert
156+ objectProvider
157+ . Invoking ( objProvider => objProvider . RentCommandWrapper ( bindingContext , setValueCommandBindingData ) )
158+ . Should ( )
159+ . Throw < NullReferenceException > ( )
160+ . WithMessage ( $ "Parameter value converter to '{ typeof ( int ) } ' not found.") ;
161+ }
162+ }
0 commit comments