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
52 changes: 36 additions & 16 deletions TUnit.Core.SourceGenerator/CodeGenerationHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,28 @@ public static string GenerateAttributeInstantiation(AttributeData attr, Immutabl
var elementIndex = 0;
var elements = arg.Values.Select(v =>
{
var paramType = parameterTypes != null && elementIndex < parameterTypes.Length
? parameterTypes[elementIndex]
var paramType = parameterTypes != null && elementIndex < parameterTypes.Length
? parameterTypes[elementIndex]
: null;


// Check if the parameter type is decimal or nullable decimal
var underlyingType = paramType?.GetNullableUnderlyingType() ?? paramType;
var isDecimalType = underlyingType?.SpecialType == SpecialType.System_Decimal;

// For decimal parameters with syntax available, use the original text
if (paramType?.SpecialType == SpecialType.System_Decimal &&
if (isDecimalType &&
syntaxArguments != null && syntaxIndex < syntaxArguments.Count)
{
var originalText = syntaxArguments[syntaxIndex].Expression.ToString();
syntaxIndex++;

// Skip special handling for null values
if (originalText == "null")
{
elementIndex++;
return "null";
}

// Check if it's a string literal (starts and ends with quotes)
if (originalText.StartsWith("\"") && originalText.EndsWith("\""))
{
Expand All @@ -114,14 +126,12 @@ public static string GenerateAttributeInstantiation(AttributeData attr, Immutabl
elementIndex++;
return TypedConstantParser.GetRawTypedConstantValue(v, paramType);
}
else
{
// For numeric literals, remove any suffix and add 'm' for decimal
originalText = originalText.TrimEnd('d', 'D', 'f', 'F', 'l', 'L', 'u', 'U', 'm', 'M');
return $"{originalText}m";
}

// For numeric literals, remove any suffix and add 'm' for decimal
originalText = originalText.TrimEnd('d', 'D', 'f', 'F', 'l', 'L', 'u', 'U', 'm', 'M');
return $"{originalText}m";
}

syntaxIndex++;
elementIndex++;
return TypedConstantParser.GetRawTypedConstantValue(v, paramType);
Expand All @@ -132,15 +142,25 @@ public static string GenerateAttributeInstantiation(AttributeData attr, Immutabl
else
{
var paramType = parameterTypes != null && i < parameterTypes.Length ? parameterTypes[i] : null;


// Check if the parameter type is decimal or nullable decimal
var underlyingType = paramType?.GetNullableUnderlyingType() ?? paramType;
var isDecimalType = underlyingType?.SpecialType == SpecialType.System_Decimal;

// For decimal parameters with syntax available, use the original text
if (paramType?.SpecialType == SpecialType.System_Decimal &&
if (isDecimalType &&
syntaxArguments != null && syntaxIndex < syntaxArguments.Count)
{
var originalText = syntaxArguments[syntaxIndex].Expression.ToString();
syntaxIndex++;

// Skip special handling for null values
if (originalText == "null")
{
argStrings.Add("null");
}
// Check if it's a string literal (starts and ends with quotes)
if (originalText.StartsWith("\"") && originalText.EndsWith("\""))
else if (originalText.StartsWith("\"") && originalText.EndsWith("\""))
{
// For string literals, let the normal processing handle it (will use decimal.Parse)
syntaxIndex--; // Back up so normal processing can handle it
Expand All @@ -163,9 +183,9 @@ public static string GenerateAttributeInstantiation(AttributeData attr, Immutabl
else
{
var paramType = parameterTypes != null && i < parameterTypes.Length ? parameterTypes[i] : null;

// For decimal parameters with syntax available, use the original text
if (paramType?.SpecialType == SpecialType.System_Decimal &&
if (paramType?.SpecialType == SpecialType.System_Decimal &&
syntaxArguments != null && syntaxIndex < syntaxArguments.Count)
{
var originalText = syntaxArguments[syntaxIndex].Expression.ToString();
Expand Down
12 changes: 12 additions & 0 deletions TUnit.TestProject/NullableArgumentsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TUnit.TestProject;

public class NullableArgumentsTest
{
[Test]
[Arguments(1, 1)]
[Arguments(1, null)]
public async Task TestCase(decimal v1, decimal? v2)
{
await Assert.That(v1).IsEqualTo(v2.GetValueOrDefault());
}
}
Loading