microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.25.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

source/language_service/src/rename/tests.rs

729lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::{get_rename, prepare_rename};
5use crate::{
6 Encoding,
7 test_utils::{compile_notebook_with_markers, compile_with_markers},
8};
9use expect_test::{Expect, expect};
10
11/// Asserts that the rename locations given at the cursor position matches the expected rename locations.
12/// The cursor position is indicated by a `↘` marker in the source text.
13/// The expected rename location ranges are indicated by `◉` markers in the source text.
14fn check(source_with_markers: &str) {
15 let (compilation, cursor_position, target_spans) =
16 compile_with_markers(source_with_markers, true);
17 let actual = get_rename(&compilation, "<source>", cursor_position, Encoding::Utf8)
18 .into_iter()
19 .map(|l| l.range)
20 .collect::<Vec<_>>();
21 for target in &target_spans {
22 assert!(actual.contains(target));
23 }
24 assert!(target_spans.len() == actual.len());
25}
26
27/// Asserts that the prepare rename given at the cursor position returns None.
28/// The cursor position is indicated by a `↘` marker in the source text.
29fn assert_no_rename(source_with_markers: &str) {
30 let (compilation, cursor_position, _) = compile_with_markers(source_with_markers, true);
31 let actual = prepare_rename(&compilation, "<source>", cursor_position, Encoding::Utf8);
32 assert!(actual.is_none());
33}
34
35fn check_notebook(cells_with_markers: &[(&str, &str)], expect: &Expect) {
36 let (compilation, cell_uri, position, _) = compile_notebook_with_markers(cells_with_markers);
37 let actual = get_rename(&compilation, &cell_uri, position, Encoding::Utf8);
38 expect.assert_debug_eq(&actual);
39}
40
41fn check_prepare_notebook(cells_with_markers: &[(&str, &str)], expect: &Expect) {
42 let (compilation, cell_uri, position, _) = compile_notebook_with_markers(cells_with_markers);
43 let actual = prepare_rename(&compilation, &cell_uri, position, Encoding::Utf8);
44 expect.assert_debug_eq(&actual);
45}
46
47#[test]
48fn callable_def() {
49 check(
50 r#"
51 namespace Test {
52 operation ◉Fo↘o◉(x : Int, y : Int, z : Int) : Unit {
53 ◉Foo◉(x, y, z);
54 }
55 operation Bar(x : Int, y : Int, z : Int) : Unit {
56 ◉Foo◉(x, y, z);
57 }
58 }
59 "#,
60 );
61}
62
63#[test]
64fn callable_ref() {
65 check(
66 r#"
67 namespace Test {
68 operation ◉Foo◉(x : Int, y : Int, z : Int) : Unit {
69 ◉Foo◉(x, y, z);
70 }
71 operation Bar(x : Int, y : Int, z : Int) : Unit {
72 ◉Fo↘o◉(x, y, z);
73 }
74 }
75 "#,
76 );
77}
78
79#[test]
80fn parameter_def() {
81 check(
82 r#"
83 namespace Test {
84 operation Foo(◉↘x◉ : Int, y : Int, z : Int) : Unit {
85 let temp = ◉x◉;
86 Foo(◉x◉, y, z);
87 }
88 }
89 "#,
90 );
91}
92
93#[test]
94fn parameter_ref() {
95 check(
96 r#"
97 namespace Test {
98 operation Foo(◉x◉ : Int, y : Int, z : Int) : Unit {
99 let temp = ◉x◉;
100 Foo(◉↘x◉, y, z);
101 }
102 }
103 "#,
104 );
105}
106
107#[test]
108fn local_def() {
109 check(
110 r#"
111 namespace Test {
112 operation Foo(x : Int, y : Int, z : Int) : Unit {
113 let ◉t↘emp◉ = x;
114 Foo(◉temp◉, y, ◉temp◉);
115 }
116 }
117 "#,
118 );
119}
120
121#[test]
122fn local_ref() {
123 check(
124 r#"
125 namespace Test {
126 operation Foo(x : Int, y : Int, z : Int) : Unit {
127 let ◉temp◉ = x;
128 Foo(◉t↘emp◉, y, ◉temp◉);
129 }
130 }
131 "#,
132 );
133}
134
135#[test]
136fn udt_def() {
137 check(
138 r#"
139 namespace Test {
140 newtype ◉F↘oo◉ = (fst : Int, snd : Int);
141 operation Bar(x : ◉Foo◉) : Unit {
142 let temp = ◉Foo◉(1, 2);
143 Bar(temp);
144 }
145 }
146 "#,
147 );
148}
149
150#[test]
151fn udt_constructor_ref() {
152 check(
153 r#"
154 namespace Test {
155 newtype ◉Foo◉ = (fst : Int, snd : Int);
156 operation Bar(x : ◉Foo◉) : Unit {
157 let temp = ◉F↘oo◉(1, 2);
158 Bar(temp);
159 }
160 }
161 "#,
162 );
163}
164
165#[test]
166fn udt_ref() {
167 check(
168 r#"
169 namespace Test {
170 newtype ◉Foo◉ = (fst : Int, snd : Int);
171 operation Bar(x : ◉F↘oo◉) : Unit {
172 let temp = ◉Foo◉(1, 2);
173 Bar(temp);
174 }
175 }
176 "#,
177 );
178}
179
180#[test]
181fn udt_field_def() {
182 check(
183 r#"
184 namespace Test {
185 newtype Foo = (◉f↘st◉ : Int, snd : Int);
186 operation Bar(x : Foo) : Unit {
187 let temp = Foo(1, 2);
188 let a = temp::◉fst◉;
189 let b = Zip()::◉fst◉;
190 }
191 operation Zip() : Foo {
192 Foo(1, 2)
193 }
194 }
195 "#,
196 );
197}
198
199#[test]
200fn udt_field_ref() {
201 check(
202 r#"
203 namespace Test {
204 newtype Foo = (◉fst◉ : Int, snd : Int);
205 operation Bar(x : Foo) : Unit {
206 let temp = Foo(1, 2);
207 let a = temp::◉f↘st◉;
208 let b = Zip()::◉fst◉;
209 }
210 operation Zip() : Foo {
211 Foo(1, 2)
212 }
213 }
214 "#,
215 );
216}
217
218#[test]
219fn udt_field_complex_ref() {
220 check(
221 r#"
222 namespace Test {
223 newtype Foo = (◉fst◉ : Int, snd : Int);
224 operation Bar(x : Foo) : Unit {
225 let temp = Foo(1, 2);
226 let a = temp::◉fst◉;
227 let b = Zip()::◉f↘st◉;
228 }
229 operation Zip() : Foo {
230 Foo(1, 2)
231 }
232 }
233 "#,
234 );
235}
236
237#[test]
238fn struct_def() {
239 check(
240 r#"
241 namespace Test {
242 struct ◉F↘oo◉ { fst : Int, snd : Int }
243 operation Bar(x : ◉Foo◉) : Unit {
244 let temp = ◉Foo◉(1, 2);
245 let temp = new ◉Foo◉ { fst = 1, snd = 2 };
246 Bar(temp);
247 }
248 }
249 "#,
250 );
251}
252
253#[test]
254fn struct_fn_constructor_ref() {
255 check(
256 r#"
257 namespace Test {
258 struct ◉Foo◉ { fst : Int, snd : Int }
259 operation Bar(x : ◉Foo◉) : Unit {
260 let temp = ◉F↘oo◉(1, 2);
261 let temp = new ◉Foo◉ { fst = 1, snd = 2 };
262 Bar(temp);
263 }
264 }
265 "#,
266 );
267}
268
269#[test]
270fn struct_constructor_ref() {
271 check(
272 r#"
273 namespace Test {
274 struct ◉Foo◉ { fst : Int, snd : Int }
275 operation Bar(x : ◉Foo◉) : Unit {
276 let temp = ◉Foo◉(1, 2);
277 let temp = new ◉F↘oo◉ { fst = 1, snd = 2 };
278 Bar(temp);
279 }
280 }
281 "#,
282 );
283}
284
285#[test]
286fn struct_ref() {
287 check(
288 r#"
289 namespace Test {
290 struct ◉Foo◉ { fst : Int, snd : Int }
291 operation Bar(x : ◉F↘oo◉) : Unit {
292 let temp = ◉Foo◉(1, 2);
293 let temp = new ◉F↘oo◉ { fst = 1, snd = 2 };
294 Bar(temp);
295 }
296 }
297 "#,
298 );
299}
300
301#[test]
302fn struct_field_def() {
303 check(
304 r#"
305 namespace Test {
306 struct Foo { ◉f↘st◉ : Int, snd : Int }
307 operation Bar(x : Foo) : Unit {
308 let temp = Foo(1, 2);
309 let temp = new Foo { ◉fst◉ = 1, snd = 2 };
310 let a = temp::◉fst◉;
311 let b = Zip()::◉fst◉;
312 }
313 operation Zip() : Foo {
314 Foo(1, 2)
315 }
316 }
317 "#,
318 );
319}
320
321#[test]
322fn struct_field_cons_ref() {
323 check(
324 r#"
325 namespace Test {
326 struct Foo { ◉fst◉ : Int, snd : Int }
327 operation Bar(x : Foo) : Unit {
328 let temp = Foo(1, 2);
329 let temp = new Foo { ◉f↘st◉ = 1, snd = 2 };
330 let a = temp::◉fst◉;
331 let b = Zip()::◉fst◉;
332 }
333 operation Zip() : Foo {
334 Foo(1, 2)
335 }
336 }
337 "#,
338 );
339}
340
341#[test]
342fn struct_field_ref() {
343 check(
344 r#"
345 namespace Test {
346 struct Foo { ◉fst◉ : Int, snd : Int }
347 operation Bar(x : Foo) : Unit {
348 let temp = Foo(1, 2);
349 let temp = new Foo { ◉fst◉ = 1, snd = 2 };
350 let a = temp::◉f↘st◉;
351 let b = Zip()::◉fst◉;
352 }
353 operation Zip() : Foo {
354 Foo(1, 2)
355 }
356 }
357 "#,
358 );
359}
360
361#[test]
362fn struct_field_complex_ref() {
363 check(
364 r#"
365 namespace Test {
366 struct Foo { ◉fst◉ : Int, snd : Int }
367 operation Bar(x : Foo) : Unit {
368 let temp = Foo(1, 2);
369 let temp = new Foo { ◉fst◉ = 1, snd = 2 };
370 let a = temp::◉fst◉;
371 let b = Zip()::◉f↘st◉;
372 }
373 operation Zip() : Foo {
374 Foo(1, 2)
375 }
376 }
377 "#,
378 );
379}
380
381#[test]
382fn struct_field_path_def() {
383 check(
384 r#"
385 namespace Test {
386 struct A { b : B }
387 struct B { ◉↘c◉ : C }
388 struct C { i : Int }
389 operation Foo(a : A) : Unit {
390 let x = a.b.◉c◉.i;
391 }
392 }
393 "#,
394 );
395}
396
397#[test]
398fn struct_field_path_ref() {
399 check(
400 r#"
401 namespace Test {
402 struct A { b : B }
403 struct B { ◉c◉ : C }
404 struct C { i : Int }
405 operation Foo(a : A) : Unit {
406 let x = a.b.◉↘c◉.i;
407 }
408 }
409 "#,
410 );
411}
412
413#[test]
414fn struct_field_path_first_def() {
415 check(
416 r#"
417 namespace Test {
418 struct A { b : B }
419 struct B { c : C }
420 struct C { i : Int }
421 operation Foo(◉↘a◉ : A) : Unit {
422 let x = ◉a◉.b.c.i;
423 }
424 }
425 "#,
426 );
427}
428
429#[test]
430fn struct_field_path_first_ref() {
431 check(
432 r#"
433 namespace Test {
434 struct A { b : B }
435 struct B { c : C }
436 struct C { i : Int }
437 operation Foo(◉a◉ : A) : Unit {
438 let x = ◉↘a◉.b.c.i;
439 }
440 }
441 "#,
442 );
443}
444
445#[test]
446fn struct_field_path_with_expr_def() {
447 check(
448 r#"
449 namespace Test {
450 struct A { ◉↘b◉ : B }
451 struct B { c : C }
452 struct C { i : Int }
453 operation Foo(a : A) : Unit {
454 let x = { a.◉b◉ }.c.i;
455 }
456 }
457 "#,
458 );
459}
460
461#[test]
462fn struct_field_path_with_expr_ref() {
463 check(
464 r#"
465 namespace Test {
466 struct A { ◉b◉ : B }
467 struct B { c : C }
468 struct C { i : Int }
469 operation Foo(a : A) : Unit {
470 let x = { a.◉↘b◉ }.c.i;
471 }
472 }
473 "#,
474 );
475}
476
477#[test]
478fn no_rename_namespace() {
479 assert_no_rename(
480 r#"
481 namespace Te↘st {
482 operation Foo() : Unit {}
483
484 }
485 "#,
486 );
487}
488
489#[test]
490fn no_rename_keyword() {
491 assert_no_rename(
492 r#"
493 namespace Test {
494 ope↘ration Foo() : Unit {}
495
496 }
497 "#,
498 );
499}
500
501#[test]
502fn no_rename_non_udt_type() {
503 assert_no_rename(
504 r#"
505 namespace Test {
506 operation Foo() : Un↘it {}
507
508 }
509 "#,
510 );
511}
512
513#[test]
514fn no_rename_string() {
515 assert_no_rename(
516 r#"
517 namespace Test {
518 operation Foo() : Unit {
519 let temp = "He↘llo World!"
520 }
521
522 }
523 "#,
524 );
525}
526
527#[test]
528fn no_rename_comment() {
529 assert_no_rename(
530 r#"
531 namespace Test {
532 // He↘llo World!
533 operation Foo() : Unit {}
534
535 }
536 "#,
537 );
538}
539
540#[test]
541fn no_rename_std_item() {
542 assert_no_rename(
543 r#"
544 namespace Test {
545 operation Foo() : Unit {
546 F↘ake();
547 }
548
549 }
550 "#,
551 );
552}
553
554#[test]
555fn no_rename_non_id_character() {
556 assert_no_rename(
557 r#"
558 namespace Test {
559 operation Foo() ↘: Unit {
560 Fake();
561 }
562
563 }
564 "#,
565 );
566}
567
568#[test]
569fn no_rename_std_udt_return_type() {
570 assert_no_rename(
571 r#"
572 namespace Test {
573 open FakeStdLib;
574 operation Foo() : U↘dt {
575 }
576 }
577 "#,
578 );
579}
580
581#[test]
582fn no_rename_std_struct_return_type() {
583 assert_no_rename(
584 r#"
585 namespace Test {
586 open FakeStdLib;
587 operation Foo() : FakeS↘truct {}
588 }
589 "#,
590 );
591}
592
593#[test]
594fn ty_param_def() {
595 check(
596 r#"
597 namespace Test {
598 operation Foo<'◉↘T◉>(x : '◉T◉) : '◉T◉ { x }
599 }
600 "#,
601 );
602}
603
604#[test]
605fn ty_param_ref() {
606 check(
607 r#"
608 namespace Test {
609 operation Foo<'◉T◉>(x : '◉↘T◉) : '◉T◉ { x }
610 }
611 "#,
612 );
613}
614
615#[test]
616fn notebook_rename_defined_in_later_cell() {
617 check_prepare_notebook(
618 &[
619 ("cell1", "C↘allee();"),
620 ("cell2", "operation Callee() : Unit {}"),
621 ],
622 &expect![[r#"
623 None
624 "#]],
625 );
626}
627
628#[test]
629fn notebook_rename_across_cells() {
630 check_notebook(
631 &[
632 ("cell1", "operation Callee() : Unit {}"),
633 ("cell2", "◉C↘allee◉();"),
634 ],
635 &expect![[r#"
636 [
637 Location {
638 source: "cell1",
639 range: Range {
640 start: Position {
641 line: 0,
642 column: 10,
643 },
644 end: Position {
645 line: 0,
646 column: 16,
647 },
648 },
649 },
650 Location {
651 source: "cell2",
652 range: Range {
653 start: Position {
654 line: 0,
655 column: 0,
656 },
657 end: Position {
658 line: 0,
659 column: 6,
660 },
661 },
662 },
663 ]
664 "#]],
665 );
666}
667
668#[test]
669fn on_declaration_with_aliased_export_finds_only_matching_names() {
670 check(
671 r#"
672 namespace Test {
673 operation ◉F↘oo◉() : Unit {
674 }
675 export ◉Foo◉ as Bar;
676 }
677 namespace Other {
678 import Test.Bar;
679 import Test.Bar as Baz;
680 operation X() : Unit {
681 Bar();
682 Baz();
683 }
684 }
685 "#,
686 );
687}
688
689#[test]
690fn on_export_alias_finds_only_matching_names() {
691 check(
692 r#"
693 namespace Test {
694 operation Foo() : Unit {
695 }
696 export Foo as Bar;
697 }
698 namespace Other {
699 import Test.Bar;
700 import Test.Bar as ◉Ba↘z◉;
701 operation X() : Unit {
702 Bar();
703 ◉Baz◉();
704 }
705 }
706 "#,
707 );
708}
709
710#[test]
711fn on_export_alias_usage_finds_only_matching_names() {
712 check(
713 r#"
714 namespace Test {
715 operation Foo() : Unit {
716 }
717 export Foo as Bar;
718 }
719 namespace Other {
720 import Test.Bar;
721 import Test.Bar as ◉Baz◉;
722 operation X() : Unit {
723 Bar();
724 ◉B↘az◉();
725 }
726 }
727 "#,
728 );
729}
730