Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,19 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
return null;
}

// Extract optional negated method name
// Extract optional negated method name and overload resolution priority
string? negatedMethodName = null;
int overloadPriority = 0; // Default to 0
foreach (var namedArg in attributeData.NamedArguments)
{
if (namedArg.Key == "NegatedMethodName")
{
negatedMethodName = namedArg.Value.Value?.ToString();
}
else if (namedArg.Key == "OverloadResolutionPriority" && namedArg.Value.Value is int priority)
{
overloadPriority = priority;
}
}

// Find the base Assertion<T> type to extract the type parameter
Expand All @@ -92,7 +97,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
methodName!,
negatedMethodName,
assertionBaseType,
constructors
constructors,
overloadPriority
);
}

Expand Down Expand Up @@ -288,6 +294,12 @@ private static void GenerateExtensionMethod(
sourceBuilder.AppendLine($" /// Extension method for {assertionType.Name}.");
sourceBuilder.AppendLine(" /// </summary>");

// Add OverloadResolutionPriority attribute only if priority > 0
if (data.OverloadResolutionPriority > 0)
{
sourceBuilder.AppendLine($" [global::System.Runtime.CompilerServices.OverloadResolutionPriority({data.OverloadResolutionPriority})]");
}

// Method declaration
var returnType = assertionType.IsGenericType
? $"{assertionType.Name}{genericParamsString}"
Expand Down Expand Up @@ -393,6 +405,7 @@ private record AssertionExtensionData(
string MethodName,
string? NegatedMethodName,
INamedTypeSymbol AssertionBaseType,
ImmutableArray<IMethodSymbol> Constructors
ImmutableArray<IMethodSymbol> Constructors,
int OverloadResolutionPriority
);
}
4 changes: 2 additions & 2 deletions TUnit.Assertions.Tests/AssertMultipleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ await Assert.That(async () =>

await Assert.That(3).IsEqualTo(6);
}
}).Throws<Exception>().And.HasMessageContaining("Expected to be equal to 2");
}).Throws<Exception>().And.HasMessageContaining("Expected to be 2");
}

[Test]
Expand Down Expand Up @@ -51,7 +51,7 @@ public async Task Caught_Exception_In_Scope_Is_Not_Captured()
}).Throws<Exception>();

await Assert.That(exception!.Message)
.Contains("Expected to be equal to 2");
.Contains("Expected to be 2");
}

[Test]
Expand Down
32 changes: 16 additions & 16 deletions TUnit.Assertions.Tests/EquatableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task Wrapper_IsEqualTo_Long_Success()
Wrapper wrapper = new() { Value = 42 };

// Act & Assert
await Assert.That(wrapper).IsEqualTo(42L);
await Assert.That(wrapper).IsEquatableTo(42L);
}

[Test]
Expand All @@ -88,7 +88,7 @@ public async Task Wrapper_IsEqualTo_Long_Failure()
// Act & Assert
await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(async () =>
{
await Assert.That(wrapper).IsEqualTo(99L);
await Assert.That(wrapper).IsEquatableTo(99L);
});
}

Expand All @@ -110,7 +110,7 @@ public async Task NullableWrapper_IsEqualTo_Long_Success()
Wrapper? wrapper = new Wrapper { Value = 42 };

// Act & Assert
await Assert.That(wrapper).IsEqualTo(42L);
await Assert.That(wrapper).IsEquatableTo(42L);
}

[Test]
Expand All @@ -122,7 +122,7 @@ public async Task NullableWrapper_IsEqualTo_Long_NullFailure()
// Act & Assert
await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(async () =>
{
await Assert.That(wrapper).IsEqualTo(42L);
await Assert.That(wrapper).IsEquatableTo(42L);
});
}

Expand All @@ -133,7 +133,7 @@ public async Task MultiEquatable_IsEqualTo_Int_Success()
MultiEquatable value = new() { IntValue = 100, StringValue = "test" };

// Act & Assert
await Assert.That(value).IsEqualTo(100);
await Assert.That(value).IsEquatableTo(100);
}

[Test]
Expand All @@ -143,7 +143,7 @@ public async Task MultiEquatable_IsEqualTo_String_Success()
MultiEquatable value = new() { IntValue = 100, StringValue = "test" };

// Act & Assert
await Assert.That(value).IsEqualTo("test");
await Assert.That(value).IsEquatableTo("test");
}

