microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
language_service/src/completion/tests.rs
322lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use expect_test::{expect, Expect}; |
| 5 | |
| 6 | use super::{get_completions, CompletionItem}; |
| 7 | use crate::test_utils::{compile_with_fake_stdlib, get_source_and_marker_offsets}; |
| 8 | |
| 9 | fn check(source_with_cursor: &str, completions_to_check: &[&str], expect: &Expect) { |
| 10 | let (source, cursor_offset, _) = get_source_and_marker_offsets(source_with_cursor); |
| 11 | let compilation = compile_with_fake_stdlib("<source>", &source); |
| 12 | let actual_completions = get_completions(&compilation, "<source>", cursor_offset[0]); |
| 13 | let checked_completions: Vec<Option<&CompletionItem>> = completions_to_check |
| 14 | .iter() |
| 15 | .map(|comp| { |
| 16 | actual_completions |
| 17 | .items |
| 18 | .iter() |
| 19 | .find(|item| item.label == **comp) |
| 20 | }) |
| 21 | .collect(); |
| 22 | |
| 23 | expect.assert_debug_eq(&checked_completions); |
| 24 | } |
| 25 | |
| 26 | #[test] |
| 27 | fn in_block_contains_std_functions() { |
| 28 | check( |
| 29 | r#" |
| 30 | namespace Test { |
| 31 | operation Test() : Unit { |
| 32 | ↘ |
| 33 | } |
| 34 | }"#, |
| 35 | &["Fake", "FakeWithParam", "FakeCtlAdj"], |
| 36 | &expect![[r#" |
| 37 | [ |
| 38 | Some( |
| 39 | CompletionItem { |
| 40 | label: "Fake", |
| 41 | kind: Function, |
| 42 | sort_text: Some( |
| 43 | "0600Fake", |
| 44 | ), |
| 45 | detail: Some( |
| 46 | "operation Fake() : Unit", |
| 47 | ), |
| 48 | additional_text_edits: Some( |
| 49 | [ |
| 50 | ( |
| 51 | Span { |
| 52 | start: 30, |
| 53 | end: 30, |
| 54 | }, |
| 55 | "open FakeStdLib;\n ", |
| 56 | ), |
| 57 | ], |
| 58 | ), |
| 59 | }, |
| 60 | ), |
| 61 | Some( |
| 62 | CompletionItem { |
| 63 | label: "FakeWithParam", |
| 64 | kind: Function, |
| 65 | sort_text: Some( |
| 66 | "0600FakeWithParam", |
| 67 | ), |
| 68 | detail: Some( |
| 69 | "operation FakeWithParam(x : Int) : Unit", |
| 70 | ), |
| 71 | additional_text_edits: Some( |
| 72 | [ |
| 73 | ( |
| 74 | Span { |
| 75 | start: 30, |
| 76 | end: 30, |
| 77 | }, |
| 78 | "open FakeStdLib;\n ", |
| 79 | ), |
| 80 | ], |
| 81 | ), |
| 82 | }, |
| 83 | ), |
| 84 | Some( |
| 85 | CompletionItem { |
| 86 | label: "FakeCtlAdj", |
| 87 | kind: Function, |
| 88 | sort_text: Some( |
| 89 | "0600FakeCtlAdj", |
| 90 | ), |
| 91 | detail: Some( |
| 92 | "operation FakeCtlAdj() : Unit is Adj + Ctl", |
| 93 | ), |
| 94 | additional_text_edits: Some( |
| 95 | [ |
| 96 | ( |
| 97 | Span { |
| 98 | start: 30, |
| 99 | end: 30, |
| 100 | }, |
| 101 | "open FakeStdLib;\n ", |
| 102 | ), |
| 103 | ], |
| 104 | ), |
| 105 | }, |
| 106 | ), |
| 107 | ] |
| 108 | "#]], |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn in_block_no_auto_open() { |
| 114 | check( |
| 115 | r#" |
| 116 | namespace Test { |
| 117 | open FakeStdLib; |
| 118 | operation Test() : Unit { |
| 119 | ↘ |
| 120 | } |
| 121 | }"#, |
| 122 | &["Fake"], |
| 123 | &expect![[r#" |
| 124 | [ |
| 125 | Some( |
| 126 | CompletionItem { |
| 127 | label: "Fake", |
| 128 | kind: Function, |
| 129 | sort_text: Some( |
| 130 | "0600Fake", |
| 131 | ), |
| 132 | detail: Some( |
| 133 | "operation Fake() : Unit", |
| 134 | ), |
| 135 | additional_text_edits: None, |
| 136 | }, |
| 137 | ), |
| 138 | ] |
| 139 | "#]], |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn in_block_with_alias() { |
| 145 | check( |
| 146 | r#" |
| 147 | namespace Test { |
| 148 | open FakeStdLib as Alias; |
| 149 | operation Test() : Unit { |
| 150 | ↘ |
| 151 | } |
| 152 | }"#, |
| 153 | &["Alias.Fake"], |
| 154 | &expect![[r#" |
| 155 | [ |
| 156 | Some( |
| 157 | CompletionItem { |
| 158 | label: "Alias.Fake", |
| 159 | kind: Function, |
| 160 | sort_text: Some( |
| 161 | "0600Alias.Fake", |
| 162 | ), |
| 163 | detail: Some( |
| 164 | "operation Fake() : Unit", |
| 165 | ), |
| 166 | additional_text_edits: None, |
| 167 | }, |
| 168 | ), |
| 169 | ] |
| 170 | "#]], |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn in_block_from_other_namespace() { |
| 176 | check( |
| 177 | r#" |
| 178 | namespace Test { |
| 179 | operation Test() : Unit { |
| 180 | ↘ |
| 181 | } |
| 182 | } |
| 183 | namespace Other { |
| 184 | operation Foo() : Unit {} |
| 185 | }"#, |
| 186 | &["Foo"], |
| 187 | &expect![[r#" |
| 188 | [ |
| 189 | Some( |
| 190 | CompletionItem { |
| 191 | label: "Foo", |
| 192 | kind: Function, |
| 193 | sort_text: Some( |
| 194 | "0500Foo", |
| 195 | ), |
| 196 | detail: Some( |
| 197 | "operation Foo() : Unit", |
| 198 | ), |
| 199 | additional_text_edits: Some( |
| 200 | [ |
| 201 | ( |
| 202 | Span { |
| 203 | start: 30, |
| 204 | end: 30, |
| 205 | }, |
| 206 | "open Other;\n ", |
| 207 | ), |
| 208 | ], |
| 209 | ), |
| 210 | }, |
| 211 | ), |
| 212 | ] |
| 213 | "#]], |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | #[ignore = "nested callables are not currently supported for completions"] |
| 218 | #[test] |
| 219 | fn in_block_nested_op() { |
| 220 | check( |
| 221 | r#" |
| 222 | namespace Test { |
| 223 | operation Test() : Unit { |
| 224 | operation Foo() : Unit {} |
| 225 | ↘ |
| 226 | } |
| 227 | }"#, |
| 228 | &["Foo"], |
| 229 | &expect![[r#" |
| 230 | [ |
| 231 | Some( |
| 232 | CompletionItem { |
| 233 | label: "Foo", |
| 234 | kind: Function, |
| 235 | sort_text: Some( |
| 236 | "0500Foo", |
| 237 | ), |
| 238 | detail: Some( |
| 239 | "operation Foo() : Unit", |
| 240 | ), |
| 241 | additional_text_edits: None, |
| 242 | }, |
| 243 | ), |
| 244 | ] |
| 245 | "#]], |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | #[test] |
| 250 | fn in_block_hidden_nested_op() { |
| 251 | check( |
| 252 | r#" |
| 253 | namespace Test { |
| 254 | operation Test() : Unit { |
| 255 | ↘ |
| 256 | } |
| 257 | operation Foo() : Unit { |
| 258 | operation Bar() : Unit {} |
| 259 | } |
| 260 | }"#, |
| 261 | &["Bar"], |
| 262 | &expect![[r#" |
| 263 | [ |
| 264 | None, |
| 265 | ] |
| 266 | "#]], |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | #[test] |
| 271 | fn in_namespace_contains_open() { |
| 272 | check( |
| 273 | r#" |
| 274 | namespace Test { |
| 275 | ↘ |
| 276 | operation Test() : Unit { |
| 277 | } |
| 278 | }"#, |
| 279 | &["open"], |
| 280 | &expect![[r#" |
| 281 | [ |
| 282 | Some( |
| 283 | CompletionItem { |
| 284 | label: "open", |
| 285 | kind: Keyword, |
| 286 | sort_text: Some( |
| 287 | "0102open", |
| 288 | ), |
| 289 | detail: None, |
| 290 | additional_text_edits: None, |
| 291 | }, |
| 292 | ), |
| 293 | ] |
| 294 | "#]], |
| 295 | ); |
| 296 | } |
| 297 | |
| 298 | #[test] |
| 299 | fn top_level_contains_namespace() { |
| 300 | check( |
| 301 | r#" |
| 302 | namespace Test {} |
| 303 | ↘ |
| 304 | "#, |
| 305 | &["namespace"], |
| 306 | &expect![[r#" |
| 307 | [ |
| 308 | Some( |
| 309 | CompletionItem { |
| 310 | label: "namespace", |
| 311 | kind: Keyword, |
| 312 | sort_text: Some( |
| 313 | "0101namespace", |
| 314 | ), |
| 315 | detail: None, |
| 316 | additional_text_edits: None, |
| 317 | }, |
| 318 | ), |
| 319 | ] |
| 320 | "#]], |
| 321 | ); |
| 322 | } |
| 323 | |