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.

[Feature Request] Graph comparison to handle objects that expose their data through user-defined conversion instead of fields/properties

See original GitHub issue

Description

Currently, Should().BeEquivalentTo() does not look at private members.

I happen to work in a codebase where most types have been encapsulated in the following fashion:

record PositiveInteger
{
  readonly int _encapsulated;
  PositiveInteger(int encapsulated)
  {
    if (encapsulated <0)
      throw new ObjectIntegrityException("A PositiveInteger cannot encapsulate a negative number.");

    _encapsulated = encapsulated;
  }

  public static PositiveInteger CreateFromInteger(int i)
  {
    return new PositiveInteger(i);
  }

  // how the data is "publicly exposed": passing a PositiveInteger where an int is expected, works!
  public static implicit operator int(PositiveInteger positiveInteger)
  {
    return positiveInteger._encapsulated;
  }
}

As you can see, the int version of the data is “exposed” not through a field or property, but through a user-defined conversion.

However, because the graph does not look at any private member, it will find that PositiveInteger.CreateFromInteger(1) is in fact equivalent to PositiveInteger.CreateFromInteger(2).

It would be cool to have a way to handle this use case.

Simple repository for reproduction : link

On top of my head, an option such as options => options.IncludePrivateFieldsForObjectsWhose[Namespace/FullName]Matches("regex") may be a good candidate.

Versions

  • fluentassertions v6.7.0
  • dotnet 6

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Melandelcommented, Oct 16, 2022

I managed to make it work.

Leaving this for future readers: custom record collections for native deep equals when using ComparingRecordsByValue (with tests)

0reactions
Melandelcommented, Oct 16, 2022

I had to ask ^^

Thank you for your time and answer!

I will see with the other options left. Maybe some kind of custom collection such as

sealed record Values<T> : IEnumerable<T>
{
	readonly T[] _encapsulated;
	Values(IEnumerable<T> encapsulated)
	{
		_encapsulated = encapsulated.ToArray();
	}
	public static Values<T> From(IEnumerable<T> enumerable)                            => new(enumerable ?? Array.Empty<T>());
	public static Values<T> Gather(params T[] array)                                   => new(array);
	public static Values<T> From(T item)                                               => new(new[] { item });
	public static Values<T> SelectFrom<S>(IEnumerable<S> enumerable, Func<S,T> lambda) => new(enumerable?.Select(lambda));

	public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>)_encapsulated).GetEnumerator();
	IEnumerator IEnumerable.GetEnumerator() => _encapsulated.GetEnumerator();
	public bool Equals(Values<T> other) => _encapsulated.SequenceEqual(other ?? new(Array.Empty<T>()));
	public override int GetHashCode()
	{
		return unchecked(this.Aggregate(19, (h, i) => h * 19 + i.GetHashCode()));
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to update the specified properties for on-premises ...
Error executing request. An Azure Active Directory call was made to keep object in sync between Azure Active Directory and Exchange Online.
Read more >
event resource type - Microsoft Graph v1.0
Identifies the version of the event object. Every time the event is changed, ChangeKey changes as well.
Read more >
Exposing a parameter | Substance 3D Designer
Exposing is the process of making a parameter available for use outside of your current Graph View. When building a Graph, you usually...
Read more >
Essential Chart Types for Data Visualization
In this article, we'll provide an overview of essential chart types that you'll see most frequently offered by visualization tools. With these charts,...
Read more >
Query (Feature Service/Layer)—ArcGIS REST APIs
The query operation returns either a feature set, an array of feature IDs, and/or a result extent.
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