@@ -187,25 +187,38 @@ private AttributeGrabBag InitializeGrabBag()
187187
188188 private void AssertAttribute < TPropertyType , TTokenType > ( IResourceTypeRegistration reg , string attributeName ,
189189 JToken tokenToSet , TPropertyType expectedPropertyValue , TTokenType expectedTokenAfterSet , Func < AttributeGrabBag , TPropertyType > getPropertyFunc )
190+ {
191+ AssertAttributeHelper ( reg , attributeName , tokenToSet , grabBag =>
192+ {
193+ var propertyValueAfterSet = getPropertyFunc ( grabBag ) ;
194+ propertyValueAfterSet . Should ( ) . Be ( expectedPropertyValue ) ;
195+ } , token =>
196+ {
197+ if ( expectedTokenAfterSet == null )
198+ token . Should ( ) . BeNull ( ) ;
199+ else
200+ {
201+ var convertedTokenValue = token . Value < TTokenType > ( ) ;
202+ convertedTokenValue . Should ( ) . Be ( expectedTokenAfterSet ) ;
203+ }
204+ } ) ;
205+ }
206+
207+ private void AssertAttributeHelper ( IResourceTypeRegistration reg , string attributeName ,
208+ JToken tokenToSet , Action < AttributeGrabBag > testPropertyValueAfterSet ,
209+ Action < JToken > testTokenAfterSetAndGet )
190210 {
191211 var grabBag = InitializeGrabBag ( ) ;
192212
193213 var field = reg . GetFieldByName ( attributeName ) ;
194- var attribute = ( ResourceTypeAttribute ) field ;
214+ var attribute = ( ResourceTypeAttribute ) field ;
195215 attribute . JsonKey . Should ( ) . Be ( attributeName ) ;
196216
197217 attribute . SetValue ( grabBag , tokenToSet ) ;
198- var propertyValueAfterSet = getPropertyFunc ( grabBag ) ;
199- propertyValueAfterSet . Should ( ) . Be ( expectedPropertyValue ) ;
200-
218+ testPropertyValueAfterSet ( grabBag ) ;
219+
201220 var convertedToken = attribute . GetValue ( grabBag ) ;
202- if ( expectedTokenAfterSet == null )
203- convertedToken . Should ( ) . BeNull ( ) ;
204- else
205- {
206- var convertedTokenValue = convertedToken . Value < TTokenType > ( ) ;
207- convertedTokenValue . Should ( ) . Be ( expectedTokenAfterSet ) ;
208- }
221+ testTokenAfterSetAndGet ( convertedToken ) ;
209222 }
210223
211224 [ TestMethod ]
@@ -733,5 +746,68 @@ public void BuildRegistration_sets_up_correct_attribute_for_nullable_enum_field(
733746 AssertAttribute ( reg , "nullable-enum-field" , ( int ) SampleEnum . Value1 , SampleEnum . Value1 , ( int ) SampleEnum . Value1 , g => g . NullableEnumField ) ;
734747 AssertAttribute ( reg , "nullable-enum-field" , null , null , ( SampleEnum ? ) null , g => g . NullableEnumField ) ;
735748 }
749+
750+ [ TestMethod ]
751+ public void BuildRegistration_sets_up_correct_attribute_for_to_one_complex_field ( )
752+ {
753+ // Arrange
754+ var registrar = new ResourceTypeRegistrar ( new DefaultNamingConventions ( new PluralizationService ( ) ) ) ;
755+
756+ // Act
757+ var reg = registrar . BuildRegistration ( typeof ( AttributeGrabBag ) ) ;
758+
759+ // Assert
760+ AssertAttributeHelper ( reg , "to-one-complex-type-field" ,
761+ new JObject { { "intProp" , 32 } , { "StringProp" , "qux" } } ,
762+ grabBag =>
763+ {
764+ grabBag . ToOneComplexTypeField . Should ( ) . NotBeNull ( ) ;
765+ grabBag . ToOneComplexTypeField . IntProp . Should ( ) . Be ( 32 ) ;
766+ grabBag . ToOneComplexTypeField . StringProp . Should ( ) . Be ( "qux" ) ;
767+ } ,
768+ token =>
769+ {
770+ ( ( int ) token [ "intProp" ] ) . Should ( ) . Be ( 32 ) ;
771+ ( ( string ) token [ "StringProp" ] ) . Should ( ) . Be ( "qux" ) ;
772+ } ) ;
773+ AssertAttribute ( reg , "to-one-complex-type-field" , null , null , ( SampleComplexType ) null , g => g . ToOneComplexTypeField ) ;
774+ }
775+
776+ [ TestMethod ]
777+ public void BuildRegistration_sets_up_correct_attribute_for_to_many_complex_field ( )
778+ {
779+ // Arrange
780+ var registrar = new ResourceTypeRegistrar ( new DefaultNamingConventions ( new PluralizationService ( ) ) ) ;
781+
782+ // Act
783+ var reg = registrar . BuildRegistration ( typeof ( AttributeGrabBag ) ) ;
784+
785+ // Assert
786+ AssertAttributeHelper ( reg , "to-many-complex-type-field" ,
787+ new JArray
788+ {
789+ new JObject { { "intProp" , 49 } , { "StringProp" , "blue" } } ,
790+ new JObject { { "intProp" , 67 } , { "StringProp" , "orange" } }
791+ } ,
792+ grabBag =>
793+ {
794+ var result = grabBag . ToManyComplexTypeField . ToArray ( ) ;
795+ result . Length . Should ( ) . Be ( 2 ) ;
796+ result [ 0 ] . IntProp . Should ( ) . Be ( 49 ) ;
797+ result [ 0 ] . StringProp . Should ( ) . Be ( "blue" ) ;
798+ result [ 1 ] . IntProp . Should ( ) . Be ( 67 ) ;
799+ result [ 1 ] . StringProp . Should ( ) . Be ( "orange" ) ;
800+ } ,
801+ token =>
802+ {
803+ var jarray = ( JArray ) token ;
804+ jarray . Count . Should ( ) . Be ( 2 ) ;
805+ ( ( int ) jarray [ 0 ] [ "intProp" ] ) . Should ( ) . Be ( 49 ) ;
806+ ( ( string ) jarray [ 0 ] [ "StringProp" ] ) . Should ( ) . Be ( "blue" ) ;
807+ ( ( int ) jarray [ 1 ] [ "intProp" ] ) . Should ( ) . Be ( 67 ) ;
808+ ( ( string ) jarray [ 1 ] [ "StringProp" ] ) . Should ( ) . Be ( "orange" ) ;
809+ } ) ;
810+ AssertAttribute ( reg , "to-many-complex-type-field" , null , null , ( SampleComplexType [ ] ) null , g => g . ToManyComplexTypeField ) ;
811+ }
736812 }
737813}
0 commit comments