From af33c7fd6934b841cf87b36bd8b466ba57bff8b3 Mon Sep 17 00:00:00 2001 From: Kevin F Date: Thu, 30 Nov 2023 10:25:42 +0100 Subject: [PATCH] Add runtime information to async calls :sparkles: --- .gitignore | 7 +++-- Fable.Pyxpecto.sln | 15 +++++++---- build/ProjectInfo.fs | 2 +- src/Pyxpecto.fs | 26 ++++++++++++++++--- tests/{ => Mocha.Tests}/Main.fs | 0 .../Mocha.Tests.fsproj} | 5 +--- tests/Mocha.Tests/README.md | 3 +++ 7 files changed, 40 insertions(+), 18 deletions(-) rename tests/{ => Mocha.Tests}/Main.fs (100%) rename tests/{Fable.Pyxpecto.Tests.fsproj => Mocha.Tests/Mocha.Tests.fsproj} (88%) create mode 100644 tests/Mocha.Tests/README.md diff --git a/.gitignore b/.gitignore index 09e2d94..c067d61 100644 --- a/.gitignore +++ b/.gitignore @@ -397,7 +397,6 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml /dist/py -/tests/Fable.Pytest.Tests/js -/tests/Fable.Pytest.Tests/py -/tests/js -/tests/py +/tests/**/js +/tests/**/py +/tests/**/ts diff --git a/Fable.Pyxpecto.sln b/Fable.Pyxpecto.sln index 051ac19..caa9e20 100644 --- a/Fable.Pyxpecto.sln +++ b/Fable.Pyxpecto.sln @@ -16,7 +16,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Build", "build\Build.fsproj", "{F7027BE5-755C-4857-9AD5-2B58953A7FFE}" EndProject -Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Fable.Pyxpecto.Tests", "tests\Fable.Pyxpecto.Tests.fsproj", "{78D65800-18D8-489F-99F9-9157D52F3749}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F6DC93A8-274F-4BC7-8CBD-BEFAE46C9941}" +EndProject +Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Mocha.Tests", "tests\Mocha.Tests\Mocha.Tests.fsproj", "{0C60BD36-F361-4C85-B86B-1BA1A7B05388}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -32,14 +34,17 @@ Global {F7027BE5-755C-4857-9AD5-2B58953A7FFE}.Debug|Any CPU.Build.0 = Debug|Any CPU {F7027BE5-755C-4857-9AD5-2B58953A7FFE}.Release|Any CPU.ActiveCfg = Release|Any CPU {F7027BE5-755C-4857-9AD5-2B58953A7FFE}.Release|Any CPU.Build.0 = Release|Any CPU - {78D65800-18D8-489F-99F9-9157D52F3749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {78D65800-18D8-489F-99F9-9157D52F3749}.Debug|Any CPU.Build.0 = Debug|Any CPU - {78D65800-18D8-489F-99F9-9157D52F3749}.Release|Any CPU.ActiveCfg = Release|Any CPU - {78D65800-18D8-489F-99F9-9157D52F3749}.Release|Any CPU.Build.0 = Release|Any CPU + {0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {0C60BD36-F361-4C85-B86B-1BA1A7B05388} = {F6DC93A8-274F-4BC7-8CBD-BEFAE46C9941} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8760AA51-9BCC-433F-A279-C4273FC26858} EndGlobalSection diff --git a/build/ProjectInfo.fs b/build/ProjectInfo.fs index 7688097..713663c 100644 --- a/build/ProjectInfo.fs +++ b/build/ProjectInfo.fs @@ -7,7 +7,7 @@ let project = "Fable.Pyxpecto" let testProjects = [ // add relative paths (from project root) to your testprojects here - "tests" + "tests/Mocha.Tests" ] let solutionFile = $"{project}.sln" diff --git a/src/Pyxpecto.fs b/src/Pyxpecto.fs index f87c1a5..1c0bac6 100644 --- a/src/Pyxpecto.fs +++ b/src/Pyxpecto.fs @@ -4,6 +4,20 @@ open System open Fable.Core.Testing open Fable.Core +[] +type Stopwatch() = + member val StartTime: DateTime option = None with get, set + member val StopTime: DateTime option = None with get, set + member this.Start() = this.StartTime <- Some DateTime.Now + member this.Stop() = + match this.StartTime with + | Some _ -> this.StopTime <- Some DateTime.Now + | None -> failwith "Error. Unable to call `Stop` before `Start`." + member this.Elapsed : TimeSpan = + match this.StartTime, this.StopTime with + | Some start, Some stop -> stop - start + | _, _ -> failwith "Error. Unable to call `Elapsed` without calling `Start` and `Stop` before." + type FocusState = | Normal | Pending @@ -350,10 +364,11 @@ module Pyxpecto = member this.SumTests with get() = this.SuccessfulTests + this.FailedTests + this.ErrorTests - member private this.printSuccessMsg (name: string) = + member private this.printSuccessMsg (name: string) (runtime: TimeSpan option) = let focused = if this.HasFocused then "💎 | " else "" this.SuccessfulTests <- this.SuccessfulTests + 1 - printfn $"{focused}✔️ {name}" + let timespan = if runtime.IsSome then $" ({runtime.Value.ToString()})" else "" + printfn $"{focused}✔️ {name}{timespan}" member private this.printErrorMsg (name: string) (msg: string) (isTrueError: bool)= this.ErrorMessages.Add(name, msg) let focused = if this.HasFocused then "💎 | " else "" @@ -370,7 +385,7 @@ module Pyxpecto = member this.RunSyncTest(name: string, body: unit -> unit) = try body() - this.printSuccessMsg name + this.printSuccessMsg name None with | :? AssertException as exn -> this.printErrorMsg name exn.Message false @@ -378,10 +393,13 @@ module Pyxpecto = this.printErrorMsg name e.Message true member this.RunAsyncTest(name, body: Async) = + let stopwatch = new Stopwatch() + stopwatch.Start() try async { do! body - this.printSuccessMsg name + stopwatch.Stop() + this.printSuccessMsg name (Some stopwatch.Elapsed) } |> Async.RunSynchronously with diff --git a/tests/Main.fs b/tests/Mocha.Tests/Main.fs similarity index 100% rename from tests/Main.fs rename to tests/Mocha.Tests/Main.fs diff --git a/tests/Fable.Pyxpecto.Tests.fsproj b/tests/Mocha.Tests/Mocha.Tests.fsproj similarity index 88% rename from tests/Fable.Pyxpecto.Tests.fsproj rename to tests/Mocha.Tests/Mocha.Tests.fsproj index 49e0938..c841e93 100644 --- a/tests/Fable.Pyxpecto.Tests.fsproj +++ b/tests/Mocha.Tests/Mocha.Tests.fsproj @@ -15,10 +15,7 @@ + - - - - diff --git a/tests/Mocha.Tests/README.md b/tests/Mocha.Tests/README.md new file mode 100644 index 0000000..37503d6 --- /dev/null +++ b/tests/Mocha.Tests/README.md @@ -0,0 +1,3 @@ +# Mocha.Tests + +These tests are used to check if switching Pyxpecto with Mocha works in an .NET Expecto environment. \ No newline at end of file