11using System ;
22using System . Collections . Generic ;
3- using System . Linq ;
43using System . Linq . Expressions ;
5- using System . Reflection ;
6- using Ploeh . AutoFixture ;
74using TestStack . Dossier . Lists ;
5+ using TestStack . Dossier . ObjectBuilders ;
86
97namespace TestStack . Dossier
108{
@@ -78,17 +76,9 @@ public static implicit operator List<TObject>(TestDataBuilder<TObject, TBuilder>
7876 /// <returns>The built object</returns>
7977 protected virtual TObject BuildObject ( )
8078 {
81- var model = Any . Fixture . Create < TObject > ( ) ;
82-
83- var properties = Reflector . GetSettablePropertiesFor < TObject > ( ) ;
84- foreach ( var property in properties )
85- {
86- if ( property . CanWrite )
87- {
88- var val = Get ( property . PropertyType , property . Name ) ;
89- property . SetValue ( model , val , null ) ;
90- }
91- }
79+ var model = FactoryRegistry
80+ . Get < AllPropertiesObjectBuilder > ( )
81+ . BuildObject ( this ) ;
9282
9383 return model ;
9484 }
@@ -126,7 +116,7 @@ public TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property, TValue v
126116 /// Gets the recorded value for the given property from {TObject} or an anonymous
127117 /// value if there isn't one specified.
128118 /// </summary>
129- /// <typeparam name="TValue">The type of the property</typeparam>
119+ /// <typeparam name="TValue">The type of the property. </typeparam>
130120 /// <param name="property">A lambda expression specifying the property to retrieve the recorded value for</param>
131121 /// <returns>The recorded value of the property or an anonymous value for it</returns>
132122 public TValue Get < TValue > ( Expression < Func < TObject , TValue > > property )
@@ -137,6 +127,13 @@ public TValue Get<TValue>(Expression<Func<TObject, TValue>> property)
137127 return ( TValue ) _properties [ Reflector . GetPropertyNameFor ( property ) ] ;
138128 }
139129
130+ /// <summary>
131+ /// Gets the recorded value for the given property from {type} or an anonymous
132+ /// value if there isn't one specified.
133+ /// </summary>
134+ /// <param name="type">The type of the property.</param>
135+ /// <param name="propertyName">The property name.</param>
136+ /// <returns></returns>
140137 public object Get ( Type type , string propertyName )
141138 {
142139 if ( ! Has ( propertyName ) )
@@ -216,48 +213,5 @@ protected virtual TChildBuilder GetChildBuilder<TChildObject, TChildBuilder>(Fun
216213 modifier ( childBuilder ) ;
217214 return childBuilder ;
218215 }
219-
220- /// <summary>
221- /// Builds the object using the constructor with the most arguments.
222- /// </summary>
223- /// <returns></returns>
224- protected virtual TObject BuildByConstructor ( )
225- {
226- var longestConstructor = typeof ( TObject )
227- . GetConstructors ( )
228- . OrderByDescending ( x => x . GetParameters ( ) . Length )
229- . FirstOrDefault ( ) ;
230-
231- if ( longestConstructor == null ) throw new ObjectCreationException ( ) ;
232-
233- var parameterValues = longestConstructor
234- . GetParameters ( )
235- . Select ( x => CallGetWithType ( x . Name , x . ParameterType ) ) ;
236-
237- return ( TObject ) longestConstructor . Invoke ( parameterValues . ToArray ( ) ) ;
238- }
239-
240- private object CallGetWithType ( string propertyName , Type propertyType )
241- {
242- // Make a Func<TObj, TPropertyType>
243- var expressionDelegateType = typeof ( Func < , > ) . MakeGenericType ( typeof ( TObject ) , propertyType ) ;
244-
245- // Make an expression parameter of type TObj
246- var tObjParameterType = Expression . Parameter ( typeof ( TObject ) ) ;
247-
248- var valueStoredInBuilder = typeof ( TBuilder )
249- . GetMethods ( )
250- . First ( method => method . Name == "Get" && method . ContainsGenericParameters && method . GetGenericArguments ( ) . Length == 1 )
251- . MakeGenericMethod ( propertyType )
252- . Invoke ( this , new object [ ]
253- {
254- Expression . Lambda (
255- expressionDelegateType ,
256- Expression . Property ( tObjParameterType , propertyName ) ,
257- tObjParameterType )
258- } ) ;
259-
260- return valueStoredInBuilder ;
261- }
262216 }
263217}
0 commit comments