Deep equality for C# types
See original GitHub issueDescription
In F# code we often have to use data types that are defined in C#. These types usually don’t have equality defined, and therefore only get referential equality.
It would be useful if FsUnit
would provide a mechanism to still be able to structurally compare these types, similarly to how FluentAssertions
does in its BeEquivalentTo
method for C#:
actual.Should().BeEquivalentTo(expected, "reason")
Repro steps
- Have a C# defined class like:
public sealed class Persion {
public string Name { get; set; }
public int Age { get; set; }
}
- Wrap it in F# union:
type User = User of Person
// Notice that the person inside is "the same"
let userA = User (Person(Name = "a", Age = 1))
let userB = User (Person(Name = "a", Age = 1))
- Compare these two values:
// this says "no"
userA |> should equal userB
Expected behavior
I suggest that it’d be possible to have an operation that would allow the previous example to pass.
It can be named beEquivalentTo
or somehow else.
The operation can detect F# types (or presence for equivalence) and do what equal
does, otherwise it can do what FluentAssertions
does currently (takes a set op public fields and properties and runs comparison on them).
Actual behavior
The given example fails the test
Known workarounds
I can’t come up with a workaround that can work for an arbitrarily nested case.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
FsUnit is an assertion library that supports DSL on top of popular unit testing frameworks. I think that
equality
should be provided by the type (e.g.Person
).Option1: If you need equality for C# types (and cannot define them in F# ☺️) you may search for C# source generator library that will generate proper equality for your types.
Option2: Reference library like DeepEqual and tiny F# layer on top for assertions
@AlexeyRaga I’ll close this here. This is out of our scope.