Skip to content

Commit 144e9e6

Browse files
committed
C#: C#9 Add target typed conditional tests
1 parent ad4b2be commit 144e9e6

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

csharp/ql/test/library-tests/csharp9/PrintAst.expected

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,58 @@ ParenthesizedPattern.cs:
335335
# 27| 0: [TypeAccessPatternExpr] access to type String
336336
# 27| 0: [TypeMention] string
337337
# 27| 2: [IntLiteral] 5
338+
TargetType.cs:
339+
# 5| [Class] TargetType
340+
# 7| 5: [Method] M2
341+
# 7| -1: [TypeMention] Void
342+
# 8| 4: [BlockStmt] {...}
343+
# 9| 0: [LocalVariableDeclStmt] ... ...;
344+
# 9| 0: [LocalVariableDeclAndInitExpr] Random rand = ...
345+
# 9| -1: [TypeMention] Random
346+
# 9| 0: [LocalVariableAccess] access to local variable rand
347+
# 9| 1: [ObjectCreation] object creation of type Random
348+
# 9| 0: [TypeMention] Random
349+
# 10| 1: [LocalVariableDeclStmt] ... ...;
350+
# 10| 0: [LocalVariableDeclAndInitExpr] Boolean condition = ...
351+
# 10| -1: [TypeMention] bool
352+
# 10| 0: [LocalVariableAccess] access to local variable condition
353+
# 10| 1: [GTExpr] ... > ...
354+
# 10| 0: [MethodCall] call to method NextDouble
355+
# 10| -1: [LocalVariableAccess] access to local variable rand
356+
# 10| 1: [DoubleLiteral] 0.5
357+
# 12| 2: [LocalVariableDeclStmt] ... ...;
358+
# 12| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x = ...
359+
# 12| -1: [TypeMention] int?
360+
# 12| 1: [TypeMention] int
361+
# 12| 0: [LocalVariableAccess] access to local variable x
362+
# 12| 1: [ConditionalExpr] ... ? ... : ...
363+
# 12| 0: [LocalVariableAccess] access to local variable condition
364+
# 13| 1: [CastExpr] (...) ...
365+
# 13| 1: [IntLiteral] 12
366+
# 14| 2: [NullLiteral] null
367+
# 16| 3: [LocalVariableDeclStmt] ... ...;
368+
# 16| 0: [LocalVariableDeclAndInitExpr] IEnumerable<Int32> xs = ...
369+
# 16| -1: [TypeMention] IEnumerable<Int32>
370+
# 16| 1: [TypeMention] int
371+
# 16| 0: [LocalVariableAccess] access to local variable xs
372+
# 16| 1: [ConditionalExpr] ... ? ... : ...
373+
# 16| 0: [IsExpr] ... is ...
374+
# 16| 0: [LocalVariableAccess] access to local variable x
375+
# 16| 1: [ConstantPatternExpr,NullLiteral] null
376+
# 17| 1: [ObjectCreation] object creation of type List<Int32>
377+
# 17| -2: [TypeMention] List<Int32>
378+
# 17| 1: [TypeMention] int
379+
# 17| -1: [CollectionInitializer] { ..., ... }
380+
# 17| 0: [ElementInitializer] call to method Add
381+
# 17| 0: [IntLiteral] 0
382+
# 17| 1: [ElementInitializer] call to method Add
383+
# 17| 0: [IntLiteral] 1
384+
# 18| 2: [ArrayCreation] array creation of type Int32[]
385+
# 18| -2: [TypeMention] Int32[]
386+
# 18| 1: [TypeMention] int
387+
# 18| -1: [ArrayInitializer] { ..., ... }
388+
# 18| 0: [IntLiteral] 2
389+
# 18| 1: [IntLiteral] 3
338390
TypeParameterNullability.cs:
339391
# 1| [Interface] I1
340392
# 3| [Class] A2
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public class TargetType
6+
{
7+
public void M2()
8+
{
9+
var rand = new Random();
10+
var condition = rand.NextDouble() > 0.5;
11+
12+
int? x = condition
13+
? 12
14+
: null;
15+
16+
IEnumerable<int> xs = x is null
17+
? new List<int>() { 0, 1 }
18+
: new int[] { 2, 3 };
19+
}
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| TargetType.cs:12:18:14:18 | ... ? ... : ... | int? | int? | null |
2+
| TargetType.cs:16:31:18:32 | ... ? ... : ... | IEnumerable<Int32> | List<Int32> | Int32[] |
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import csharp
2+
3+
query predicate conditional(ConditionalExpr expr, string t, string t1, string t2) {
4+
expr.getType().toStringWithTypes() = t and
5+
expr.getThen().getType().toStringWithTypes() = t1 and
6+
expr.getElse().getType().toStringWithTypes() = t2
7+
}

0 commit comments

Comments
 (0)