haf / expecto

A smooth testing lib for F#. APIs made for humans! Strong testing methodologies for everyone!
Apache License 2.0
663 stars 96 forks source link

Structural equality for C# objects #389

Open haf opened 4 years ago

haf commented 4 years ago

Feature; support structural equality for C# objects. On the wish-list from

https://twitter.com/ursenzler/status/1277354370229850113

image

Implementation, something like:

module Expect

let equalStructural subject expected message =
  let rec inner (refCounter: ObjectIdGenerator from System Serialization) (name, subjectProperty, expectedProperty) =
    match refCounter.getIdOf item with
    | false, newId when isPrimitive subjectProperty ->
      equal subjectProperty expectedProperty (sprintf "Property '%s' is has value '%A' but was '%A'" name subjectProperty actualProperty)
    | false, _ ->
      inner refCounter (name, ...)
    | true, _ ->
      // cyclic ref
      ()
  let idGen = ObjectIdGenerator()
  propsOf subject
    |> sort
    |> zipWith (propsOf actual)
    // Also check for difference of structure
    |> iter (fun (ps, pa) -> inner idGen (ps.Name, ps.Value, pa.Value) 

Or even better https://github.com/eiriktsarpalis/TypeShape/blob/master/samples/TypeShape.Samples/equality-comparer.fs — internalised