Skip to content

Commit

Permalink
Update xUnit3002 to support non-generic type-based assertions (only w…
Browse files Browse the repository at this point in the history
…ith typeof(T))
  • Loading branch information
bradwilson committed Jul 22, 2024
1 parent 680e052 commit c1c69d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ public void TheMethod() {
Assert.NotNull(message as string);
Assert.NotNull((string)message);
Assert.Empty(collection.OfType<string>());
Assert.IsType(typeof(string), message);
Assert.IsType<string>(message);
Assert.IsNotType(typeof(string), message);
Assert.IsNotType<string>(message);
Assert.IsAssignableFrom(typeof(string), message);
Assert.IsAssignableFrom<string>(message);
Assert.IsNotAssignableFrom(typeof(string), message);
Assert.IsNotAssignableFrom<string>(message);

// Testing against a serializable type
Expand All @@ -40,9 +44,13 @@ public void TheMethod() {
Assert.NotNull([|message as MyMessage|]);
Assert.NotNull([|(MyMessage)message|]);
Assert.Empty([|collection.OfType<MyMessage>()|]);
[|Assert.IsType(typeof(MyMessage), message)|];
[|Assert.IsType<MyMessage>(message)|];
[|Assert.IsNotType(typeof(MyMessage), message)|];
[|Assert.IsNotType<MyMessage>(message)|];
[|Assert.IsAssignableFrom(typeof(MyMessage), message)|];
[|Assert.IsAssignableFrom<MyMessage>(message)|];
[|Assert.IsNotAssignableFrom(typeof(MyMessage), message)|];
[|Assert.IsNotAssignableFrom<MyMessage>(message)|];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,21 @@ public override void AnalyzeCompilation(
&& method.Parameters.Length == (method.IsExtensionMethod ? 1 : 0))
reportIfMessageType(context, semanticModel, method.TypeArguments[0], invocationOperation.Syntax);

// We also look for type-related calls to Assert
if (assertType is not null
&& matchingAssertions.Contains(method.Name)
&& SymbolEqualityComparer.Default.Equals(assertType, method.ContainingType))
reportIfMessageType(context, semanticModel, method.TypeArguments[0], invocationOperation.Syntax);
{
var testType = default(ITypeSymbol);
if (method.IsGenericMethod && method.TypeArguments.Length == 1)
testType = method.TypeArguments[0];
else if (invocationOperation.Arguments.FirstOrDefault() is IArgumentOperation typeArgumentOperation
&& typeArgumentOperation.Value is ITypeOfOperation typeOfArgumentOperation)
testType = typeOfArgumentOperation.TypeOperand;

if (testType is not null)
reportIfMessageType(context, semanticModel, testType, invocationOperation.Syntax);
}
}, OperationKind.Invocation);

void reportIfMessageType(
Expand Down

0 comments on commit c1c69d6

Please sign in to comment.