From 984592da66d4c792ef2abe1dda243397d7ab5a82 Mon Sep 17 00:00:00 2001 From: Scott Carda Date: Thu, 15 Oct 2020 16:29:17 -0700 Subject: [PATCH] Updated generated code to use try-catch in unit tests. --- .../SimulationCodeTests.fs | 51 +++++++++++++++---- .../CsharpGeneration/SimulationCode.fs | 24 +++++++-- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs b/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs index b20864f09ee..f3556f08b3d 100644 --- a/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs +++ b/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs @@ -3685,10 +3685,21 @@ namespace Microsoft.Quantum.Tests.UnitTests baseSim.OnLog += this.Output.WriteLine; } - sim.Run(QVoid.Instance).Wait(); - if (sim is IDisposable disposeSim) + try + { + sim.Execute(QVoid.Instance); + } + catch (Exception e) + { +#line 22 "%%" + Xunit.Assert.True(false, "Q# Test failed. For details see the Standard output below."); + } + finally { - disposeSim.Dispose(); + if (sim is IDisposable disposeSim) + { + disposeSim.Dispose(); + } } } } @@ -3717,10 +3728,21 @@ namespace Microsoft.Quantum.Tests.UnitTests baseSim.OnLog += this.Output.WriteLine; } - sim.Run(QVoid.Instance).Wait(); - if (sim is IDisposable disposeSim) + try { - disposeSim.Dispose(); + sim.Execute(QVoid.Instance); + } + catch (Exception e) + { +#line 22 "%%" + Xunit.Assert.True(false, "Q# Test failed. For details see the Standard output below."); + } + finally + { + if (sim is IDisposable disposeSim) + { + disposeSim.Dispose(); + } } } } @@ -3778,10 +3800,21 @@ namespace Microsoft.Quantum.Tests.UnitTests baseSim.OnLog += this.Output.WriteLine; } - sim.Run(QVoid.Instance).Wait(); - if (sim is IDisposable disposeSim) + try + { + sim.Execute(QVoid.Instance); + } + catch (Exception e) { - disposeSim.Dispose(); +#line 26 "%%" + Xunit.Assert.True(false, "Q# Test failed. For details see the Standard output below."); + } + finally + { + if (sim is IDisposable disposeSim) + { + disposeSim.Dispose(); + } } } } diff --git a/src/Simulation/CsharpGeneration/SimulationCode.fs b/src/Simulation/CsharpGeneration/SimulationCode.fs index d1dafb7b4bf..16000eb6ad3 100644 --- a/src/Simulation/CsharpGeneration/SimulationCode.fs +++ b/src/Simulation/CsharpGeneration/SimulationCode.fs @@ -1240,7 +1240,7 @@ module SimulationCode = let disposeSim = ``ident`` "disposeSim" let ``this.Output`` = ``ident`` "this" <|.|> ``ident`` "Output" let ``sim.OnLog`` = baseSim <|.|> ``ident`` "OnLog" - let Run = generic "Run" ``<<`` [opName; "QVoid"; "QVoid"] ``>>`` + let Execute = generic "Execute" ``<<`` [opName; "QVoid"; "QVoid"] ``>>`` let simCond = sim |> ``is assign`` "Microsoft.Quantum.Simulation.Common.SimulatorBase" baseSim .&&. ``this.Output`` .!=. ``null`` @@ -1248,11 +1248,29 @@ module SimulationCode = let assignLogEvent = ``if`` ``(`` simCond ``)`` [ ``sim.OnLog`` <+=> (``this.Output`` <|.|> ``ident`` "WriteLine") ] None - let ``sim.Run.Wait`` = sim <.> (Run, [ ``ident`` "QVoid" <|.|> ``ident`` "Instance"]) <.> ((``ident`` "Wait"), []) |> statement + let ``sim.Execute`` = sim <.> (Execute, [ ``ident`` "QVoid" <|.|> ``ident`` "Instance"]) |> statement let disposeOfRun = ``if`` ``(`` (sim |> ``is assign`` "IDisposable" disposeSim) ``)`` [ disposeSim <.> ((``ident`` "Dispose"), []) |> statement ] None + let errMsg = ``literal`` "Q# Test failed. For details see the Standard output below." + + let tryRunCatch = + ``try`` + [ + ``sim.Execute`` + ] + [ + ``catch`` (Some ("Exception", "e")) + [ + (``ident`` "Xunit.Assert") <.> ((``ident`` "True"), [``false`` :> ExpressionSyntax; errMsg]) |> (statement >> ``#line`` (opStart + 1) opSourceFile) + ] + ] + (Some (``finally`` + [ + ``disposeOfRun`` + ])) + ``attributes`` [ ``attribute`` None (``ident`` "Xunit.Fact") []; @@ -1261,7 +1279,7 @@ module SimulationCode = ] (``method`` "void" opName ``<<`` [] ``>>`` ``(`` [] ``)`` [``public``] ``{`` - [getSimulator; assignLogEvent; ``sim.Run.Wait``; disposeOfRun] + [getSimulator; assignLogEvent; tryRunCatch] ``}`` |> ``with trivia`` (``#lineNr`` (opStart + 1) opSourceFile) // we need 1-based line numbers here, and opStart is zero-based )