microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.0.10-rc

Branches

Tags

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

Clone

HTTPS

Download ZIP

language_service/src/definition/tests.rs

365lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use expect_test::{expect, Expect};
5
6use super::{get_definition, Definition};
7use crate::test_utils::{compile_with_fake_stdlib, get_source_and_marker_offsets};
8
9/// Asserts that the definition found at the given cursor position matches the expected position.
10/// The cursor position is indicated by a `↘` marker in the source text.
11/// The expected definition position is indicated by a `◉` marker in the source text.
12fn assert_definition(source_with_markers: &str) {
13 let (source, cursor_offsets, target_offsets) =
14 get_source_and_marker_offsets(source_with_markers);
15 let compilation = compile_with_fake_stdlib("<source>", &source);
16 let actual_definition = get_definition(&compilation, "<source>", cursor_offsets[0]);
17 let expected_definition = if target_offsets.is_empty() {
18 None
19 } else {
20 Some(Definition {
21 source: "<source>".to_string(),
22 offset: target_offsets[0],
23 })
24 };
25 assert_eq!(&expected_definition, &actual_definition);
26}
27
28fn check(source_with_markers: &str, expect: &Expect) {
29 let (source, cursor_offsets, _) = get_source_and_marker_offsets(source_with_markers);
30 let compilation = compile_with_fake_stdlib("<source>", &source);
31 let actual_definition = get_definition(&compilation, "<source>", cursor_offsets[0]);
32 expect.assert_debug_eq(&actual_definition);
33}
34
35#[test]
36fn callable() {
37 assert_definition(
38 r#"
39 namespace Test {
40 operation ◉F↘oo() : Unit {
41 }
42 }
43 "#,
44 );
45}
46
47#[test]
48fn callable_ref() {
49 assert_definition(
50 r#"
51 namespace Test {
52 operation ◉Callee() : Unit {
53 }
54
55 operation Caller() : Unit {
56 C↘allee();
57 }
58 }
59 "#,
60 );
61}
62
63#[test]
64fn variable() {
65 assert_definition(
66 r#"
67 namespace Test {
68 operation Foo() : Unit {
69 let ◉↘x = 3;
70 }
71 }
72 "#,
73 );
74}
75
76#[test]
77fn variable_ref() {
78 assert_definition(
79 r#"
80 namespace Test {
81 operation Foo() : Unit {
82 let ◉x = 3;
83 let y = ↘x;
84 }
85 }
86 "#,
87 );
88}
89
90#[test]
91fn parameter() {
92 assert_definition(
93 r#"
94 namespace Test {
95 operation Foo(◉↘x: Int) : Unit {
96 }
97 }
98 "#,
99 );
100}
101
102#[test]
103fn parameter_ref() {
104 assert_definition(
105 r#"
106 namespace Test {
107 operation Foo(◉x: Int) : Unit {
108 let y = ↘x;
109 }
110 }
111 "#,
112 );
113}
114
115#[test]
116fn udt() {
117 assert_definition(
118 r#"
119 namespace Test {
120 newtype ◉B↘ar = (a: Int, b: Double);
121 }
122 "#,
123 );
124}
125
126#[test]
127fn udt_ref() {
128 assert_definition(
129 r#"
130 namespace Test {
131 newtype ◉Bar = (a: Int, b: Double);
132
133 operation Foo() : Unit {
134 let x = B↘ar(1, 2.3);
135 }
136 }
137 "#,
138 );
139}
140
141#[test]
142fn udt_ref_sig() {
143 assert_definition(
144 r#"
145 namespace Test {
146 newtype ◉Bar = (a: Int, b: Double);
147
148 operation Foo() : B↘ar {
149 Bar(1, 2.3)
150 }
151 }
152 "#,
153 );
154}
155
156#[test]
157fn udt_ref_param() {
158 assert_definition(
159 r#"
160 namespace Test {
161 newtype ◉Bar = (a: Int, b: Double);
162
163 operation Foo(x: B↘ar) : Unit {
164 }
165 }
166 "#,
167 );
168}
169
170#[test]
171fn udt_ref_anno() {
172 assert_definition(
173 r#"
174 namespace Test {
175 newtype ◉Bar = (a: Int, b: Double);
176
177 operation Foo() : Unit {
178 let x: B↘ar = Bar(1, 2.3);
179 }
180 }
181 "#,
182 );
183}
184
185#[test]
186fn udt_ref_ty_def() {
187 assert_definition(
188 r#"
189 namespace Test {
190 newtype ◉Bar = (a: Int, b: Double);
191 newtype Foo = (a: B↘ar, b: Double);
192 }
193 "#,
194 );
195}
196
197#[test]
198fn udt_field() {
199 assert_definition(
200 r#"
201 namespace Test {
202 newtype Pair = (◉f↘st: Int, snd: Double);
203 }
204 "#,
205 );
206}
207
208#[test]
209fn udt_field_ref() {
210 assert_definition(
211 r#"
212 namespace Test {
213 newtype Pair = (fst: Int, ◉snd: Double);
214 operation Foo() : Unit {
215 let a = Pair(1, 2.3);
216 let b = a::s↘nd;
217 }
218 }
219 "#,
220 );
221}
222
223#[test]
224fn lambda_param() {
225 assert_definition(
226 r#"
227 namespace Test {
228 operation Foo() : Unit {
229 let local = (◉↘x, y) => x;
230 let z = local(1, 2.3);
231 }
232 }
233 "#,
234 );
235}
236
237#[test]
238fn lambda_param_ref() {
239 assert_definition(
240 r#"
241 namespace Test {
242 operation Foo() : Unit {
243 let local = (◉x, y) => ↘x;
244 let z = local(1, 2.3);
245 }
246 }
247 "#,
248 );
249}
250
251#[test]
252fn lambda_closure_ref() {
253 assert_definition(
254 r#"
255 namespace Test {
256 operation Foo() : Unit {
257 let ◉a = "Hello";
258 let local = (x, y) => ↘a;
259 let z = local(1, 2.3);
260 }
261 }
262 "#,
263 );
264}
265
266#[test]
267fn std_call() {
268 check(
269 r#"
270 namespace Test {
271 open FakeStdLib;
272 operation Foo() : Unit {
273 F↘ake();
274 }
275 }
276 "#,
277 &expect![[r#"
278 Some(
279 Definition {
280 source: "qsharp-library-source:<std>",
281 offset: 49,
282 },
283 )
284 "#]],
285 );
286}
287
288#[test]
289fn other_namespace_call_ref() {
290 assert_definition(
291 r#"
292 namespace Test {
293 open Other;
294 operation Foo() : Unit {
295 B↘ar();
296 }
297 }
298
299 namespace Other {
300 operation ◉Bar() : Unit {}
301 }
302 "#,
303 );
304}
305
306#[test]
307fn parameter_ref_with_body_specialization() {
308 assert_definition(
309 r#"
310 namespace Test {
311 operation Foo(◉x: Int) : Unit is Adj {
312 body ... {
313 let y = ↘x;
314 }
315 }
316 }
317 "#,
318 );
319}
320
321#[test]
322fn parameter_ref_with_adj_specialization() {
323 assert_definition(
324 r#"
325 namespace Test {
326 operation Foo(◉x: Int) : Unit is Adj {
327 body ... {}
328 adjoint ... {
329 let y = ↘x;
330 }
331 }
332 }
333 "#,
334 );
335}
336
337#[test]
338fn ctl_specialization_parameter() {
339 assert_definition(
340 r#"
341 namespace Test {
342 operation Foo(x: Int) : Unit is Ctl {
343 body ... {}
344 controlled (◉c↘s, ...) {}
345 }
346 }
347 "#,
348 );
349}
350
351#[test]
352fn ctl_specialization_parameter_ref() {
353 assert_definition(
354 r#"
355 namespace Test {
356 operation Foo(x: Int) : Unit is Ctl {
357 body ... {}
358 controlled (◉cs, ...) {
359 let y = c↘s;
360 }
361 }
362 }
363 "#,
364 );
365}