microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/language_service/src/code_lens/tests.rs

242lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::get_code_lenses;
5use crate::{
6 Encoding,
7 test_utils::{
8 compile_notebook_with_fake_stdlib, compile_with_fake_stdlib_and_markers_no_cursor,
9 },
10};
11use expect_test::{Expect, expect};
12
13fn check(source_with_markers: &str, expect: &Expect) {
14 let (compilation, expected_code_lens_ranges) =
15 compile_with_fake_stdlib_and_markers_no_cursor(source_with_markers, true);
16 let mut actual_code_lenses = get_code_lenses(&compilation, "<source>", Encoding::Utf8);
17
18 for expected_range in &expected_code_lens_ranges {
19 assert!(
20 actual_code_lenses
21 .iter()
22 .any(|cl| cl.range == *expected_range),
23 "expected range not found in actual code lenses: {expected_range:?}"
24 );
25 }
26
27 for actual_range in actual_code_lenses.iter().map(|cl| cl.range) {
28 assert!(
29 expected_code_lens_ranges.iter().any(|r| r == &actual_range),
30 "got code lens for unexpected range: {actual_range:?}"
31 );
32 }
33
34 let actual = expected_code_lens_ranges
35 .into_iter()
36 .enumerate()
37 .map(move |(i, r)| {
38 actual_code_lenses.sort_by_key(|cl| cl.range == r);
39 let partition_point = actual_code_lenses.partition_point(|cl| cl.range != r);
40 let for_this_range = actual_code_lenses.drain(partition_point..);
41 (i, for_this_range.map(|cl| cl.command).collect::<Vec<_>>())
42 })
43 .collect::<Vec<_>>();
44 expect.assert_debug_eq(&actual);
45}
46
47#[test]
48fn one_entrypoint() {
49 check(
50 r#"
51 namespace Test {
52 @EntryPoint()
53 ◉operation Test() : Unit{
54 }◉
55 }"#,
56 &expect![[r#"
57 [
58 (
59 0,
60 [
61 Run(
62 "Test.Test()",
63 ),
64 Histogram(
65 "Test.Test()",
66 ),
67 Estimate(
68 "Test.Test()",
69 ),
70 Debug(
71 "Test.Test()",
72 ),
73 Circuit(
74 OperationInfo {
75 operation: "Test.Test",
76 total_num_qubits: 0,
77 },
78 ),
79 ],
80 ),
81 ]
82 "#]],
83 );
84}
85
86#[test]
87fn main_function() {
88 check(
89 r#"
90 namespace Test {
91 ◉operation Main() : Unit {
92 }◉
93
94 ◉operation Foo() : Unit{
95 }◉
96 }"#,
97 &expect![[r#"
98 [
99 (
100 0,
101 [
102 Run(
103 "Test.Main()",
104 ),
105 Histogram(
106 "Test.Main()",
107 ),
108 Estimate(
109 "Test.Main()",
110 ),
111 Debug(
112 "Test.Main()",
113 ),
114 Circuit(
115 OperationInfo {
116 operation: "Test.Main",
117 total_num_qubits: 0,
118 },
119 ),
120 ],
121 ),
122 (
123 1,
124 [
125 Run(
126 "Test.Foo()",
127 ),
128 Histogram(
129 "Test.Foo()",
130 ),
131 Estimate(
132 "Test.Foo()",
133 ),
134 Debug(
135 "Test.Foo()",
136 ),
137 Circuit(
138 OperationInfo {
139 operation: "Test.Foo",
140 total_num_qubits: 0,
141 },
142 ),
143 ],
144 ),
145 ]
146 "#]],
147 );
148}
149
150#[test]
151fn no_entrypoint_code_lens_in_notebook() {
152 let compilation = compile_notebook_with_fake_stdlib(
153 [(
154 "cell1",
155 "@EntryPoint()
156 operation Main() : Unit {}",
157 )]
158 .into_iter(),
159 );
160
161 let lenses = get_code_lenses(&compilation, "cell1", Encoding::Utf8);
162 assert!(
163 lenses.is_empty(),
164 "entrypoint code lenses should not be present in notebooks"
165 );
166}
167
168#[test]
169fn qubit_operation_circuit() {
170 check(
171 r#"
172 namespace Test {
173 ◉operation Foo(q: Qubit) : Unit {
174 }◉
175 }"#,
176 &expect![[r#"
177 [
178 (
179 0,
180 [
181 Circuit(
182 OperationInfo {
183 operation: "Test.Foo",
184 total_num_qubits: 1,
185 },
186 ),
187 ],
188 ),
189 ]
190 "#]],
191 );
192}
193
194#[test]
195fn qubit_arrays_operation_circuit() {
196 check(
197 r#"
198 namespace Test {
199 ◉operation Foo(q: Qubit, q1: Qubit[], q2: Qubit[][]) : Unit {
200 }◉
201 }"#,
202 &expect![[r#"
203 [
204 (
205 0,
206 [
207 Circuit(
208 OperationInfo {
209 operation: "Test.Foo",
210 total_num_qubits: 7,
211 },
212 ),
213 ],
214 ),
215 ]
216 "#]],
217 );
218}
219
220#[test]
221fn no_code_lenses_with_compilation_errors() {
222 let source = r#"
223 namespace Test {
224 operation Main() : Unit {
225 foo // undefined variable - compilation error
226 }
227 }"#;
228
229 let (compilation, _) = compile_with_fake_stdlib_and_markers_no_cursor(source, true);
230
231 // Verify the compilation actually has errors
232 assert!(
233 !compilation.compile_errors.is_empty(),
234 "Test should have compilation errors"
235 );
236
237 let lenses = get_code_lenses(&compilation, "<source>", Encoding::Utf8);
238 assert!(
239 lenses.is_empty(),
240 "code lenses should not be present when there are compilation errors"
241 );
242}
243