Monday, 15 August 2011

testing - How do I create an Arbitrary for a System.Type? -


I am trying to start my model organizations using FsCheck. Models live in C # and typically begin through the unit framework through their own personal setters. For example (contrived):

  public class model {public string one {get; Private set; } Public int two {get; Private set; }}  

I want to create an FsCheck Generator which automatically uses the generator registered for each property's model. Something like this:

  model model maker = gene (event) = new model () type & lt; Model & gt; .GetProperties () | & Gt; Array.filter (funny p - & gt; p.CanWrite). & Gt; Array.iter (funny p -> NewVal = Arb.generateType p.PropertyType // I believe I can drink it. Set value (event, newwall)) event of return}  

Two things are wrong with it:

  1. Come on! can not be used outside the gene computing expression.
  2. Arb.generateType is not present, and I can not find any way to equal it

Is it possible to make this generator automatic Will you set a private field on your model?

With the power of reflection, everything is possible (or throws at runtime).

  module array = open system. Reflection // This is just a helpful type on which there is reflection. Type Internal GenerateInvoker = Fixed Member Invitation & lt; 'Typ & gt; () = Generate billion & lt; 'Typ & gt; | & Gt; Gen.map box // invokes a common method using a runtime type as the normal sequence. Type typed type (type: type) = typhus & lt; GenerateInvoker & gt; .GetMethod ("Invoke", Binding Flex. Static ||| Binding Falges Nonpolic). Mascogenic Meth ([| type |]). Invoke (empty, [||]):? & Gt; General's & lt; Obj & gt; Model Generator = Gin (Give Event = Enter New Model) (Prop = Type ; GetProperties () | & gt; Array.filter (Fun p - & gt; p.CanWrite) // GEN Builder Implementation, so you can do something to support in Sahara! NewVal = Arb.generateType prop.PropertyType prop.SetValue (event, newVal) event of return} Gen.Sample 1 3 modelGenerator  < / Pre> 

No comments:

Post a Comment