microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.0.33

Branches

Tags

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

Clone

HTTPS

Download ZIP

compiler/qsc_parse/src/ty/tests.rs

373lines · modecode

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::ty;
5use crate::tests::check;
6use expect_test::expect;
7
8#[test]
9fn ty_big_int() {
10 check(
11 ty,
12 "BigInt",
13 &expect![[r#"Type _id_ [0-6]: Path: Path _id_ [0-6] (Ident _id_ [0-6] "BigInt")"#]],
14 );
15}
16
17#[test]
18fn ty_bool() {
19 check(
20 ty,
21 "Bool",
22 &expect![[r#"Type _id_ [0-4]: Path: Path _id_ [0-4] (Ident _id_ [0-4] "Bool")"#]],
23 );
24}
25
26#[test]
27fn ty_double() {
28 check(
29 ty,
30 "Double",
31 &expect![[r#"Type _id_ [0-6]: Path: Path _id_ [0-6] (Ident _id_ [0-6] "Double")"#]],
32 );
33}
34
35#[test]
36fn ty_int() {
37 check(
38 ty,
39 "Int",
40 &expect![[r#"Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Int")"#]],
41 );
42}
43
44#[test]
45fn ty_pauli() {
46 check(
47 ty,
48 "Pauli",
49 &expect![[r#"Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Pauli")"#]],
50 );
51}
52
53#[test]
54fn ty_qubit() {
55 check(
56 ty,
57 "Qubit",
58 &expect![[r#"Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")"#]],
59 );
60}
61
62#[test]
63fn ty_range() {
64 check(
65 ty,
66 "Range",
67 &expect![[r#"Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Range")"#]],
68 );
69}
70
71#[test]
72fn ty_result() {
73 check(
74 ty,
75 "Result",
76 &expect![[r#"Type _id_ [0-6]: Path: Path _id_ [0-6] (Ident _id_ [0-6] "Result")"#]],
77 );
78}
79
80#[test]
81fn ty_string() {
82 check(
83 ty,
84 "String",
85 &expect![[r#"Type _id_ [0-6]: Path: Path _id_ [0-6] (Ident _id_ [0-6] "String")"#]],
86 );
87}
88
89#[test]
90fn ty_unit() {
91 check(
92 ty,
93 "Unit",
94 &expect![[r#"Type _id_ [0-4]: Path: Path _id_ [0-4] (Ident _id_ [0-4] "Unit")"#]],
95 );
96}
97
98#[test]
99fn ty_param() {
100 check(
101 ty,
102 "'T",
103 &expect![[r#"Type _id_ [0-2]: Type Param: Ident _id_ [0-2] "'T""#]],
104 );
105}
106
107#[test]
108fn ty_hole() {
109 check(ty, "_", &expect!["Type _id_ [0-1]: Hole"]);
110}
111
112#[test]
113fn ty_path() {
114 check(
115 ty,
116 "Foo",
117 &expect![[r#"Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Foo")"#]],
118 );
119}
120
121#[test]
122fn ty_path2() {
123 check(
124 ty,
125 "Foo.Bar",
126 &expect![[
127 r#"Type _id_ [0-7]: Path: Path _id_ [0-7] (Ident _id_ [0-3] "Foo") (Ident _id_ [4-7] "Bar")"#
128 ]],
129 );
130}
131
132#[test]
133fn ty_paren() {
134 check(
135 ty,
136 "(Int)",
137 &expect![[
138 r#"Type _id_ [0-5]: Paren: Type _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "Int")"#
139 ]],
140 );
141}
142
143#[test]
144fn ty_singleton_tuple() {
145 check(
146 ty,
147 "(Int,)",
148 &expect![[r#"
149 Type _id_ [0-6]: Tuple:
150 Type _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "Int")"#]],
151 );
152}
153
154#[test]
155fn ty_tuple() {
156 check(
157 ty,
158 "(Int, Bool)",
159 &expect![[r#"
160 Type _id_ [0-11]: Tuple:
161 Type _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "Int")
162 Type _id_ [6-10]: Path: Path _id_ [6-10] (Ident _id_ [6-10] "Bool")"#]],
163 );
164}
165
166#[test]
167fn ty_tuple2() {
168 check(
169 ty,
170 "((Int, Bool), Double)",
171 &expect![[r#"
172 Type _id_ [0-21]: Tuple:
173 Type _id_ [1-12]: Tuple:
174 Type _id_ [2-5]: Path: Path _id_ [2-5] (Ident _id_ [2-5] "Int")
175 Type _id_ [7-11]: Path: Path _id_ [7-11] (Ident _id_ [7-11] "Bool")
176 Type _id_ [14-20]: Path: Path _id_ [14-20] (Ident _id_ [14-20] "Double")"#]],
177 );
178}
179
180#[test]
181fn ty_array() {
182 check(
183 ty,
184 "Int[]",
185 &expect![[
186 r#"Type _id_ [0-5]: Array: Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Int")"#
187 ]],
188 );
189}
190
191#[test]
192fn ty_array2() {
193 check(
194 ty,
195 "Int[][]",
196 &expect![[
197 r#"Type _id_ [0-7]: Array: Type _id_ [0-5]: Array: Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Int")"#
198 ]],
199 );
200}
201
202#[test]
203fn ty_tuple_array() {
204 check(
205 ty,
206 "(Int, Bool)[]",
207 &expect![[r#"
208 Type _id_ [0-13]: Array: Type _id_ [0-11]: Tuple:
209 Type _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "Int")
210 Type _id_ [6-10]: Path: Path _id_ [6-10] (Ident _id_ [6-10] "Bool")"#]],
211 );
212}
213
214#[test]
215fn ty_function() {
216 check(
217 ty,
218 "Int -> Int",
219 &expect![[r#"
220 Type _id_ [0-10]: Arrow (Function):
221 param: Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Int")
222 return: Type _id_ [7-10]: Path: Path _id_ [7-10] (Ident _id_ [7-10] "Int")"#]],
223 );
224}
225
226#[test]
227fn ty_operation() {
228 check(
229 ty,
230 "Int => Int",
231 &expect![[r#"
232 Type _id_ [0-10]: Arrow (Operation):
233 param: Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Int")
234 return: Type _id_ [7-10]: Path: Path _id_ [7-10] (Ident _id_ [7-10] "Int")"#]],
235 );
236}
237
238#[test]
239fn ty_curried_function() {
240 check(
241 ty,
242 "Int -> Int -> Int",
243 &expect![[r#"
244 Type _id_ [0-17]: Arrow (Function):
245 param: Type _id_ [0-3]: Path: Path _id_ [0-3] (Ident _id_ [0-3] "Int")
246 return: Type _id_ [7-17]: Arrow (Function):
247 param: Type _id_ [7-10]: Path: Path _id_ [7-10] (Ident _id_ [7-10] "Int")
248 return: Type _id_ [14-17]: Path: Path _id_ [14-17] (Ident _id_ [14-17] "Int")"#]],
249 );
250}
251
252#[test]
253fn ty_higher_order_function() {
254 check(
255 ty,
256 "(Int -> Int) -> Int",
257 &expect![[r#"
258 Type _id_ [0-19]: Arrow (Function):
259 param: Type _id_ [0-12]: Paren: Type _id_ [1-11]: Arrow (Function):
260 param: Type _id_ [1-4]: Path: Path _id_ [1-4] (Ident _id_ [1-4] "Int")
261 return: Type _id_ [8-11]: Path: Path _id_ [8-11] (Ident _id_ [8-11] "Int")
262 return: Type _id_ [16-19]: Path: Path _id_ [16-19] (Ident _id_ [16-19] "Int")"#]],
263 );
264}
265
266#[test]
267fn op_ty_is_adj() {
268 check(
269 ty,
270 "Qubit => Unit is Adj",
271 &expect![[r#"
272 Type _id_ [0-20]: Arrow (Operation):
273 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
274 return: Type _id_ [9-13]: Path: Path _id_ [9-13] (Ident _id_ [9-13] "Unit")
275 functors: Functor Expr _id_ [17-20]: Adj"#]],
276 );
277}
278
279#[test]
280fn op_ty_is_adj_ctl() {
281 check(
282 ty,
283 "Qubit => Unit is Adj + Ctl",
284 &expect![[r#"
285 Type _id_ [0-26]: Arrow (Operation):
286 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
287 return: Type _id_ [9-13]: Path: Path _id_ [9-13] (Ident _id_ [9-13] "Unit")
288 functors: Functor Expr _id_ [17-26]: BinOp Union: (Functor Expr _id_ [17-20]: Adj) (Functor Expr _id_ [23-26]: Ctl)"#]],
289 );
290}
291
292#[test]
293fn op_ty_is_nested() {
294 check(
295 ty,
296 "Qubit => Qubit => Unit is Adj is Ctl",
297 &expect![[r#"
298 Type _id_ [0-36]: Arrow (Operation):
299 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
300 return: Type _id_ [9-29]: Arrow (Operation):
301 param: Type _id_ [9-14]: Path: Path _id_ [9-14] (Ident _id_ [9-14] "Qubit")
302 return: Type _id_ [18-22]: Path: Path _id_ [18-22] (Ident _id_ [18-22] "Unit")
303 functors: Functor Expr _id_ [26-29]: Adj
304 functors: Functor Expr _id_ [33-36]: Ctl"#]],
305 );
306}
307
308#[test]
309fn op_ty_is_nested_paren() {
310 check(
311 ty,
312 "Qubit => (Qubit => Unit) is Ctl",
313 &expect![[r#"
314 Type _id_ [0-31]: Arrow (Operation):
315 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
316 return: Type _id_ [9-24]: Paren: Type _id_ [10-23]: Arrow (Operation):
317 param: Type _id_ [10-15]: Path: Path _id_ [10-15] (Ident _id_ [10-15] "Qubit")
318 return: Type _id_ [19-23]: Path: Path _id_ [19-23] (Ident _id_ [19-23] "Unit")
319 functors: Functor Expr _id_ [28-31]: Ctl"#]],
320 );
321}
322
323#[test]
324fn op_ty_is_paren() {
325 check(
326 ty,
327 "Qubit => Unit is (Adj)",
328 &expect![[r#"
329 Type _id_ [0-22]: Arrow (Operation):
330 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
331 return: Type _id_ [9-13]: Path: Path _id_ [9-13] (Ident _id_ [9-13] "Unit")
332 functors: Functor Expr _id_ [17-22]: Paren: Functor Expr _id_ [18-21]: Adj"#]],
333 );
334}
335
336#[test]
337fn op_ty_union_assoc() {
338 check(
339 ty,
340 "Qubit => Unit is Adj + Adj + Adj",
341 &expect![[r#"
342 Type _id_ [0-32]: Arrow (Operation):
343 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
344 return: Type _id_ [9-13]: Path: Path _id_ [9-13] (Ident _id_ [9-13] "Unit")
345 functors: Functor Expr _id_ [17-32]: BinOp Union: (Functor Expr _id_ [17-26]: BinOp Union: (Functor Expr _id_ [17-20]: Adj) (Functor Expr _id_ [23-26]: Adj)) (Functor Expr _id_ [29-32]: Adj)"#]],
346 );
347}
348
349#[test]
350fn op_ty_intersect_assoc() {
351 check(
352 ty,
353 "Qubit => Unit is Adj * Adj * Adj",
354 &expect![[r#"
355 Type _id_ [0-32]: Arrow (Operation):
356 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
357 return: Type _id_ [9-13]: Path: Path _id_ [9-13] (Ident _id_ [9-13] "Unit")
358 functors: Functor Expr _id_ [17-32]: BinOp Intersect: (Functor Expr _id_ [17-26]: BinOp Intersect: (Functor Expr _id_ [17-20]: Adj) (Functor Expr _id_ [23-26]: Adj)) (Functor Expr _id_ [29-32]: Adj)"#]],
359 );
360}
361
362#[test]
363fn op_ty_is_prec() {
364 check(
365 ty,
366 "Qubit => Unit is Adj + Adj * Ctl",
367 &expect![[r#"
368 Type _id_ [0-32]: Arrow (Operation):
369 param: Type _id_ [0-5]: Path: Path _id_ [0-5] (Ident _id_ [0-5] "Qubit")
370 return: Type _id_ [9-13]: Path: Path _id_ [9-13] (Ident _id_ [9-13] "Unit")
371 functors: Functor Expr _id_ [17-32]: BinOp Union: (Functor Expr _id_ [17-20]: Adj) (Functor Expr _id_ [23-32]: BinOp Intersect: (Functor Expr _id_ [23-26]: Adj) (Functor Expr _id_ [29-32]: Ctl))"#]],
372 );
373}