question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deep equality for C# types

See original GitHub issue

Description

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

  1. Have a C# defined class like:
public sealed class Persion { 
  public string Name { get; set; }
  public int Age { get; set; }
}
  1. 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))
  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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sergey-tihoncommented, Sep 27, 2022

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

0reactions
CaptnCodrcommented, Jan 13, 2023

@AlexeyRaga I’ll close this here. This is out of our scope.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you compare structs for equality in C?
C provides no language facilities to do this - you have to do it yourself and compare each structure member by member.
Read more >
How to compare structures for equality in C?
This blog post explains the best way to compare two structures (same type) for equality. You will learn from this blog post How...
Read more >
Does C support deep comparison or shallow comparison?
Neither, really. Deep vs. shallow comparisons are only meaningful on structured datatypes (which are a form of aggregate type in C).
Read more >
Equality Comparisons - C# Programming Guide
Value equality means that two objects contain the same value or values. For primitive value types such as int or bool, tests for...
Read more >
C# Journey into struct equality comparison, deep dive
The Equals(Object) implementation provided by ValueType performs a byte-by-byte comparison for value types whose fields are all value types, but ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found