Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 86d3a81

Browse files
authored
Update C# generation and tests for file path format change (#263)
* Use original file name without converting it to a URI * Add test for spaces in path with EmbedAllSources * Fix tests * Update Library with Spaces SDK version
1 parent 6eb9d45 commit 86d3a81

File tree

17 files changed

+125
-63
lines changed

17 files changed

+125
-63
lines changed

Simulation.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Library1", "src\Simulation\
4747
EndProject
4848
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Library2", "src\Simulation\Simulators.Tests\TestProjects\Library2\Library2.csproj", "{A85277B3-4E07-4E15-8F0C-07CC855A3BCB}"
4949
EndProject
50+
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}"
51+
EndProject
5052
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "src\Simulation\Simulators.Tests\TestProjects\UnitTests\UnitTests.csproj", "{46278108-D247-4EFC-AC34-23D4A676F62F}"
5153
EndProject
5254
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Azure", "Azure", "{37CDC768-16D4-4574-8553-07D99D0A72F7}"
@@ -355,6 +357,22 @@ Global
355357
{46278108-D247-4EFC-AC34-23D4A676F62F}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
356358
{46278108-D247-4EFC-AC34-23D4A676F62F}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
357359
{46278108-D247-4EFC-AC34-23D4A676F62F}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
360+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
361+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|Any CPU.Build.0 = Debug|Any CPU
362+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|x64.ActiveCfg = Debug|Any CPU
363+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Debug|x64.Build.0 = Debug|Any CPU
364+
{418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
365+
{418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
366+
{418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
367+
{418E79F7-9FCF-4128-AA35-1334A685377D}.MinSizeRel|x64.Build.0 = Debug|Any CPU
368+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Release|Any CPU.ActiveCfg = Release|Any CPU
369+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Release|Any CPU.Build.0 = Release|Any CPU
370+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Release|x64.ActiveCfg = Release|Any CPU
371+
{418E79F7-9FCF-4128-AA35-1334A685377D}.Release|x64.Build.0 = Release|Any CPU
372+
{418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|Any CPU.ActiveCfg = Debug|Any CPU
373+
{418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|Any CPU.Build.0 = Debug|Any CPU
374+
{418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU
375+
{418E79F7-9FCF-4128-AA35-1334A685377D}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU
358376
{7F05FD87-A2FB-4915-A988-4EF92AB82179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
359377
{7F05FD87-A2FB-4915-A988-4EF92AB82179}.Debug|Any CPU.Build.0 = Debug|Any CPU
360378
{7F05FD87-A2FB-4915-A988-4EF92AB82179}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -411,6 +429,7 @@ Global
411429
{7256B986-6705-42FC-9F57-485D72D9DE51} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820}
412430
{A85277B3-4E07-4E15-8F0C-07CC855A3BCB} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820}
413431
{46278108-D247-4EFC-AC34-23D4A676F62F} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820}
432+
{418E79F7-9FCF-4128-AA35-1334A685377D} = {09C842CB-930C-4C7D-AD5F-E30DE4A55820}
414433
{7F05FD87-A2FB-4915-A988-4EF92AB82179} = {37CDC768-16D4-4574-8553-07D99D0A72F7}
415434
{4858E5E3-23FA-4928-B99A-54065875A2B9} = {37CDC768-16D4-4574-8553-07D99D0A72F7}
416435
EndGlobalSection

src/Simulation/CsharpGeneration.Tests/SimulationCodeTests.fs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open System
77
open System.Collections.Immutable
88
open System.IO
99
open System.Globalization
10+
open System.Web
1011

1112
open Microsoft.CodeAnalysis
1213
open Microsoft.CodeAnalysis.CSharp
@@ -218,9 +219,13 @@ namespace N1
218219

219220

220221
let testOneFile fileName (expected:string) =
221-
let expected = expected.Replace("%%%", (Uri(Path.GetFullPath fileName)).AbsolutePath)
222-
let expected = expected.Replace("%%", (Path.GetFullPath fileName).Replace("\\", "\\\\"))
223-
let tree = parse [(Path.Combine("Circuits","Intrinsic.qs")); fileName]
222+
let fullPath = Path.GetFullPath fileName
223+
let escapeCSharpString (s : string) = SymbolDisplay.FormatLiteral (s, false)
224+
let expected =
225+
expected
226+
|> (fun s -> s.Replace("%%%", fullPath |> HttpUtility.JavaScriptStringEncode |> escapeCSharpString))
227+
|> (fun s -> s.Replace("%%", fullPath |> escapeCSharpString))
228+
let tree = parse [Path.Combine ("Circuits", "Intrinsic.qs"); fileName]
224229
let actual =
225230
CodegenContext.Create (tree, ImmutableDictionary.Empty)
226231
|> generate (Path.GetFullPath fileName |> NonNullable<string>.New)
@@ -2307,7 +2312,7 @@ namespace N1
23072312
false |> testOne randomOperation
23082313

23092314
let testOneClass (_,op : QsCallable) executionTarget (expected : string) =
2310-
let expected = expected.Replace("%%%", op.SourceFile.Value)
2315+
let expected = expected.Replace("%%%", HttpUtility.JavaScriptStringEncode op.SourceFile.Value)
23112316
let assemblyConstants =
23122317
new System.Collections.Generic.KeyValuePair<_,_> (AssemblyConstants.ExecutionTarget, executionTarget)
23132318
|> Seq.singleton
@@ -3397,7 +3402,7 @@ using Microsoft.Quantum.Simulation.Core;
33973402
#line hidden
33983403
namespace Microsoft.Quantum.Tests.Inline
33993404
{
3400-
[SourceLocation("%%%", OperationFunctor.Body, 7, -1)]
3405+
[SourceLocation("%%", OperationFunctor.Body, 7, -1)]
34013406
public partial class HelloWorld : Operation<Int64, Int64>, ICallable
34023407
{
34033408
public HelloWorld(IOperationFactory m) : base(m)
@@ -3454,7 +3459,7 @@ using Microsoft.Quantum.Simulation.Core;
34543459
#line hidden
34553460
namespace Microsoft.Quantum.Tests.LineNumbers
34563461
{
3457-
[SourceLocation("%%%", OperationFunctor.Body, 9, -1)]
3462+
[SourceLocation("%%", OperationFunctor.Body, 9, -1)]
34583463
public partial class TestLineInBlocks : Operation<Int64, Result>, ICallable
34593464
{
34603465
public TestLineInBlocks(IOperationFactory m) : base(m)
@@ -3628,7 +3633,7 @@ namespace Microsoft.Quantum.Diagnostics
36283633
#line hidden
36293634
namespace Microsoft.Quantum.Tests.UnitTests
36303635
{
3631-
[SourceLocation("%%%", OperationFunctor.Body, 22, 26)]
3636+
[SourceLocation("%%", OperationFunctor.Body, 22, 26)]
36323637
public partial class UnitTest1 : Operation<QVoid, QVoid>, ICallable
36333638
{
36343639
public UnitTest1(IOperationFactory m) : base(m)
@@ -3651,7 +3656,7 @@ namespace Microsoft.Quantum.Tests.UnitTests
36513656
[Xunit.Trait("Target", "QuantumSimulator")]
36523657
[Xunit.Trait("Name", "UnitTest1")]
36533658
public void UnitTest1()
3654-
#line 22 "%%%"
3659+
#line 22 "%%"
36553660
{
36563661
var sim = new Microsoft.Quantum.Simulation.Simulators.QuantumSimulator();
36573662
if (sim is Microsoft.Quantum.Simulation.Common.SimulatorBase baseSim && this.Output != null)
@@ -3683,7 +3688,7 @@ namespace Microsoft.Quantum.Tests.UnitTests
36833688
[Xunit.Trait("Target", "ToffoliSimulator")]
36843689
[Xunit.Trait("Name", "UnitTest1")]
36853690
public void UnitTest1()
3686-
#line 22 "%%%"
3691+
#line 22 "%%"
36873692
{
36883693
var sim = new Microsoft.Quantum.Simulation.Simulators.ToffoliSimulator();
36893694
if (sim is Microsoft.Quantum.Simulation.Common.SimulatorBase baseSim && this.Output != null)
@@ -3721,7 +3726,7 @@ namespace Microsoft.Quantum.Tests.UnitTests
37213726
}
37223727
}
37233728
3724-
[SourceLocation("%%%", OperationFunctor.Body, 26, -1)]
3729+
[SourceLocation("%%", OperationFunctor.Body, 26, -1)]
37253730
public partial class UnitTest2 : Operation<QVoid, QVoid>, ICallable
37263731
{
37273732
public UnitTest2(IOperationFactory m) : base(m)
@@ -3744,7 +3749,7 @@ namespace Microsoft.Quantum.Tests.UnitTests
37443749
[Xunit.Trait("Target", "CustomSimulator")]
37453750
[Xunit.Trait("Name", "UnitTest2")]
37463751
public void UnitTest2()
3747-
#line 26 "%%%"
3752+
#line 26 "%%"
37483753
{
37493754
var sim = new SomeNamespace.CustomSimulator();
37503755
if (sim is Microsoft.Quantum.Simulation.Common.SimulatorBase baseSim && this.Output != null)

src/Simulation/CsharpGeneration/Microsoft.Quantum.CsharpGeneration.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<ItemGroup>
2323
<PackageReference Update="FSharp.Core" Version="4.7.0" />
24-
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.11.2006.2118-beta" />
24+
<PackageReference Include="Microsoft.Quantum.Compiler" Version="0.11.2006.2309-alpha" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

src/Simulation/CsharpGeneration/SimulationCode.fs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,13 +1573,10 @@ module SimulationCode =
15731573
generator.Apply elements
15741574

15751575
// Returns only those namespaces and their elements that are defined for the given file.
1576-
let findLocalElements selector fileName syntaxTree =
1577-
let path =
1578-
match CompilationBuilder.CompilationUnitManager.TryGetUri fileName with
1579-
| true, uri -> uri.AbsolutePath |> NonNullable<string>.New
1580-
| false, _ -> NonNullable<string>.New ""
1576+
let findLocalElements selector (fileName : NonNullable<string>) syntaxTree =
15811577
syntaxTree
1582-
|> Seq.map (fun ns -> (ns.Name, (FilterBySourceFile.Apply (ns, path)).Elements |> Seq.choose selector |> Seq.toList))
1578+
|> Seq.map (fun ns ->
1579+
(ns.Name, (FilterBySourceFile.Apply (ns, fileName)).Elements |> Seq.choose selector |> Seq.toList))
15831580
|> Seq.sortBy fst
15841581
|> Seq.filter (fun (_,elements) -> not elements.IsEmpty)
15851582
|> Seq.toList

src/Simulation/QCTraceSimulator.Tests/Tests.Microsoft.Quantum.Simulation.QCTraceSimulatorRuntime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2118-beta">
1+
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2309-alpha">
22

33
<Import Project="..\Common\AssemblyCommon.props" />
44
<Import Project="..\Common\Simulators.Dev.props" />

src/Simulation/QsharpCore/Microsoft.Quantum.QSharp.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2118-beta">
1+
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2309-alpha">
22

33
<Import Project="..\Common\AssemblyCommon.props" />
44
<Import Project="..\Common\DebugSymbols.props" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2309-alpha">
2+
<PropertyGroup>
3+
<TargetFramework>netstandard2.1</TargetFramework>
4+
<CsharpGeneration>false</CsharpGeneration>
5+
<IncludeQsharpCorePackages>false</IncludeQsharpCorePackages>
6+
7+
<!-- Use EmbedAllSources so that the #line annotations in the generated C# are checked by the C# compiler. If the
8+
- file paths in the annotations are invalid, the test will fail.
9+
-->
10+
<EmbedAllSources>true</EmbedAllSources>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\..\QsharpCore\Microsoft.Quantum.QSharp.Core.csproj" />
15+
<ProjectReference Include="..\..\..\CsharpGeneration\Microsoft.Quantum.CsharpGeneration.fsproj"
16+
PrivateAssets="All"
17+
IsQscReference="true" />
18+
</ItemGroup>
19+
20+
<Target Name="BeforeCsharpCompile">
21+
<ItemGroup>
22+
<Compile Include="$(GeneratedFilesOutputPath)**/*.g.cs" Exclude="@(Compile)" AutoGen="true" />
23+
</ItemGroup>
24+
</Target>
25+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace LibraryWithSpaces {
2+
function HelloQ() : String {
3+
return "Hello quantum world!";
4+
}
5+
}

src/Simulation/Simulators.Tests/TestProjects/Library1/Library1.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2118-beta">
1+
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2309-alpha">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>

src/Simulation/Simulators.Tests/TestProjects/Library2/Library2.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2118-beta">
1+
<Project Sdk="Microsoft.Quantum.Sdk/0.11.2006.2309-alpha">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>

0 commit comments

Comments
 (0)