This repository was archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathWorkspaceTests.cs
More file actions
160 lines (135 loc) · 7.34 KB
/
Copy pathWorkspaceTests.cs
File metadata and controls
160 lines (135 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.IO;
using Microsoft.Quantum.IQSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tests.IQSharp
{
[TestClass]
public class WorkspaceTests
{
[TestMethod]
public async Task InitWorkspace()
{
var ws = Startup.Create<Workspace>("Workspace");
await ws.Initialization;
var dll = ws.Projects.Single().CacheDllPath;
if (File.Exists(dll)) File.Delete(dll);
// First time
ws = Startup.Create<Workspace>("Workspace");
await ws.Initialization;
Assert.That.Workspace(ws).DoesNotHaveErrors();
var op = ws.AssemblyInfo?.Operations.FirstOrDefault(o => o.FullName == "Tests.qss.NoOp");
Assert.IsNotNull(op);
// On next reload:
ws = Startup.Create<Workspace>("Workspace");
await ws.Initialization;
Assert.That.Workspace(ws).DoesNotHaveErrors();
op = ws.AssemblyInfo?.Operations.FirstOrDefault(o => o.FullName == "Tests.qss.NoOp");
Assert.IsNotNull(op);
}
[TestMethod]
public async Task ReloadWorkspace()
{
var ws = Startup.Create<Workspace>("Workspace");
await ws.Initialization;
var originalAssembly = ws.AssemblyInfo;
var op = ws.AssemblyInfo?.Operations.FirstOrDefault(o => o.FullName == "Tests.qss.NoOp");
Assert.IsFalse(ws.HasErrors);
Assert.IsNotNull(op);
// Calling Reload with no changes, should regenerate the dll:
await ws.Reload();
op = ws.AssemblyInfo?.Operations.FirstOrDefault(o => o.FullName == "Tests.qss.NoOp");
Assert.IsFalse(ws.HasErrors);
Assert.IsNotNull(op);
Assert.AreNotSame(originalAssembly, ws.AssemblyInfo);
var fileName = Path.Combine(Path.GetFullPath("Workspace"), "BasicOps.qs");
File.SetLastWriteTimeUtc(fileName, DateTime.UtcNow);
await ws.Reload();
op = ws.AssemblyInfo?.Operations.FirstOrDefault(o => o.FullName == "Tests.qss.NoOp");
Assert.IsNotNull(op);
Assert.IsFalse(ws.HasErrors);
Assert.AreNotSame(originalAssembly, ws.AssemblyInfo);
}
[TestMethod]
public async Task BrokenWorkspace()
{
var ws = Startup.Create<Workspace>("Workspace.Broken");
await ws.Reload();
Assert.IsTrue(ws.HasErrors);
}
[TestMethod]
public async Task ProjectReferencesWorkspace()
{
// Loading this workspace should succeed and automatically pull in the referenced projects
// Workspace.ProjectReferences.ProjectA and Workspace.ProjectReferences.ProjectB.
var ws = Startup.Create<Workspace>("Workspace.ProjectReferences");
await ws.Reload();
Assert.IsFalse(ws.HasErrors, string.Join(Environment.NewLine, ws.ErrorMessages.OrEmpty()));
var operations = ws.Projects.SelectMany(p => (p.AssemblyInfo?.Operations).OrEmpty());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.MeasureSingleQubit").Any());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.ProjectA.RotateAndMeasure").Any());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.ProjectB.RotateAndMeasure").Any());
}
[TestMethod]
public async Task ProjectReferencesWorkspaceNoAutoLoad()
{
// Loading this workspace should fail because the .csproj does not specify <IQSharpLoadAutomatically>,
// which prevents the code that depends on Workspace.ProjectReferences.ProjectB from compiling correctly.
var ws = Startup.Create<Workspace>("Workspace.ProjectReferences.ProjectA");
await ws.Reload();
Assert.IsTrue(ws.HasErrors);
// Loading this workspace should succeed, and its Q# operations should be available, but the .csproj
// reference should not be loaded because the .csproj specifies <IQSharpLoadAutomatically> as false.
ws = Startup.Create<Workspace>("Workspace.ProjectReferences.ProjectB");
await ws.Reload();
Assert.IsFalse(ws.HasErrors, string.Join(Environment.NewLine, ws.ErrorMessages ?? Enumerable.Empty<string>()));
Assert.IsTrue(ws.Projects.Count() == 1);
Assert.IsTrue(string.IsNullOrEmpty(ws.Projects.First().ProjectFile));
var operations = ws.Projects.SelectMany(p => p.AssemblyInfo?.Operations ?? Enumerable.Empty<OperationInfo>());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.ProjectB.RotateAndMeasure").Any());
}
[TestMethod]
public async Task ManuallyAddProjects()
{
var ws = Startup.Create<Workspace>("Workspace");
await ws.Reload();
Assert.IsFalse(ws.HasErrors, string.Join(Environment.NewLine, ws.ErrorMessages ?? Enumerable.Empty<string>()));
ws.AddProject("../Workspace.ProjectReferences.ProjectA/ProjectA.csproj");
await ws.Reload();
Assert.IsFalse(ws.HasErrors, string.Join(Environment.NewLine, ws.ErrorMessages ?? Enumerable.Empty<string>()));
var operations = ws.Projects.SelectMany(p => p.AssemblyInfo?.Operations ?? Enumerable.Empty<OperationInfo>());
Assert.IsFalse(operations.Where(o => o.FullName == "Tests.ProjectReferences.MeasureSingleQubit").Any());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.ProjectA.RotateAndMeasure").Any());
ws.AddProject("../Workspace.ProjectReferences/Workspace.ProjectReferences.csproj");
await ws.Reload();
Assert.IsFalse(ws.HasErrors, string.Join(Environment.NewLine, ws.ErrorMessages ?? Enumerable.Empty<string>()));
operations = ws.Projects.SelectMany(p => p.AssemblyInfo?.Operations ?? Enumerable.Empty<OperationInfo>());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.MeasureSingleQubit").Any());
Assert.IsTrue(operations.Where(o => o.FullName == "Tests.ProjectReferences.ProjectA.RotateAndMeasure").Any());
// Try to add a project that doesn't exist
Assert.ThrowsException<FileNotFoundException>(() =>
ws.AddProject("../InvalidProject/InvalidProject.csproj")
);
// Try to add a project that should have already been loaded
Assert.ThrowsException<InvalidOperationException>(() =>
ws.AddProject("../Workspace.ProjectReferences.ProjectB/ProjectB.csproj")
);
}
[TestMethod]
public async Task ChemistryWorkspace()
{
var ws = Startup.Create<Workspace>("Workspace.Chemistry");
await ws.Reload();
Assert.IsTrue(ws.HasErrors);
ws.GlobalReferences.AddPackage($"mock.chemistry").Wait();
await ws.Reload();
Assert.IsFalse(ws.HasErrors);
var op = ws
.AssemblyInfo
?.Operations
?.FirstOrDefault(o => o.FullName == "Tests.IQSharp.Chemistry.Samples.UseJordanWignerEncodingData");
Assert.IsNotNull(op);
}
}
}