Skip to content

Commit

Permalink
Migrate to xUnit v3
Browse files Browse the repository at this point in the history
  • Loading branch information
TSRBerry committed Dec 11, 2024
1 parent 3e03c97 commit 459c014
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Moq" Version="4.20.70" />
<PackageVersion Include="NetCoreServer" Version="8.0.7" />
<PackageVersion Include="xunit" Version="2.9.2"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2"/>
<PackageVersion Include="xunit.v3" Version="0.7.0-pre.15"/>
<PackageVersion Include="xunit.runner.visualstudio" Version="3.0.0-pre.49"/>
</ItemGroup>
</Project>
3 changes: 2 additions & 1 deletion RyuSocks.Test.Integration/RyuSocks.Test.Integration.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand All @@ -11,7 +12,7 @@
<PackageReference Include="Docker.DotNet" />
<PackageReference Include="FluentFTP" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
3 changes: 2 additions & 1 deletion RyuSocks.Test/RyuSocks.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand All @@ -17,7 +18,7 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
16 changes: 13 additions & 3 deletions RyuSocks.Test/Utils/EnumDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
using Xunit.v3;

namespace RyuSocks.Test.Utils
{
public class EnumDataAttribute<T> : DataAttribute
where T : struct, Enum
{
public override IEnumerable<object[]> GetData(MethodInfo testMethod)
public override bool SupportsDiscoveryEnumeration() => true;

public override ValueTask<IReadOnlyCollection<ITheoryDataRow>> GetData(MethodInfo testMethod, DisposalTracker disposalTracker)
{
foreach (T value in Enum.GetValues<T>())
T[] values = Enum.GetValues<T>();
TheoryDataRow<T>[] rows = new TheoryDataRow<T>[values.Length];

for (int i = 0; i < values.Length; i++)
{
yield return [value];
rows[i] = new TheoryDataRow<T>(values[i]);
}

return ValueTask.FromResult<IReadOnlyCollection<ITheoryDataRow>>(rows);
}
}
}
15 changes: 10 additions & 5 deletions RyuSocks.Test/Utils/RangeDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
using System.Collections.Generic;
using System.Numerics;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
using Xunit.v3;

namespace RyuSocks.Test.Utils
{
Expand All @@ -26,28 +29,30 @@ public class RangeDataAttribute<T> : DataAttribute
private readonly T _max;
private readonly T[] _extraElements;

public override bool SupportsDiscoveryEnumeration() => true;

public RangeDataAttribute(T min, T max, params T[] extraElements)
{
_min = min;
_max = max;
_extraElements = extraElements;
}

public override IEnumerable<object[]> GetData(MethodInfo testMethod)
public override ValueTask<IReadOnlyCollection<ITheoryDataRow>> GetData(MethodInfo testMethod, DisposalTracker disposalTracker)
{
List<object> data = [];
List<TheoryDataRow<T[]>> data = [];

for (T i = _min; i < _max; i++)
{
data.Add(i);
data.Add(new TheoryDataRow<T[]>([i]));
}

foreach (T element in _extraElements)
{
data.Add(element);
data.Add(new TheoryDataRow<T[]>([element]));
}

return [[data.ToArray()]];
return ValueTask.FromResult<IReadOnlyCollection<ITheoryDataRow>>(data);
}
}
}
20 changes: 13 additions & 7 deletions RyuSocks.Test/Utils/StringDataAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Sdk;
using Xunit.v3;

namespace RyuSocks.Test.Utils
{
public class StringDataAttribute : DataAttribute
{
private const char Char = 'a';
private readonly int _min;
private readonly int _max;
private int _count;

public override bool SupportsDiscoveryEnumeration() => true;

public StringDataAttribute(int min, int max, int count)
{
_min = min;
Expand All @@ -41,9 +47,9 @@ public StringDataAttribute(int firstLength, int secondLength)
_count = 2;
}

public override IEnumerable<object[]> GetData(MethodInfo testMethod)
public override ValueTask<IReadOnlyCollection<ITheoryDataRow>> GetData(MethodInfo testMethod, DisposalTracker disposalTracker)
{
List<object[]> data = [];
List<TheoryDataRow<string>> data = [];
StringBuilder builder = new();
int currentMin = _min;
int currentMax = _max;
Expand All @@ -60,8 +66,8 @@ public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
isMin = false;

builder.Append('a', currentMin);
data.Add([builder.ToString()]);
builder.Append(Char, currentMin);
data.Add(new TheoryDataRow<string>(builder.ToString()));

if (!isMinReverse)
{
Expand Down Expand Up @@ -90,8 +96,8 @@ public override IEnumerable<object[]> GetData(MethodInfo testMethod)
{
isMin = true;

builder.Append('a', currentMax);
data.Add([builder.ToString()]);
builder.Append(Char, currentMax);
data.Add(new TheoryDataRow<string>(builder.ToString()));

if (!isMaxReverse)
{
Expand All @@ -118,7 +124,7 @@ public override IEnumerable<object[]> GetData(MethodInfo testMethod)
}
}

return [.. data];
return ValueTask.FromResult<IReadOnlyCollection<ITheoryDataRow>>(data);
}
}
}

0 comments on commit 459c014

Please sign in to comment.