[Test]
Expand All @@ -153,7 +153,7 @@ public async Task IntWrapper_IsEqualTo_Int_Success()
IntWrapper wrapper = new() { Value = 123 };

// Act & Assert
await Assert.That(wrapper).IsEqualTo(123);
await Assert.That(wrapper).IsEquatableTo(123);
}

[Test]
Expand All @@ -165,7 +165,7 @@ public async Task IntWrapper_IsEqualTo_Int_Failure()
// Act & Assert
await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(async () =>
{
await Assert.That(wrapper).IsEqualTo(456);
await Assert.That(wrapper).IsEquatableTo(456);
});
}

Expand All @@ -176,7 +176,7 @@ public async Task EquatableAssertion_WithAnd_Success()
Wrapper wrapper = new() { Value = 42 };

// Act & Assert
await Assert.That(wrapper).IsEqualTo(42L).And.IsEqualTo(new Wrapper { Value = 42 });
await Assert.That(wrapper).IsEquatableTo(42L).And.IsEqualTo(new Wrapper { Value = 42 });
}

[Test]
Expand All @@ -186,7 +186,7 @@ public async Task EquatableAssertion_WithOr_Success()
Wrapper wrapper = new() { Value = 42 };

// Act & Assert - should pass because first condition is true
await Assert.That(wrapper).IsEqualTo(42L).Or.IsEqualTo(new Wrapper { Value = 99 });
await Assert.That(wrapper).IsEquatableTo(42L).Or.IsEqualTo(new Wrapper { Value = 99 });
}

[Test]
Expand All @@ -198,7 +198,7 @@ public async Task EquatableAssertion_WithAnd_Failure()
// Act & Assert
await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(async () =>
{
await Assert.That(wrapper).IsEqualTo(42L).And.IsEqualTo(new Wrapper { Value = 99 });
await Assert.That(wrapper).IsEquatableTo(42L).And.IsEqualTo(new Wrapper { Value = 99 });
});
}

Expand All @@ -209,7 +209,7 @@ public async Task EquatableAssertion_ChainedWithAndContinuation()
Wrapper wrapper = new() { Value = 42 };

// Act & Assert - test that And continuation works
await Assert.That(wrapper).IsEqualTo(42L).And.IsEqualTo(42L);
await Assert.That(wrapper).IsEquatableTo(42L).And.IsEquatableTo(42L);
}

[Test]
Expand All @@ -219,7 +219,7 @@ public async Task EquatableAssertion_ChainedWithOrContinuation()
Wrapper wrapper = new() { Value = 42 };

// Act & Assert - test that Or continuation works
await Assert.That(wrapper).IsEqualTo(99L).Or.IsEqualTo(42L);
await Assert.That(wrapper).IsEquatableTo(99L).Or.IsEquatableTo(42L);
}

[Test]
Expand All @@ -229,7 +229,7 @@ public async Task NullableIntWrapper_IsEqualTo_Int_Success()
IntWrapper? wrapper = new IntWrapper { Value = 789 };

// Act & Assert
await Assert.That(wrapper).IsEqualTo(789);
await Assert.That(wrapper).IsEquatableTo(789);
}

[Test]
Expand All @@ -241,7 +241,7 @@ public async Task NullableIntWrapper_IsEqualTo_Int_NullFailure()
// Act & Assert
await Assert.ThrowsAsync<TUnit.Assertions.Exceptions.AssertionException>(async () =>
{
await Assert.That(wrapper).IsEqualTo(789);
await Assert.That(wrapper).IsEquatableTo(789);
});
}

Expand All @@ -262,7 +262,7 @@ public async Task GitHubIssue2972_Example()
{
// This is the exact example from the GitHub issue
Wrapper value = new() { Value = 1 };
await Assert.That(value).IsEqualTo(1L);
await Assert.That(value).IsEquatableTo(1L);
}
}
#endif
44 changes: 22 additions & 22 deletions TUnit.Assertions.Tests/Old/AssertMultipleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ public async Task MultipleFailures()
var exception5 = (TUnitAssertionException) aggregateException.InnerExceptions[4];

await TUnitAssert.That(exception1.Message).IsEqualTo("""
Expected to be equal to 2
Expected to be 2
but found 1

at Assert.That(1).IsEqualTo(2)
""");

await TUnitAssert.That(exception2.Message).IsEqualTo("""
Expected to be equal to 3
Expected to be 3
but found 2

at Assert.That(2).IsEqualTo(3)
""");

