Skip to content

Commit

Permalink
Wider test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyrest committed Aug 14, 2024
1 parent e4d859d commit 42548fd
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
5.0.x
6.0.x
7.0.x
8.0.x
9.0.x
include-prerelease: true
- name: Restore dependencies
run: dotnet restore
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
5.0.x
6.0.x
7.0.x
8.0.x
9.0.x
include-prerelease: true
- name: Restore dependencies
run: dotnet restore
Expand Down
4 changes: 3 additions & 1 deletion FastGenericNew.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Root", "Root", "{F142034C-8
Benchmark_ReferenceType.png = Benchmark_ReferenceType.png
Benchmark_ValueType.png = Benchmark_ValueType.png
FastGenericNew.Shared.props = FastGenericNew.Shared.props
.github\workflows\publish.yml = .github\workflows\publish.yml
README.md = README.md
.github\workflows\tests.yml = .github\workflows\tests.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGenericNew.Tests", "src\FastGenericNew.Tests\FastGenericNew.Tests.csproj", "{7A750C15-4CF3-4F5B-BFA8-AFBB98827770}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGenericNew.Benchmarks", "src\FastGenericNew.Benchmarks\FastGenericNew.Benchmarks.csproj", "{24108B29-8FAD-4080-90B4-0A6A19CE06BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastGenericNew.SourceGenerator.InternalGenerator", "src\FastGenericNew.SourceGenerator.InternalGenerator\FastGenericNew.SourceGenerator.InternalGenerator.csproj", "{33032C07-28EF-4AD6-BE00-32303BB9DA7B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGenericNew.SourceGenerator.InternalGenerator", "src\FastGenericNew.SourceGenerator.InternalGenerator\FastGenericNew.SourceGenerator.InternalGenerator.csproj", "{33032C07-28EF-4AD6-BE00-32303BB9DA7B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
4 changes: 2 additions & 2 deletions src/FastGenericNew.Tests/FastGenericNew.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0;net9.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net48</TargetFrameworks>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
Expand Down
8 changes: 8 additions & 0 deletions src/FastGenericNew.Tests/TestData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -35,6 +38,11 @@ public static class TestData
typeof(nint),
typeof(nuint),

#if NET8_0_OR_GREATER
typeof(Int128),
typeof(UInt128),
#endif

#endregion

typeof(DateTime),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ public class ExceptionsTest
[Test()]
public void ExceptionInterface()
{
if (!ClrAllocator<IEnumerable>.IsSupported) Assert.Ignore("Unsupported");
try
{
ClrAllocator<IEnumerable>.CreateInstance();
Assert.Fail("The expected exception is not thrown.");
}
catch (MissingMethodException e)
catch (Exception e)
{
Assert.IsTrue(e.Message.StartsWith("Cannot create an instance of an interface"));
}
Expand All @@ -20,6 +21,7 @@ public void ExceptionInterface()
[Test()]
public void ExceptionAbstract()
{
if (!ClrAllocator<Stream>.IsSupported) Assert.Ignore("Unsupported");
try
{
ClrAllocator<Stream>.CreateInstance();
Expand All @@ -34,6 +36,7 @@ public void ExceptionAbstract()
[Test()]
public void ExceptionPLString()
{
if (!ClrAllocator<string>.IsSupported) Assert.Ignore("Unsupported");
try
{
ClrAllocator<string>.CreateInstance();
Expand All @@ -49,6 +52,7 @@ public void ExceptionPLString()
[Test()]
public void ExceptionNotFoundNoParameter()
{
if (!ClrAllocator<DemoClassNoParamlessCtor>.IsSupported) Assert.Ignore("Unsupported");
try
{
ClrAllocator<DemoClassNoParamlessCtor>.CreateInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class ReferenceTypes
[Test]
public void Object()
{
if (!ClrAllocator<object>.IsSupported) Assert.Ignore("Unsupported");
var expected = Activator.CreateInstance<object>();
var actual = ClrAllocator<object>.CreateInstance();
Assert.IsTrue(expected.GetType() == actual.GetType());
Expand All @@ -17,6 +18,7 @@ public void Object()
[Parallelizable(ParallelScope.All)]
public void CommonTypes<T>()
{
if (!ClrAllocator<T>.IsSupported) Assert.Ignore("Unsupported");
var expected = Activator.CreateInstance<T>();
var actual = ClrAllocator<T>.CreateInstance();
Assert.AreEqual(expected, actual);
Expand All @@ -26,6 +28,7 @@ public void CommonTypes<T>()
[Parallelizable(ParallelScope.All)]
public void ParallelNew<T>()
{
if (!ClrAllocator<T>.IsSupported) Assert.Ignore("Unsupported");
const int count = 512;
T[] array = new T[count];
Parallel.For(0, count, new ParallelOptions() { MaxDegreeOfParallelism = count }, i =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ValueTypes
[Parallelizable(ParallelScope.All)]
public void CommonTypes<T>()
{
if (!ClrAllocator<T>.IsSupported) Assert.Ignore("Unsupported");
var expected = Activator.CreateInstance<T>();
var actual = FastNew<T>.CompiledDelegate();
Assert.AreEqual(expected, actual);
Expand Down
74 changes: 74 additions & 0 deletions src/FastGenericNew.Tests/Units/GetFastNewTests/ExceptionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace FastGenericNew.Tests.Units.GetFastNewTests;

public class ExceptionsTest
{
[Test()]
public void ExceptionInterface()
{
try
{
FastNew.GetCreateInstance<IEnumerable>(typeof(IEnumerable))();
Assert.Fail("The expected exception is not thrown.");
}
catch (MissingMethodException e)
{
Assert.IsTrue(e.Message.StartsWith("Cannot create an instance of an interface"));
}
}

[Test()]
public void ExceptionAbstract()
{
try
{
FastNew.GetCreateInstance<Stream>(typeof(Stream))();
Assert.Fail("The expected exception is not thrown.");
}
catch (MissingMethodException e)
{
Assert.IsTrue(e.Message.StartsWith("Cannot create an abstract class"));
}
}

[Test()]
public void ExceptionPLString()
{
try
{
FastNew.GetCreateInstance<string>(typeof(string))();
Assert.Fail("The expected exception is not thrown.");
}
catch (MissingMethodException e)
{
Assert.IsTrue(e.Message.StartsWith("No match constructor found in type"));
}
}

[Test()]
public void ExceptionNotFoundNoParameter()
{
try
{
FastNew.GetCreateInstance<DemoClassNoParamlessCtor>(typeof(DemoClassNoParamlessCtor))();
Assert.Fail("The expected exception is not thrown.");
}
catch (MissingMethodException e)
{
Assert.IsTrue(e.Message.StartsWith("No match constructor found in type"));
}
}

[Test()]
public void ExceptionNotFoundWithParameter()
{
try
{
FastNew.GetCreateInstance<DemoClass, DBNull>(typeof(DemoClass), typeof(DBNull))(DBNull.Value);
Assert.Fail("The expected exception is not thrown.");
}
catch (MissingMethodException e)
{
Assert.IsTrue(e.Message.StartsWith("No match constructor found in type"));
}
}
}
13 changes: 13 additions & 0 deletions src/FastGenericNew.Tests/Units/GetFastNewTests/ValueType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace FastGenericNew.Tests.Units.GetFastNewTests;

public class ValueTypes
{
[TestCaseSourceGeneric(typeof(TestData), nameof(TestData.CommonValueTypes))]
[Parallelizable(ParallelScope.All)]
public void CommonTypes<T>()
{
var expected = Activator.CreateInstance<T>();
var actual = FastNew.GetCreateInstance<T>(typeof(T))();
Assert.AreEqual(expected, actual);
}
}

0 comments on commit 42548fd

Please sign in to comment.