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
17 changes: 17 additions & 0 deletions TUnit.Assertions.FSharp/Extensions.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace TUnit.Assertions.FSharp

open TUnit.Assertions.AssertionBuilders

module Extensions =
let check (assertion: IInvokableAssertionBuilder) =
Async.FromContinuations(fun (cont, econt, ccont) ->
let awaiter = assertion.GetAwaiter()

awaiter.OnCompleted(fun () ->
try
if awaiter.IsCompleted then
cont(awaiter.GetResult())
else
ccont (System.OperationCanceledException())
with ex ->
econt ex))
15 changes: 15 additions & 0 deletions TUnit.Assertions.FSharp/TUnit.Assertions.FSharp.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="$(MSBuildThisFileDirectory)TUnit.Assertions.FSharp.props" />
<Import Project="..\Library.props" />

<ItemGroup>
<Compile Include="Extensions.fs" />
<Content Include="TUnit.Assertions.FSharp.props" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TUnit.Assertions\TUnit.Assertions.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions TUnit.Assertions.FSharp/TUnit.Assertions.FSharp.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project>
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsingsMode>auto</ImplicitUsingsMode>
</PropertyGroup>

<ItemGroup>
<Using Include="TUnit.Assertions.Assert" Static="true" />
<Using Include="TUnit.Assertions.FSharp" />
<Using Include="TUnit.Assertions.FSharp.Extensions" />
<Using Include="TUnit.Assertions.FSharp.ValueExtensions" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions TUnit.Assertions/TUnit.Assertions.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
<TUnitAssertionsImplicitUsings>true</TUnitAssertionsImplicitUsings>
</PropertyGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.fsproj'">
<PackageReference Include="TUnit.Assertions.FSharp" Version="*" VersionOverride="*" />
</ItemGroup>
</Project>
31 changes: 29 additions & 2 deletions TUnit.Core/AsyncConvert.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace TUnit.Core;

Expand All @@ -7,6 +9,8 @@ namespace TUnit.Core;
/// </summary>
public static class AsyncConvert
{
private static Type? _fSharpAsyncType;

[MethodImpl(MethodImplOptions.AggressiveInlining
#if NET
| MethodImplOptions.AggressiveOptimization
Expand Down Expand Up @@ -92,6 +96,29 @@ public static ValueTask ConvertObject(object? invoke)
return valueTask;
}

throw new ArgumentException("Invalid object type");
var type = invoke.GetType();
if (type.IsGenericType
&& type.GetGenericTypeDefinition().FullName == "Microsoft.FSharp.Control.FSharpAsync`1")
{
#pragma warning disable
return StartAsFSharpTask(invoke, type);
#pragma warning restore
}

throw new ArgumentException("Invalid object type: " + type.Name, nameof(invoke));
}

[RequiresDynamicCode("Dynamic code is required to call F# async methods.")]
[RequiresUnreferencedCode("Dynamic code is required to call F# async methods.")]
private static ValueTask StartAsFSharpTask(object invoke, Type type)
{
var startAsTaskOpenGenericMethod = (_fSharpAsyncType ??= type.Assembly.GetType("Microsoft.FSharp.Control.FSharpAsync"))!
.GetRuntimeMethods()
.First(m => m.Name == "StartAsTask");

var fSharpTask = (Task)startAsTaskOpenGenericMethod.MakeGenericMethod(type.GetGenericArguments()[0])
.Invoke(null, [invoke, null, null])!;

return new ValueTask(fSharpTask);
}
}
1 change: 1 addition & 0 deletions TUnit.Pipeline/Modules/GetPackageProjectsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class GetPackageProjectsModule : Module<List<File>>
return
[
Sourcy.DotNet.Projects.TUnit_Assertions,
Sourcy.DotNet.Projects.TUnit_Assertions_FSharp,
Sourcy.DotNet.Projects.TUnit_Core,
Sourcy.DotNet.Projects.TUnit_Engine,
Sourcy.DotNet.Projects.TUnit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.1.0" />
<Sdk Name="Aspire.AppHost.Sdk" Version="9.2.1" />

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
5 changes: 5 additions & 0 deletions TUnit.TestProject.FSharp/TUnit.TestProject.FSharp.fsproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<Import Project="..\TestProject.props" />

<ItemGroup>
<ProjectReference Include="..\TUnit.Assertions.FSharp\TUnit.Assertions.FSharp.fsproj" />
<ProjectReference Include="..\TUnit\TUnit.csproj" />
</ItemGroup>

Expand Down
14 changes: 13 additions & 1 deletion TUnit.TestProject.FSharp/Tests.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
namespace TUnit.TestProject.FSharp

open System
open TUnit.Assertions
open TUnit.Assertions.Extensions
open TUnit.Assertions.FSharp.Extensions
open TUnit.Core

type Tests() =
[<Test>]
member this.Test() =
printfn "Test method executed"
printfn "Test method executed"

#if NETCOREAPP
[<Test>]
member this.TestAsync() = async {
let result = 1 + 1
do! check (Assert.That(result).IsPositive())
}
#endif
3 changes: 3 additions & 0 deletions TUnit.TestProject.Library/Models/Dummy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace TUnit.TestProject.Library.Models;

public record Dummy;
16 changes: 15 additions & 1 deletion TUnit.TestProject.VB.NET/Program.vb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
Imports TUnit.Assertions
Imports TUnit.Assertions.Extensions
Imports TUnit.Core

Public Class Tests
<Test>
Public Sub Test()
TestContext.Current.OutputWriter.WriteLine("Test method executed")
End Sub
End Class

#If Not NETFRAMEWORK Then
<Test>
Public Async Function TestAsync() As Task
Dim result = 1 + 1
Await Assert.That(result).IsNegative()
Dim assertionResult = Await Assert.That(result).IsPositive()
Console.WriteLine(assertionResult)
End Function
#End If

End Class

6 changes: 6 additions & 0 deletions TUnit.sln
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TUnit.TestProject.FSharp",
EndProject
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "TUnit.TestProject.VB.NET", "TUnit.TestProject.VB.NET\TUnit.TestProject.VB.NET.vbproj", "{E0F6713B-3F3E-4335-B65F-69C9DE8FBA7C}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TUnit.Assertions.FSharp", "TUnit.Assertions.FSharp\TUnit.Assertions.FSharp.fsproj", "{D283B604-1860-4A6D-800A-A51849ACFEAF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -291,6 +293,10 @@ Global
{E0F6713B-3F3E-4335-B65F-69C9DE8FBA7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0F6713B-3F3E-4335-B65F-69C9DE8FBA7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0F6713B-3F3E-4335-B65F-69C9DE8FBA7C}.Release|Any CPU.Build.0 = Release|Any CPU
{D283B604-1860-4A6D-800A-A51849ACFEAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D283B604-1860-4A6D-800A-A51849ACFEAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D283B604-1860-4A6D-800A-A51849ACFEAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D283B604-1860-4A6D-800A-A51849ACFEAF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions TestProject.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<Import Project="$(MSBuildThisFileDirectory)TUnit.Engine\TUnit.Engine.props" />
<Import Project="$(MSBuildThisFileDirectory)TUnit.Assertions\TUnit.Assertions.props" />

<Import Project="$(MSBuildThisFileDirectory)TUnit.Assertions.FSharp\TUnit.Assertions.FSharp.props"
Condition="'$(MSBuildProjectExtension)' == '.fsproj'" />

<PropertyGroup>
<TargetFrameworks>net472;net8.0;net9.0</TargetFrameworks>

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/tutorial-assertions/congratulations.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 8
sidebar_position: 99
---

# Congratulations
Expand Down
20 changes: 20 additions & 0 deletions docs/docs/tutorial-assertions/fsharp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
sidebar_position: 10
---

# FSharp

As awaiting doesn't work quite the same in F#, the syntax instead looks like this:

```csharp
do! check Assert.That(...).IsSomething()
```

So a test could look like:

```csharp
member this.CheckPositive() = async {
let result = 1 + 1
do! check (Assert.That(result).IsPositive())
}
```