await TUnitAssert.That(exception3.Message).IsEqualTo("""
Expected to be equal to 4
Expected to be 4
but found 3

at Assert.That(3).IsEqualTo(4)
""");

await TUnitAssert.That(exception4.Message).IsEqualTo("""
Expected to be equal to 5
Expected to be 5
but found 4

at Assert.That(4).IsEqualTo(5)
""");

await TUnitAssert.That(exception5.Message).IsEqualTo("""
Expected to be equal to 6
Expected to be 6
but found 5

at Assert.That(5).IsEqualTo(6)
Expand Down Expand Up @@ -88,40 +88,40 @@ public async Task MultipleFailures_With_Connectors()
var exception5 = (TUnitAssertionException) aggregateException.InnerExceptions[4];

await TUnitAssert.That(exception1.Message).IsEqualTo("""
Expected to be equal to 2
or to be equal to 3
Expected to be 2
or to be 3
but found 1

at Assert.That(1).IsEqualTo(2).Or.IsEqualTo(3)
""");

await TUnitAssert.That(exception2.Message).IsEqualTo("""
Expected to be equal to 3
and to be equal to 4
Expected to be 3
and to be 4
but found 2

at Assert.That(2).IsEqualTo(3).And.IsEqualTo(4)
""");

await TUnitAssert.That(exception3.Message).IsEqualTo("""
Expected to be equal to 4
or to be equal to 5
Expected to be 4
or to be 5
but found 3

at Assert.That(3).IsEqualTo(4).Or.IsEqualTo(5)
""");

await TUnitAssert.That(exception4.Message).IsEqualTo("""
Expected to be equal to 5
and to be equal to 6
Expected to be 5
and to be 6
but found 4

at Assert.That(4).IsEqualTo(5).And.IsEqualTo(6)
""");

await TUnitAssert.That(exception5.Message).IsEqualTo("""
Expected to be equal to 6
or to be equal to 7
Expected to be 6
or to be 7
but found 5

at Assert.That(5).IsEqualTo(6).Or.IsEqualTo(7)
Expand Down Expand Up @@ -172,49 +172,49 @@ public async Task Nested_Multiples()
var assertionException7 = (TUnitAssertionException) aggregateException.InnerExceptions[6];

await TUnitAssert.That(assertionException1.Message).IsEqualTo("""
Expected to be equal to 2
Expected to be 2
but found 1

at Assert.That(1).IsEqualTo(2)
""");

await TUnitAssert.That(assertionException2.Message).IsEqualTo("""
Expected to be equal to 3
Expected to be 3
but found 2

at Assert.That(2).IsEqualTo(3)
""");

await TUnitAssert.That(assertionException3.Message).IsEqualTo("""
Expected to be equal to 4
Expected to be 4
but found 3

at Assert.That(3).IsEqualTo(4)
""");

await TUnitAssert.That(assertionException4.Message).IsEqualTo("""
Expected to be equal to 5
Expected to be 5
but found 4

at Assert.That(4).IsEqualTo(5)
""");

await TUnitAssert.That(assertionException5.Message).IsEqualTo("""
Expected to be equal to 6
Expected to be 6
but found 5

at Assert.That(5).IsEqualTo(6)
""");

await TUnitAssert.That(assertionException6.Message).IsEqualTo("""
Expected to be equal to 7
Expected to be 7
but found 6

at Assert.That(6).IsEqualTo(7)
""");

await TUnitAssert.That(assertionException7.Message).IsEqualTo("""
Expected to be equal to 8
Expected to be 8
but found 7

at Assert.That(7).IsEqualTo(8)
Expand Down
6 changes: 3 additions & 3 deletions TUnit.Assertions.Tests/Old/EqualityComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public async Task ComparerTestFailureAsync()
{
const double a = 10;
const double b = 0;
// Old API used to accept IEqualityComparer, new API requires using the comparer directly
await TUnitAssert.That(new Comparer().Equals(a, b)).IsTrue();
// With IEqualityComparer support restored, we can use it directly
await TUnitAssert.That(a).IsEqualTo(b, new Comparer());
}

[Test]
public async Task ComparerTestSuccessAsync()
{
const double a = 10;
const double b = 0;
await TUnitAssert.That(new Comparer().Equals(a, b)).IsTrue();
await TUnitAssert.That(a).IsEqualTo(b, new Comparer());
}
}
Loading
Loading