Skip to content

Commit 36bc40c

Browse files
authored
F# ASP.NET templates (#2429)
* created f# template * completed f# ASP template tests * updated FSharp.AspNet template config * Added FSharp.Asp template test * removed reference to TUnit.Assertions in FSharp ASP templates
1 parent 2ad27fd commit 36bc40c

File tree

25 files changed

+497
-0
lines changed

25 files changed

+497
-0
lines changed

TUnit.Templates.Tests/AspNetTemplateTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ public async Task InstantiationTest()
99
{
1010
await Engine.Execute(Options).ConfigureAwait(false);
1111
}
12+
13+
[Test]
14+
public async Task InstantiationTestWithFSharp()
15+
{
16+
TemplateShortName = "TUnit.AspNet.FSharp";
17+
await Engine.Execute(Options).ConfigureAwait(false);
18+
}
1219
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TUnit.AspNet.FSharp
2+
3+
// Here you could define global logic that would affect all tests
4+
5+
// You can use attributes at the assembly level to apply to all tests in the assembly
6+
[<assembly: System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage>]
7+
do ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<LangVersion>latest</LangVersion>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<OutputType>Exe</OutputType>
8+
<TargetFramework>net9.0</TargetFramework>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="1.0.0" />
13+
<PackageReference Include="TUnit" Version="1.0.0" />
14+
<PackageReference Include="TUnit.Assertions.FSharp" Version="1.0.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Compile Include="WebApplicationFactory.fs" />
19+
<Compile Include="GlobalSetup.fs" />
20+
<Compile Include="Tests.fs" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\WebApp\WebApp.fsproj" />
25+
</ItemGroup>
26+
27+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace TUnit.AspNet.FSharp
2+
3+
open System
4+
open System.Threading.Tasks
5+
open TUnit
6+
open TUnit.Core
7+
open TUnit.Assertions
8+
open TUnit.Assertions.FSharp.Operations
9+
open TUnit.Assertions.Extensions
10+
11+
type Tests() =
12+
13+
[<ClassDataSource(typeof<WebApplicationFactory>, Shared = SharedType.PerTestSession)>]
14+
member val public WebApplicationFactory: WebApplicationFactory = Unchecked.defaultof<WebApplicationFactory> with get, set
15+
16+
[<Test>]
17+
member this.Test() =
18+
async {
19+
let client = this.WebApplicationFactory.CreateClient()
20+
let! response = client.GetAsync("/ping") |> Async.AwaitTask
21+
let! stringContent = response.Content.ReadAsStringAsync() |> Async.AwaitTask
22+
do! check (Assert.That<string>(stringContent).IsEqualTo("Hello, World!"))
23+
}
24+
25+
[<Test>]
26+
member this.GetWeatherForecast() =
27+
async {
28+
let client = this.WebApplicationFactory.CreateClient()
29+
let! response = client.GetAsync("/weatherforecast") |> Async.AwaitTask
30+
let! stringContent = response.Content.ReadAsStringAsync() |> Async.AwaitTask
31+
do! check (Assert.That<string>(stringContent).IsNotNullOrEmpty())
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace TUnit.AspNet.FSharp
2+
3+
open System.Threading.Tasks
4+
open Microsoft.AspNetCore.Mvc.Testing
5+
open TUnit.Core.Interfaces
6+
7+
type WebApplicationFactory() =
8+
inherit WebApplicationFactory<WebApp.Program.Program>()
9+
10+
interface IAsyncInitializer with
11+
member this.InitializeAsync() : Task =
12+
let _ = this.Server
13+
Task.CompletedTask
14+
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace WebApp.Controllers
2+
3+
open System
4+
open System.Collections.Generic
5+
open System.Linq
6+
open System.Threading.Tasks
7+
open Microsoft.AspNetCore.Mvc
8+
open Microsoft.Extensions.Logging
9+
open WebApp
10+
11+
[<ApiController>]
12+
[<Route("[controller]")>]
13+
type WeatherForecastController (logger : ILogger<WeatherForecastController>) =
14+
inherit ControllerBase()
15+
16+
let summaries =
17+
[|
18+
"Freezing"
19+
"Bracing"
20+
"Chilly"
21+
"Cool"
22+
"Mild"
23+
"Warm"
24+
"Balmy"
25+
"Hot"
26+
"Sweltering"
27+
"Scorching"
28+
|]
29+
30+
[<HttpGet>]
31+
member _.Get() =
32+
let rng = System.Random()
33+
[|
34+
for index in 0..4 ->
35+
{ Date = DateTime.Now.AddDays(float index)
36+
TemperatureC = rng.Next(-20,55)
37+
Summary = summaries.[rng.Next(summaries.Length)] }
38+
|]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace WebApp
2+
#nowarn "20"
3+
open System
4+
open System.Collections.Generic
5+
open System.IO
6+
open System.Linq
7+
open System.Threading.Tasks
8+
open Microsoft.AspNetCore
9+
open Microsoft.AspNetCore.Builder
10+
open Microsoft.AspNetCore.Hosting
11+
open Microsoft.AspNetCore.HttpsPolicy
12+
open Microsoft.Extensions.Configuration
13+
open Microsoft.Extensions.DependencyInjection
14+
open Microsoft.Extensions.Hosting
15+
open Microsoft.Extensions.Logging
16+
17+
module Program =
18+
type Program = class end
19+
let exitCode = 0
20+
21+
[<EntryPoint>]
22+
let main (args: string[]) =
23+
let builder = WebApplication.CreateBuilder(args)
24+
25+
builder.Services.AddEndpointsApiExplorer() |> ignore
26+
27+
builder.Services.AddControllers() |> ignore
28+
29+
let app = builder.Build()
30+
31+
app.UseHttpsRedirection()
32+
33+
app.UseAuthorization()
34+
app.MapControllers()
35+
36+
app.MapGet("/ping", Func<string>(fun () -> "Hello, World!")) |> ignore
37+
38+
app.Run()
39+
40+
exitCode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": true,
8+
"launchUrl": "weatherforecast",
9+
"applicationUrl": "http://localhost:5112",
10+
"environmentVariables": {
11+
"ASPNETCORE_ENVIRONMENT": "Development"
12+
}
13+
},
14+
"https": {
15+
"commandName": "Project",
16+
"dotnetRunMessages": true,
17+
"launchBrowser": true,
18+
"launchUrl": "weatherforecast",
19+
"applicationUrl": "https://localhost:7000;http://localhost:5112",
20+
"environmentVariables": {
21+
"ASPNETCORE_ENVIRONMENT": "Development"
22+
}
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace WebApp
2+
3+
open System
4+
5+
type WeatherForecast =
6+
{ Date: DateTime
7+
TemperatureC: int
8+
Summary: string }
9+
10+
member this.TemperatureF =
11+
32.0 + (float this.TemperatureC / 0.5556)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Compile Include="WeatherForecast.fs" />
9+
<Compile Include="Controllers/WeatherForecastController.fs" />
10+
<Compile Include="Program.fs" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)