Skip to content

Provide assertions for IEquatable<T> #2972

Description

@AdrianoAE

I have structs that implement multiple IEquatable<T> but there are no assertions that take advantage of this.

Example:

public readonly struct Wrapper
	: IEquatable<Wrapper>,
	  IEquatable<long>
{
	public long Value { get; init; }

	public override bool Equals(object? obj)
		=> obj is Wrapper other && Equals(other)
			|| obj is long value && Equals(value);

	public bool Equals(Wrapper other)
		=> Value == other.Value;

	public static bool operator ==(Wrapper left, Wrapper right) => left.Equals(right);
	public static bool operator !=(Wrapper left, Wrapper right) => !left.Equals(right);

	public bool Equals(long other)
		=> Value == other;

	public static bool operator ==(Wrapper left, long right) => left.Equals(right);
	public static bool operator !=(Wrapper left, long right) => !left.Equals(right);

	public static bool operator ==(long left, Wrapper right) => left.Equals(right);
	public static bool operator !=(long left, Wrapper right) => !left.Equals(right);
}

Ideally I should be able to write the assertions like:

Wrapper value = new() { Value = 1 };

await Assert.That(value).IsEqualTo(1L);

but currently I need to be explicit

Wrapper wrapper = new() { Value = 1 };

await Assert.That(wrapper.Value).IsEqualTo(1);

I would imagine the API would look something like this:

public static InvokableValueAssertionBuilder<TActual, TExpected> IsEqualTo<TActual, TExpected>
(
	this IValueSource<TActual> valueSource,
	TExpected expected,
	[CallerArgumentExpression(nameof(expected))] string doNotPopulateThisValue1 = null!
)
	where TActual : IEquatable<TExpected>
{
	return valueSource.RegisterAssertion(new EqualsExpectedValueAssertCondition<TActual, TExpected>(expected), [doNotPopulateThisValue1]);
	
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions