Skip to content

Commit

Permalink
Update ValueToArgumentName to support double and float
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Dec 9, 2024
1 parent 72506df commit 03e06cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add `AppDomainDisposable` internal class to create a new `AppDomain` and dispose it.
- Add `TryChangeType` to try to convert `object` to `ParameterType`.
- Update `GetTestNameWithArguments` to support generic types.
- Update `ValueToArgumentName` to support `double` and `float`.
### Tests
- Add `SampleMetadata` and `SampleMetadataAttribute` test project.
- Add `TestParameterCaseDouble` to test parameter with `double` type and parameter `int`. (Fix: #29)
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.4.1-beta</Version>
<Version>1.4.1-beta.1</Version>
</PropertyGroup>
</Project>
66 changes: 37 additions & 29 deletions ricaun.NUnit/Services/TestAttributeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,42 +179,50 @@ public string GetTestName(MethodInfo method, NUnitAttribute attribute)
return method.Name;
}

private string GetTestNameWithArguments(MethodInfo method, params object[] objects)
static string GetTestNameWithArguments(MethodInfo method, params object[] objects)
{
string ToArgumentName(object value)
return $"{method.Name}({string.Join(",", objects.Select(ValueToArgumentName))})";
}
static string ValueToArgumentName(object value)
{
if (value is null)
{
if (value is null)
{
return "null";
}
else if (value is string)
{
return $"\"{value}\"";
}
else if (value is Type type)
return "null";
}
else if (value is string)
{
return $"\"{value}\"";
}
else if (value is double)
{
return $"{value}d";
}
else if (value is float)
{
return $"{value}f";
}
else if (value is Type type)
{
try
{
try
if (type.IsGenericType)
{
if (type.IsGenericType)
{
var genericTypeName = type.Name.Split('`')[0];
var genericArgs = type
.GetGenericArguments()
.Select(ToArgumentName);
return $"{genericTypeName}<{string.Join(",", genericArgs)}>";
}
var genericTypeName = type.Name.Split('`')[0];
var genericArgs = type
.GetGenericArguments()
.Select(ValueToArgumentName);
return $"{genericTypeName}<{string.Join(",", genericArgs)}>";
}
catch { }
return type.Name;
}
else if (value is ParameterInfo parameter)
{
var parameterType = parameter.ParameterType;
return ToArgumentName(parameterType);
}
return $"{value}";
catch { }
return type.Name;
}
else if (value is ParameterInfo parameter)
{
var parameterType = parameter.ParameterType;
return ValueToArgumentName(parameterType);
}
return $"{method.Name}({string.Join(",", objects.Select(ToArgumentName))})";
return $"{value}";
}

/// <summary>
Expand Down

0 comments on commit 03e06cd

Please sign in to comment.