Skip to content
  • Iustin Pop's avatar
    Introduce a helper for simple Arbitrary instances · 8492daa3
    Iustin Pop authored
    
    
    After getting really annoyed at yet another "<*> arbitrary", I thought
    that we should be able to automate this, at least for types which are
    simple enough and have already all the "prerequisites".
    
    Hence the new genArbitrary function and its helpers, which can:
    
    - build an arbitrary for Bounded types, using the regular "elements
      [minBound..maxBound]" (20 manually defined cases right now)
    - build an arbitrary instance for single-constructor data types,
      e.g. "data A = A x y z", using a simple "arbitrary = pure A <*>
      arbitrary <*> arbitrary <*> arbitrary"
    - build an arbitrary instance for multi-constructor data types, using
      "arbitrary = oneof [<arbitrary for each individual construct, per
      the previous>]"
    
    Both normal and record-based constructors are supported.
    
    It can also build arbitrary instances for new types and type synonyms,
    although for these last two I'm not so confident on the soundness of
    the instances.
    
    Note that this helper won't work for types which are not well behaved;
    for example, Node has the name as String not as FQDN, so our manually
    written arbitrary instance has just a few overrides as getFQDN instead
    of arbitrary, so we can't automate this particular type yet; this only
    means we get another push to use proper types, instead of primitive
    ones, for fields which have any kinds of restrictions ("what's good
    for arbitrary is good for regular code" too).
    
    Signed-off-by: default avatarIustin Pop <iustin@google.com>
    Reviewed-by: default avatarRené Nussbaumer <rn@google.com>
    8492daa3