From 540528c490303dc5c81ebe921dcdbe97fbef09e0 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 17 Jun 2020 16:53:48 -0700 Subject: [PATCH 1/4] Use original file name without converting it to a URI --- src/Simulation/CsharpGeneration/SimulationCode.fs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Simulation/CsharpGeneration/SimulationCode.fs b/src/Simulation/CsharpGeneration/SimulationCode.fs index 32cc584932b..81031640733 100644 --- a/src/Simulation/CsharpGeneration/SimulationCode.fs +++ b/src/Simulation/CsharpGeneration/SimulationCode.fs @@ -1566,13 +1566,10 @@ module SimulationCode = generator.Apply elements // Returns only those namespaces and their elements that are defined for the given file. - let findLocalElements selector fileName syntaxTree = - let path = - match CompilationBuilder.CompilationUnitManager.TryGetUri fileName with - | true, uri -> uri.AbsolutePath |> NonNullable.New - | false, _ -> NonNullable.New "" + let findLocalElements selector (fileName : NonNullable) syntaxTree = syntaxTree - |> Seq.map (fun ns -> (ns.Name, (FilterBySourceFile.Apply (ns, path)).Elements |> Seq.choose selector |> Seq.toList)) + |> Seq.map (fun ns -> + (ns.Name, (FilterBySourceFile.Apply (ns, fileName)).Elements |> Seq.choose selector |> Seq.toList)) |> Seq.sortBy fst |> Seq.filter (fun (_,elements) -> not elements.IsEmpty) |> Seq.toList From 62c9104854afd237e693de80a81195995cbc3df3 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 17 Jun 2020 20:47:41 -0700 Subject: [PATCH 2/4] Add test for spaces in path with EmbedAllSources --- Simulation.sln | 19 ++++++++++ .../Microsoft.Quantum.CsharpGeneration.fsproj | 2 +- ....Simulation.QCTraceSimulatorRuntime.csproj | 2 +- .../Microsoft.Quantum.QSharp.Core.csproj | 2 +- .../Library with Spaces.csproj | 25 ++++++++++++ .../Library with Spaces/Library.qs | 5 +++ .../TestProjects/Library1/Library1.csproj | 2 +- .../TestProjects/Library2/Library2.csproj | 2 +- .../TestProjects/QsharpExe/QsharpExe.csproj | 2 +- .../TestProjects/UnitTests/Facts.qs | 37 ++++++++++++++++++ .../TestProjects/UnitTests/SpaceTests.qs | 9 +++++ .../UnitTests/{Tests.qs => TestNameTests.qs} | 38 +------------------ .../TestProjects/UnitTests/UnitTests.csproj | 3 +- .../Tests.Microsoft.Quantum.Simulators.csproj | 2 +- .../Microsoft.Quantum.Simulators.csproj | 2 +- 15 files changed, 106 insertions(+), 46 deletions(-) create mode 100644 src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj create mode 100644 src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library.qs create mode 100644 src/Simulation/Simulators.Tests/TestProjects/UnitTests/Facts.qs create mode 100644 src/Simulation/Simulators.Tests/TestProjects/UnitTests/SpaceTests.qs rename src/Simulation/Simulators.Tests/TestProjects/UnitTests/{Tests.qs => TestNameTests.qs} (52%) diff --git a/Simulation.sln b/Simulation.sln index 7886189c882..517083d2468 100644 --- a/Simulation.sln +++ b/Simulation.sln @@ -55,6 +55,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library1", "src\Simulation\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library2", "src\Simulation\Simulators.Tests\TestProjects\Library2\Library2.csproj", "{A85277B3-4E07-4E15-8F0C-07CC855A3BCB}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library with Spaces", "src\Simulation\Simulators.Tests\TestProjects\Library with Spaces\Library with Spaces.csproj", "{418E79F7-9FCF-4128-AA35-1334A685377D}" +EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "src\Simulation\Simulators.Tests\TestProjects\UnitTests\UnitTests.csproj", "{46278108-D247-4EFC-AC34-23D4A676F62F}" EndProject Global @@ -389,6 +391,22 @@ Global {46278108-D247-4EFC-AC34-23D4A676F62F}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU {46278108-D247-4EFC-AC34-23D4A676F62F}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU {46278108-D247-4EFC-AC34-23D4A676F62F}.RelWithDebInfo|x64.Build.0 = Release|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|x64.ActiveCfg = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|x64.Build.0 = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Release|Any CPU.Build.0 = Release|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Release|x64.ActiveCfg = Release|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.Release|x64.Build.0 = Release|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|Any CPU.ActiveCfg = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|Any CPU.Build.0 = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -416,6 +434,7 @@ Global {7256B986-6705-42FC-9F57-485D72D9DE51} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820} {A85277B3-4E07-4E15-8F0C-07CC855A3BCB} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820} {46278108-D247-4EFC-AC34-23D4A676F62F} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820} + {418E79F7-9FCF-4128-AA35-1334A685377D} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {929C0464-86D8-4F70-8835-0A5EAF930821} diff --git a/src/Simulation/CsharpGeneration/Microsoft.Quantum.CsharpGeneration.fsproj b/src/Simulation/CsharpGeneration/Microsoft.Quantum.CsharpGeneration.fsproj index 0da8bb707e6..c9918569359 100644 --- a/src/Simulation/CsharpGeneration/Microsoft.Quantum.CsharpGeneration.fsproj +++ b/src/Simulation/CsharpGeneration/Microsoft.Quantum.CsharpGeneration.fsproj @@ -21,7 +21,7 @@ - + diff --git a/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj b/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj index f83db39edc0..f6c4f8424a0 100644 --- a/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj +++ b/src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/QsharpCore/Microsoft.Quantum.QSharp.Core.csproj b/src/Simulation/QsharpCore/Microsoft.Quantum.QSharp.Core.csproj index 6fa6d8bc6a1..9929c01247c 100644 --- a/src/Simulation/QsharpCore/Microsoft.Quantum.QSharp.Core.csproj +++ b/src/Simulation/QsharpCore/Microsoft.Quantum.QSharp.Core.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj new file mode 100644 index 00000000000..eafbd2f8cee --- /dev/null +++ b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj @@ -0,0 +1,25 @@ + + + netstandard2.1 + false + false + + + true + + + + + + + + + + + + + diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library.qs b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library.qs new file mode 100644 index 00000000000..eebe352cb30 --- /dev/null +++ b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library.qs @@ -0,0 +1,5 @@ +namespace LibraryWithSpaces { + function HelloQ() : String { + return "Hello quantum world!"; + } +} diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj index c7cf8bab959..9ea56440596 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj index c7cf8bab959..9ea56440596 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 diff --git a/src/Simulation/Simulators.Tests/TestProjects/QsharpExe/QsharpExe.csproj b/src/Simulation/Simulators.Tests/TestProjects/QsharpExe/QsharpExe.csproj index c851ccd391c..dce7e09d68a 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/QsharpExe/QsharpExe.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/QsharpExe/QsharpExe.csproj @@ -1,4 +1,4 @@ - + Exe diff --git a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/Facts.qs b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/Facts.qs new file mode 100644 index 00000000000..880bdf81a85 --- /dev/null +++ b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/Facts.qs @@ -0,0 +1,37 @@ +namespace Microsoft.Quantum.Testing { + internal function FactI(expected : Int, got : Int) : Unit { + if (expected != got) { + fail $"Expected: {expected}, got: {got}"; + } + } + + internal function FactS(expected : String, got : String) : Unit { + if (expected != got) { + fail $"Expected: {expected}, got: {got}"; + } + } + + internal function FactMyInt1(expected : Int, got : Library1.MyInt) : Unit { + if (expected != got::Value1) { + fail $"Expected: {expected}, got: {got::Value1}"; + } + } + + internal function FactMyInt2(expected : Int, got : Library2.MyInt) : Unit { + if (expected != got::Value2) { + fail $"Expected: {expected}, got: {got::Value2}"; + } + } + + internal function FactMyString1(expected : String, got : Microsoft.Quantum.Library.MyString) : Unit { + if (expected != got::Text) { + fail $"Expected: {expected}, got: {got::Text}"; + } + } + + internal function FactMyString2(expected : String, got : Library2.MyString) : Unit { + if (expected != got::Text) { + fail $"Expected: {expected}, got: {got::Text}"; + } + } +} diff --git a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/SpaceTests.qs b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/SpaceTests.qs new file mode 100644 index 00000000000..2faa5616e2b --- /dev/null +++ b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/SpaceTests.qs @@ -0,0 +1,9 @@ +namespace Microsoft.Quantum.Testing.SpacesInFileName { + open Microsoft.Quantum.Diagnostics; + open Microsoft.Quantum.Testing; + + @Test("QuantumSimulator") + function LibraryWithSpacesTest() : Unit { + FactS("Hello quantum world!", LibraryWithSpaces.HelloQ()); + } +} diff --git a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/Tests.qs b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/TestNameTests.qs similarity index 52% rename from src/Simulation/Simulators.Tests/TestProjects/UnitTests/Tests.qs rename to src/Simulation/Simulators.Tests/TestProjects/UnitTests/TestNameTests.qs index 008f2c9f646..68a3ee38845 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/Tests.qs +++ b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/TestNameTests.qs @@ -2,43 +2,7 @@ open Microsoft.Quantum.Diagnostics; open Microsoft.Quantum.Intrinsic; - - internal function FactI(expected : Int, got : Int) : Unit { - if (expected != got) { - fail $"Expected: {expected}, got: {got}"; - } - } - - internal function FactS(expected : String, got : String) : Unit { - if (expected != got) { - fail $"Expected: {expected}, got: {got}"; - } - } - - internal function FactMyInt1(expected : Int, got : Library1.MyInt) : Unit { - if (expected != got::Value1) { - fail $"Expected: {expected}, got: {got::Value1}"; - } - } - - internal function FactMyInt2(expected : Int, got : Library2.MyInt) : Unit { - if (expected != got::Value2) { - fail $"Expected: {expected}, got: {got::Value2}"; - } - } - - internal function FactMyString1(expected : String, got : Microsoft.Quantum.Library.MyString) : Unit { - if (expected != got::Text) { - fail $"Expected: {expected}, got: {got::Text}"; - } - } - - internal function FactMyString2(expected : String, got : Library2.MyString) : Unit { - if (expected != got::Text) { - fail $"Expected: {expected}, got: {got::Text}"; - } - } - + open Microsoft.Quantum.Testing; @Test("QuantumSimulator") operation BothCallables () : Unit { diff --git a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj index 6ff0e165ec6..0a69f975332 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/UnitTests/UnitTests.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -15,6 +15,7 @@ + diff --git a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj index 1255e9180a0..4df5832a44e 100644 --- a/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj +++ b/src/Simulation/Simulators.Tests/Tests.Microsoft.Quantum.Simulators.csproj @@ -1,4 +1,4 @@ - + diff --git a/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj b/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj index d47e83a63ad..0c34e04224c 100644 --- a/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj +++ b/src/Simulation/Simulators/Microsoft.Quantum.Simulators.csproj @@ -1,4 +1,4 @@ - + From a8b6a792b9e76d664e53c640e4f03ac33680dc8d Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Wed, 17 Jun 2020 21:45:54 -0700 Subject: [PATCH 3/4] Fix tests --- .../SimulationCodeTests.fs | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs b/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs index 6605b571cd2..e03d52d4f29 100644 --- a/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs +++ b/src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs @@ -7,6 +7,7 @@ open System open System.Collections.Immutable open System.IO open System.Globalization +open System.Web open Microsoft.CodeAnalysis open Microsoft.CodeAnalysis.CSharp @@ -217,10 +218,14 @@ namespace N1 let testOneFile fileName (expected:string) = - let expected = expected.Replace("%%%", (Uri(Path.GetFullPath fileName)).AbsolutePath) - let expected = expected.Replace("%%", (Path.GetFullPath fileName).Replace("\\", "\\\\")) - let tree = parse [(Path.Combine("Circuits","Intrinsic.qs")); fileName] - let actual = + let fullPath = Path.GetFullPath fileName + let escapeCSharpString (s : string) = SymbolDisplay.FormatLiteral (s, false) + let expected = + expected + |> (fun s -> s.Replace("%%%", fullPath |> HttpUtility.JavaScriptStringEncode |> escapeCSharpString)) + |> (fun s -> s.Replace("%%", fullPath |> escapeCSharpString)) + let tree = parse [Path.Combine ("Circuits", "Intrinsic.qs"); fileName] + let actual = CodegenContext.Create (tree, ImmutableDictionary.Empty) |> generate (Path.GetFullPath fileName |> NonNullable.New) Assert.Equal(expected |> clearFormatting, actual |> clearFormatting) @@ -2306,7 +2311,7 @@ namespace N1 false |> testOne randomOperation let testOneClass (_,op : QsCallable) executionTarget (expected : string) = - let expected = expected.Replace("%%%", op.SourceFile.Value) + let expected = expected.Replace("%%%", HttpUtility.JavaScriptStringEncode op.SourceFile.Value) let assemblyConstants = new System.Collections.Generic.KeyValuePair<_,_> (AssemblyConstants.ExecutionTarget, executionTarget) |> Seq.singleton @@ -3369,7 +3374,7 @@ using Microsoft.Quantum.Simulation.Core; #line hidden namespace Microsoft.Quantum.Tests.Inline { - [SourceLocation("%%%", OperationFunctor.Body, 7, -1)] + [SourceLocation("%%", OperationFunctor.Body, 7, -1)] public partial class HelloWorld : Operation, ICallable { public HelloWorld(IOperationFactory m) : base(m) @@ -3426,7 +3431,7 @@ using Microsoft.Quantum.Simulation.Core; #line hidden namespace Microsoft.Quantum.Tests.LineNumbers { - [SourceLocation("%%%", OperationFunctor.Body, 9, -1)] + [SourceLocation("%%", OperationFunctor.Body, 9, -1)] public partial class TestLineInBlocks : Operation, ICallable { public TestLineInBlocks(IOperationFactory m) : base(m) @@ -3600,7 +3605,7 @@ namespace Microsoft.Quantum.Diagnostics #line hidden namespace Microsoft.Quantum.Tests.UnitTests { - [SourceLocation("%%%", OperationFunctor.Body, 22, 26)] + [SourceLocation("%%", OperationFunctor.Body, 22, 26)] public partial class UnitTest1 : Operation, ICallable { public UnitTest1(IOperationFactory m) : base(m) @@ -3623,7 +3628,7 @@ namespace Microsoft.Quantum.Tests.UnitTests [Xunit.Trait("Target", "QuantumSimulator")] [Xunit.Trait("Name", "UnitTest1")] public void UnitTest1() -#line 22 "%%%" +#line 22 "%%" { var sim = new Microsoft.Quantum.Simulation.Simulators.QuantumSimulator(); if (sim is Microsoft.Quantum.Simulation.Common.SimulatorBase baseSim && this.Output != null) @@ -3655,7 +3660,7 @@ namespace Microsoft.Quantum.Tests.UnitTests [Xunit.Trait("Target", "ToffoliSimulator")] [Xunit.Trait("Name", "UnitTest1")] public void UnitTest1() -#line 22 "%%%" +#line 22 "%%" { var sim = new Microsoft.Quantum.Simulation.Simulators.ToffoliSimulator(); if (sim is Microsoft.Quantum.Simulation.Common.SimulatorBase baseSim && this.Output != null) @@ -3693,7 +3698,7 @@ namespace Microsoft.Quantum.Tests.UnitTests } } - [SourceLocation("%%%", OperationFunctor.Body, 26, -1)] + [SourceLocation("%%", OperationFunctor.Body, 26, -1)] public partial class UnitTest2 : Operation, ICallable { public UnitTest2(IOperationFactory m) : base(m) @@ -3716,7 +3721,7 @@ namespace Microsoft.Quantum.Tests.UnitTests [Xunit.Trait("Target", "CustomSimulator")] [Xunit.Trait("Name", "UnitTest2")] public void UnitTest2() -#line 26 "%%%" +#line 26 "%%" { var sim = new SomeNamespace.CustomSimulator(); if (sim is Microsoft.Quantum.Simulation.Common.SimulatorBase baseSim && this.Output != null) From e8536fe0339f3204022ade50a8b81773e94e4915 Mon Sep 17 00:00:00 2001 From: Sarah Marshall Date: Tue, 23 Jun 2020 16:33:19 -0700 Subject: [PATCH 4/4] Update Library with Spaces SDK version --- .../TestProjects/Library with Spaces/Library with Spaces.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj index eafbd2f8cee..abfb0b57f2f 100644 --- a/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj +++ b/src/Simulation/Simulators.Tests/TestProjects/Library with Spaces/Library with Spaces.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 false