Skip to content

Commit

Permalink
Don't use dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst committed Feb 20, 2025
1 parent 13abb7d commit fd9ac6f
Show file tree
Hide file tree
Showing 27 changed files with 161 additions and 142 deletions.
24 changes: 12 additions & 12 deletions TUnit.Assertions.UnitTests/AssertMultipleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public void MultipleFailures()
});

var exception1 = (TUnitAssertionException)assertionException!.InnerExceptions[0];
var exception2 = (TUnitAssertionException)assertionException!.InnerExceptions[1];
var exception3 = (TUnitAssertionException)assertionException!.InnerExceptions[2];
var exception4 = (TUnitAssertionException)assertionException!.InnerExceptions[3];
var exception5 = (TUnitAssertionException)assertionException!.InnerExceptions[4];
var exception2 = (TUnitAssertionException)assertionException.InnerExceptions[1];
var exception3 = (TUnitAssertionException)assertionException.InnerExceptions[2];
var exception4 = (TUnitAssertionException)assertionException.InnerExceptions[3];
var exception5 = (TUnitAssertionException)assertionException.InnerExceptions[4];

NUnitAssert.That(exception1.Message, Is.EqualTo("""
Expected 1 to be equal to 2
Expand Down Expand Up @@ -84,10 +84,10 @@ public void MultipleFailures_With_Connectors()
});

var exception1 = (TUnitAssertionException)assertionException!.InnerExceptions[0];
var exception2 = (TUnitAssertionException)assertionException!.InnerExceptions[1];
var exception3 = (TUnitAssertionException)assertionException!.InnerExceptions[2];
var exception4 = (TUnitAssertionException)assertionException!.InnerExceptions[3];
var exception5 = (TUnitAssertionException)assertionException!.InnerExceptions[4];
var exception2 = (TUnitAssertionException)assertionException.InnerExceptions[1];
var exception3 = (TUnitAssertionException)assertionException.InnerExceptions[2];
var exception4 = (TUnitAssertionException)assertionException.InnerExceptions[3];
var exception5 = (TUnitAssertionException)assertionException.InnerExceptions[4];

NUnitAssert.That(exception1.Message, Is.EqualTo("""
Expected 1 to be equal to 2
Expand Down Expand Up @@ -163,10 +163,10 @@ public void Nested_Multiples()
NUnitAssert.That(aggregateException!.InnerExceptions[0], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException.InnerExceptions[1], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException.InnerExceptions[2], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException!.InnerExceptions[3], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException!.InnerExceptions[4], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException!.InnerExceptions[5], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException!.InnerExceptions[6], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException.InnerExceptions[3], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException.InnerExceptions[4], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException.InnerExceptions[5], Is.TypeOf<TUnitAssertionException>());
NUnitAssert.That(aggregateException.InnerExceptions[6], Is.TypeOf<TUnitAssertionException>());

var assertionException1 = (TUnitAssertionException)aggregateException.InnerExceptions[0];
var assertionException2 = (TUnitAssertionException)aggregateException.InnerExceptions[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ AssertionMetadata assertionMetadata
.FailIf(actualException is null,
"the exception is null")
.OrFailIf(!string.Equals(actualException!.Message, expectedMessage, stringComparison),
$"{new StringDifference(actualException!.Message, expectedMessage)
$"{new StringDifference(actualException.Message, expectedMessage)
.ToString("it differs at index")}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ AssertionMetadata assertionMetadata
.FailIf(actualException is null,
"the exception is null")
.OrFailIf(!match.Matches(actualException!.Message),
$"found \"{actualException!.Message.ShowNewLines().TruncateWithEllipsis(100)}\"");
$"found \"{actualException.Message.ShowNewLines().TruncateWithEllipsis(100)}\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ AssertionMetadata assertionMetadata
.FailIf(actualException is null,
"the exception is null")
.OrFailIf(!string.Equals(actualException!.ParamName, expectedParamName, stringComparison),
$"{new StringDifference(actualException!.ParamName, expectedParamName)
$"{new StringDifference(actualException.ParamName, expectedParamName)
.ToString("it differs at index")}");
}
}
6 changes: 3 additions & 3 deletions TUnit.Assertions/Extensions/SourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static InvokableValueAssertionBuilder<TActual> RegisterAssertion<TActual>
{
if (!string.IsNullOrEmpty(caller))
{
source.AppendExpression(BuildExpression(caller!, argumentExpressions));
source.AppendExpression(BuildExpression(caller, argumentExpressions));

Check failure on line 16 in TUnit.Assertions/Extensions/SourceExtensions.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (ubuntu-latest)

Possible null reference argument for parameter 'caller' in 'string SourceExtensions.BuildExpression(string caller, string?[] argumentExpressions)'.

Check failure on line 16 in TUnit.Assertions/Extensions/SourceExtensions.cs

View workflow job for this annotation

GitHub Actions / modularpipeline-netframework

Possible null reference argument for parameter 'caller' in 'string SourceExtensions.BuildExpression(string caller, string?[] argumentExpressions)'.

Check failure on line 16 in TUnit.Assertions/Extensions/SourceExtensions.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (windows-latest)

Possible null reference argument for parameter 'caller' in 'string SourceExtensions.BuildExpression(string caller, string?[] argumentExpressions)'.
}

var invokeableAssertionBuilder = source.WithAssertion(assertCondition);
Expand All @@ -36,7 +36,7 @@ public static InvokableDelegateAssertionBuilder RegisterAssertion<TActual>(this
{
if (!string.IsNullOrEmpty(caller))
{
delegateSource.AppendExpression(BuildExpression(caller!, argumentExpressions));
delegateSource.AppendExpression(BuildExpression(caller, argumentExpressions));

Check failure on line 39 in TUnit.Assertions/Extensions/SourceExtensions.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (ubuntu-latest)

Possible null reference argument for parameter 'caller' in 'string SourceExtensions.BuildExpression(string caller, string?[] argumentExpressions)'.

Check failure on line 39 in TUnit.Assertions/Extensions/SourceExtensions.cs

View workflow job for this annotation

GitHub Actions / modularpipeline-netframework

Possible null reference argument for parameter 'caller' in 'string SourceExtensions.BuildExpression(string caller, string?[] argumentExpressions)'.

Check failure on line 39 in TUnit.Assertions/Extensions/SourceExtensions.cs

View workflow job for this annotation

GitHub Actions / modularpipeline (windows-latest)

Possible null reference argument for parameter 'caller' in 'string SourceExtensions.BuildExpression(string caller, string?[] argumentExpressions)'.
}

var source = delegateSource.WithAssertion(assertCondition);
Expand Down Expand Up @@ -72,7 +72,7 @@ private static string BuildExpression(string caller, string?[] argumentExpressio
continue;
}

assertionBuilder.Append(argumentExpression!);
assertionBuilder.Append(argumentExpression);

if (index < argumentExpressions.Length - 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax
}

return SyntaxFactory
.IdentifierName(symbol!.GloballyQualified())
.IdentifierName(symbol.GloballyQualified())
.WithoutTrivia();
}

Expand Down Expand Up @@ -143,7 +143,7 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
{
return SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal(identifierNameSyntax!.Identifier.ValueText)
SyntaxFactory.Literal(identifierNameSyntax.Identifier.ValueText)
);
}

Expand All @@ -157,7 +157,7 @@ public override SyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)

return SyntaxFactory.LiteralExpression(
SyntaxKind.StringLiteralExpression,
SyntaxFactory.Literal(argumentExpression!.ToString())
SyntaxFactory.Literal(argumentExpression.ToString())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static ITypeSymbol GetMethodClass(AttributeData methodDataAttribute, INa
{
if (methodDataAttribute.AttributeClass?.IsGenericType is true)
{
return methodDataAttribute.AttributeClass.TypeArguments[0]!;
return methodDataAttribute.AttributeClass.TypeArguments[0];
}

if (methodDataAttribute.ConstructorArguments.Length is 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,25 @@ public static void Execute(SourceCodeWriter sourceBuilder, HooksDataModel model)

if (model.IsVoid)
{
sourceBuilder.WriteLine($"Body = (classInstance, context, cancellationToken) => (({GetCastType(model.ClassType.IsGenericDefinition(), model.FullyQualifiedTypeName)})classInstance).{model.MethodName}({GetArgs(model)}),");
if (model.ClassType.IsGenericDefinition())
{
sourceBuilder.WriteLine($"Body = (classInstance, context, cancellationToken) => typeof({model.FullyQualifiedTypeName}).GetMethod(\"{model.MethodName}\", [{string.Join(", ", model.ParameterTypes.Select(x => $"typeof({x})"))}]).Invoke(classInstance, {GetArgsOrEmptyArray(model)}),");
}
else
{
sourceBuilder.WriteLine($"Body = (classInstance, context, cancellationToken) => ((typeof({model.FullyQualifiedTypeName}))classInstance).{model.MethodName}({GetArgs(model)}),");
}
}
else
{
sourceBuilder.WriteLine($"AsyncBody = (classInstance, context, cancellationToken) => AsyncConvert.Convert(() => (({GetCastType(model.ClassType.IsGenericDefinition(), model.FullyQualifiedTypeName)})classInstance).{model.MethodName}({GetArgs(model)})),");
if (model.ClassType.IsGenericDefinition())
{
sourceBuilder.WriteLine($"AsyncBody = (classInstance, context, cancellationToken) => await ((global::System.Threading.Tasks.Task) typeof({model.FullyQualifiedTypeName}).GetMethod(\"{model.MethodName}\", [{string.Join(", ", model.ParameterTypes.Select(x => $"typeof({x})"))}]).Invoke(classInstance, {GetArgsOrEmptyArray(model)})),");
}
else
{
sourceBuilder.WriteLine($"AsyncBody = (classInstance, context, cancellationToken) => AsyncConvert.Convert(() => ((typeof({model.FullyQualifiedTypeName}))classInstance).{model.MethodName}({GetArgs(model)})),");
}
}

sourceBuilder.WriteLine($"HookExecutor = {HookExecutorHelper.GetHookExecutor(model.HookExecutor)},");
Expand All @@ -91,8 +105,13 @@ public static void Execute(SourceCodeWriter sourceBuilder, HooksDataModel model)
sourceBuilder.WriteLine("},");
}

private static string GetCastType(bool isGenericDefinition, string fullyQualifiedTypeName)
private static string GetArgsOrEmptyArray(HooksDataModel model)
{
return isGenericDefinition ? "dynamic" : fullyQualifiedTypeName;
if (!model.ParameterTypes.Any())
{
return "[]";
}

return GetArgs(model);
}
}
10 changes: 5 additions & 5 deletions TUnit.Core/Attributes/TestData/ClassDataSourceAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public override IEnumerable<Func<T>> GenerateDataSources(DataGeneratorMetadata d
{
yield return () =>
{
var item = ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
var item = ClassDataSources.Get(dataGeneratorMetadata.TestSessionId)
.Get<T>(Shared, dataGeneratorMetadata.TestClassType, Key, dataGeneratorMetadata);

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestRegistered(
context.TestContext,
ClassDataSources.IsStaticProperty(dataGeneratorMetadata),
Shared,
Expand All @@ -26,7 +26,7 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistere

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestStart(
context,
ClassDataSources.IsStaticProperty(dataGeneratorMetadata),
Shared,
Expand All @@ -36,12 +36,12 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item);
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestEnd(Shared, Key, item);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(Shared, Key, item);
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestEnd(Shared, Key, item);
};

return item;
Expand Down
20 changes: 10 additions & 10 deletions TUnit.Core/Attributes/TestData/ClassDataSourceAttribute_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ public sealed class ClassDataSourceAttribute<
(T1 T, SharedType SharedType, string Key),
(T2 T, SharedType SharedType, string Key)
) itemsWithMetadata = (
ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
ClassDataSources.Get(dataGeneratorMetadata.TestSessionId)
.GetItemForIndex<T1>(0, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata),
ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId)
ClassDataSources.Get(dataGeneratorMetadata.TestSessionId)
.GetItemForIndex<T2>(1, dataGeneratorMetadata.TestClassType, Shared, Keys, dataGeneratorMetadata)
);

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestRegistered += async (_, context) =>
{
var testContext = context.TestContext;

await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestRegistered(
testContext,
false,
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistered(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestRegistered(
testContext,
false,
itemsWithMetadata.Item2.SharedType,
Expand All @@ -48,14 +48,14 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestRegistere

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestStart += async (_, context) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestStart(
context,
false,
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestStart(
context,
false,
itemsWithMetadata.Item2.SharedType,
Expand All @@ -65,25 +65,25 @@ await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestStart(

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestSkipped += async (_, _) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestEnd(
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestEnd(
itemsWithMetadata.Item2.SharedType,
itemsWithMetadata.Item2.Key,
itemsWithMetadata.Item2.T);
};

dataGeneratorMetadata.TestBuilderContext.Current.Events.OnTestEnd += async (_, _) =>
{
await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestEnd(
itemsWithMetadata.Item1.SharedType,
itemsWithMetadata.Item1.Key,
itemsWithMetadata.Item1.T);

await ClassDataSources.Get(dataGeneratorMetadata!.TestSessionId).OnTestEnd(
await ClassDataSources.Get(dataGeneratorMetadata.TestSessionId).OnTestEnd(
itemsWithMetadata.Item2.SharedType,
itemsWithMetadata.Item2.Key,
itemsWithMetadata.Item2.T);
Expand Down
Loading

0 comments on commit fd9ac6f

Please sign in to comment.