Skip to content

Commit 4f77430

Browse files
committed
C#: Restructure existing generics tests
1 parent b745809 commit 4f77430

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+300
-384
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
test1
2+
| generics.cs:7:23:7:40 | GenericDelegate<> |
3+
test2
4+
| generics.cs:9:18:9:18 | A |
5+
test3
6+
| generics.cs:13:18:13:21 | A<X> | generics.cs:13:18:13:21 | A<> |
7+
test4
8+
test5
9+
test6
10+
| generics.cs:13:18:13:21 | A<T> | generics.cs:22:18:22:21 | B<> | generics.cs:22:18:22:21 | B<X> | generics.cs:25:23:25:24 | at |
11+
test7
12+
| generics.cs:13:18:13:21 | A<String> | generics.cs:22:18:22:21 | B<String> |
13+
test8
14+
| generics.cs:22:18:22:21 | B<String> | generics.cs:31:21:31:29 | fooParams |
15+
test9
16+
| generics.cs:22:18:22:21 | B<String> | generics.cs:35:51:35:53 | set_Name | generics.cs:35:51:35:53 | set_Name |
17+
test10
18+
| generics.cs:22:18:22:21 | B<String> | generics.cs:37:41:37:47 | myEvent |
19+
test11
20+
| generics.cs:22:18:22:21 | B<String> | generics.cs:39:37:39:38 | ++ |
21+
test12
22+
| generics.cs:60:18:60:24 | Grid<Int32> | generics.cs:73:18:73:21 | Item |
23+
test13
24+
| generics.cs:60:18:60:24 | Grid<Int32> | generics.cs:68:20:68:23 | Item |
25+
| generics.cs:60:18:60:24 | Grid<Int32> | generics.cs:73:18:73:21 | Item |
26+
test14
27+
| generics.cs:60:18:60:24 | Grid<> | generics.cs:68:20:68:23 | Item |
28+
test15
29+
| generics.cs:7:23:7:40 | GenericDelegate<String> |
30+
test16
31+
| generics.cs:134:11:134:16 | Subtle | generics.cs:137:21:137:25 | fs |
32+
| generics.cs:134:11:134:16 | Subtle | generics.cs:139:21:139:25 | fs |
33+
| generics.cs:134:11:134:16 | Subtle | generics.cs:141:21:141:22 | fs |
34+
test17
35+
| generics.cs:134:11:134:16 | Subtle | generics.cs:137:24:137:24 | X | generics.cs:137:21:137:25 | fs | generics.cs:139:24:139:24 | X | generics.cs:139:21:139:25 | fs | 1 |
36+
| generics.cs:134:11:134:16 | Subtle | generics.cs:139:24:139:24 | X | generics.cs:139:21:139:25 | fs | generics.cs:137:24:137:24 | X | generics.cs:137:21:137:25 | fs | 2 |
37+
test18
38+
| generics.cs:13:18:13:21 | A<> | generics.cs:18:18:18:23 | bar | generics.cs:18:27:18:27 | x | 0 | X | 2 | 2 |
39+
| generics.cs:13:18:13:21 | A<> | generics.cs:18:18:18:23 | bar | generics.cs:18:32:18:32 | t | 0 | T | 2 | 2 |
40+
| generics.cs:13:18:13:21 | A<Int32> | generics.cs:18:18:18:23 | bar | generics.cs:18:27:18:27 | x | 0 | X | 2 | 2 |
41+
| generics.cs:13:18:13:21 | A<Int32> | generics.cs:18:18:18:23 | bar | generics.cs:18:27:18:27 | x | 1 | Test | 2 | 2 |
42+
| generics.cs:13:18:13:21 | A<Int32> | generics.cs:18:18:18:23 | bar | generics.cs:18:32:18:32 | t | 0 | Int32 | 2 | 2 |
43+
| generics.cs:13:18:13:21 | A<Int32> | generics.cs:18:18:18:23 | bar | generics.cs:18:32:18:32 | t | 1 | Int32 | 2 | 2 |
44+
| generics.cs:13:18:13:21 | A<String> | generics.cs:18:18:18:23 | bar | generics.cs:18:27:18:27 | x | 0 | X | 2 | 2 |
45+
| generics.cs:13:18:13:21 | A<String> | generics.cs:18:18:18:23 | bar | generics.cs:18:27:18:27 | x | 1 | Int32 | 2 | 2 |
46+
| generics.cs:13:18:13:21 | A<String> | generics.cs:18:18:18:23 | bar | generics.cs:18:32:18:32 | t | 0 | String | 2 | 2 |
47+
| generics.cs:13:18:13:21 | A<String> | generics.cs:18:18:18:23 | bar | generics.cs:18:32:18:32 | t | 1 | String | 2 | 2 |
48+
test19
49+
| generics.cs:137:21:137:25 | fs | generics.cs:137:24:137:24 | X | 1 |
50+
| generics.cs:139:21:139:25 | fs | generics.cs:139:24:139:24 | X | 1 |
51+
test20
52+
test21
53+
| generics.cs:147:14:147:14 | E | generics.cs:145:11:145:18 | Param<> |
54+
test22
55+
| generics.cs:152:14:152:19 | CM1 | Double |
56+
| generics.cs:152:14:152:19 | CM1 | Int32 |
57+
| generics.cs:153:11:153:16 | CM2 | Double |
58+
| generics.cs:153:11:153:16 | CM2 | Int32 |
59+
| generics.cs:157:23:157:29 | CM3 | Double |
60+
| generics.cs:157:23:157:29 | CM3 | Double |
61+
test23
62+
| generics.cs:178:11:178:24 | Inheritance<> | generics.cs:173:15:173:26 | Interface<T> |
63+
| generics.cs:178:11:178:24 | Inheritance<Int32> | generics.cs:173:15:173:26 | Interface<Int32> |
64+
test24
65+
| generics.cs:188:15:188:39 | Interface2<,> | generics.cs:188:29:188:30 | T1 | in |
66+
| generics.cs:188:15:188:39 | Interface2<,> | generics.cs:188:37:188:38 | T2 | out |
67+
test25
68+
| generics.cs:157:23:157:29 | CM3 |
69+
test26
70+
test27
71+
| generics.cs:51:22:51:29 | Inner<String> | generics.cs:51:22:51:29 | Inner<> | generics.cs:51:22:51:29 | Inner<> |
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
import csharp
2+
3+
query predicate test1(UnboundGenericDelegateType d) {
4+
d.hasName("GenericDelegate<>") and
5+
d.getTypeParameter(0).hasName("T") and
6+
d.getParameter(0).getType().hasName("T")
7+
}
8+
9+
query predicate test2(Class c) {
10+
c.hasName("A") and
11+
not c instanceof UnboundGenericClass
12+
}
13+
14+
query predicate test3(ConstructedClass c, UnboundGenericClass d) {
15+
d.hasName("A<>") and
16+
c.getTypeArgument(0).hasName("X") and
17+
c.getTypeArgument(0) instanceof TypeParameter and
18+
c.getUnboundGeneric() = d
19+
}
20+
21+
query predicate test4(UnboundGenericClass c, string s) {
22+
c.fromSource() and
23+
not c.getName().matches("%<%>") and
24+
s = "Unbound generic class with inconsistent name"
25+
}
26+
27+
query predicate test5(ConstructedClass c, string s) {
28+
c.fromSource() and
29+
not c.getName().matches("%<%>") and
30+
s = "Constructed class with inconsistent name"
31+
}
32+
33+
query predicate test6(ConstructedClass at, UnboundGenericClass b, ConstructedClass bt, Field f) {
34+
at.hasName("A<T>") and
35+
b.hasName("B<>") and
36+
bt.hasName("B<X>") and
37+
at.getTypeArgument(0).hasName("T") and
38+
at.getTypeArgument(0) instanceof TypeParameter and
39+
at.getTypeArgument(0) = b.getTypeParameter(0) and
40+
bt.getUnboundGeneric() = b and
41+
f.getDeclaringType() = b and
42+
f.getType() = at
43+
}
44+
45+
query predicate test7(ConstructedClass aString, ConstructedClass bString) {
46+
aString.hasName("A<String>") and
47+
bString.hasName("B<String>") and
48+
aString.getSourceDeclaration().hasName("A<>") and
49+
bString.getSourceDeclaration().hasName("B<>")
50+
}
51+
52+
query predicate test8(ConstructedClass bString, Method m) {
53+
bString.hasName("B<String>") and
54+
m.getDeclaringType() = bString and
55+
m.hasName("fooParams") and
56+
m.getParameter(0).getType().(ArrayType).getElementType() instanceof StringType and
57+
m.getSourceDeclaration().getDeclaringType() = m.getDeclaringType().getSourceDeclaration()
58+
}
59+
60+
query predicate test9(ConstructedClass bString, Setter sourceSetter, Setter setter) {
61+
exists(Property p |
62+
bString.hasName("B<String>") and
63+
p.getDeclaringType() = bString and
64+
p.hasName("Name") and
65+
p.getSourceDeclaration().getDeclaringType() = p.getDeclaringType().getSourceDeclaration() and
66+
p.getSetter().getParameter(0).getType() instanceof StringType and
67+
p.getSetter().getSourceDeclaration() = p.getSourceDeclaration().getSetter() and
68+
p.getGetter().getSourceDeclaration() = p.getSourceDeclaration().getGetter() and
69+
sourceSetter = p.getSourceDeclaration().getSetter() and
70+
setter = p.getSetter()
71+
)
72+
}
73+
74+
query predicate test10(ConstructedClass bString, Event e) {
75+
bString.hasName("B<String>") and
76+
e.getDeclaringType() = bString and
77+
e.hasName("myEvent") and
78+
e.getSourceDeclaration().getDeclaringType() = e.getDeclaringType().getSourceDeclaration() and
79+
e.getType().(ConstructedDelegateType).getTypeArgument(0) instanceof StringType and
80+
e.getAddEventAccessor().getSourceDeclaration() = e.getSourceDeclaration().getAddEventAccessor() and
81+
e.getRemoveEventAccessor().getSourceDeclaration() =
82+
e.getSourceDeclaration().getRemoveEventAccessor()
83+
}
84+
85+
query predicate test11(ConstructedClass bString, Operator o) {
86+
bString.hasName("B<String>") and
87+
o.getDeclaringType() = bString and
88+
o instanceof IncrementOperator and
89+
o.getSourceDeclaration().getDeclaringType() = o.getDeclaringType().getSourceDeclaration()
90+
}
91+
92+
query predicate test12(ConstructedClass gridInt, Indexer i) {
93+
gridInt.hasName("Grid<Int32>") and
94+
i.getDeclaringType() = gridInt and
95+
i.getSourceDeclaration().getDeclaringType() = i.getDeclaringType().getSourceDeclaration() and
96+
i.getGetter().getSourceDeclaration() = i.getSourceDeclaration().getGetter() and
97+
i.getSetter().getSourceDeclaration() = i.getSourceDeclaration().getSetter()
98+
}
99+
100+
query predicate test13(ConstructedClass gridInt, Indexer i) {
101+
gridInt.hasName("Grid<Int32>") and
102+
i.getDeclaringType() = gridInt and
103+
i.getType() instanceof IntType
104+
}
105+
106+
query predicate test14(UnboundGenericClass gridInt, Indexer i) {
107+
gridInt.hasName("Grid<>") and
108+
i.getDeclaringType() = gridInt and
109+
i.getType() instanceof IntType
110+
}
111+
112+
query predicate test15(ConstructedDelegateType d) {
113+
d.hasName("GenericDelegate<String>") and
114+
d.getTypeArgument(0) instanceof StringType and
115+
d.getParameter(0).getType() instanceof StringType
116+
}
117+
118+
query predicate test16(Class c, Method m) {
119+
c.hasName("Subtle") and
120+
count(c.getAMethod()) = 3 and
121+
m = c.getAMethod()
122+
}
123+
124+
query predicate test17(
125+
Class c, TypeParameter p, UnboundGenericMethod m, TypeParameter q, UnboundGenericMethod n,
126+
int numParams
127+
) {
128+
c.hasName("Subtle") and
129+
m = c.getAMethod() and
130+
m.getATypeParameter() = p and
131+
n = c.getAMethod() and
132+
n.getATypeParameter() = q and
133+
m != n and
134+
p != q and
135+
numParams = m.getNumberOfParameters()
136+
}
137+
138+
query predicate test18(
139+
Class c, Method m, Parameter p, int numArgs, string typeName, int numParams, int numTypes
140+
) {
141+
c.getName().matches("A<%") and
142+
m = c.getAMethod() and
143+
p = m.getAParameter() and
144+
numArgs = count(m.(ConstructedMethod).getATypeArgument()) and
145+
typeName = p.getType().getName() and
146+
numParams = count(m.getAParameter()) and
147+
numTypes = count(m.getAParameter().getType())
148+
}
149+
150+
/** Test that locations are populated for the type parameters of generic methods. */
151+
query predicate test19(UnboundGenericMethod m, TypeParameter tp, int hasLoc) {
152+
m.hasName("fs") and
153+
tp = m.getATypeParameter() and
154+
if exists(tp.getLocation()) then hasLoc = 1 else hasLoc = 0
155+
}
156+
157+
/** Test that locations are populated for unbound generic types. */
158+
query predicate test20(UnboundGenericType t, string s) {
159+
not type_location(t, _) and s = "Missing location"
160+
}
161+
162+
/**
163+
* This tests a regression in the extractor where the following failed to extract:
164+
*
165+
* class Foo<T>
166+
* {
167+
* enum E { a };
168+
* }
169+
*/
170+
query predicate test21(Enum e, Class c) {
171+
c.hasName("Param<>") and
172+
e.hasName("E") and
173+
e.getDeclaringType() = c
174+
}
175+
176+
query predicate test22(ConstructedMethod m, string tpName) {
177+
m.getName().matches("CM%") and
178+
tpName = m.getATypeArgument().getName()
179+
}
180+
181+
query predicate test23(Class c, Interface i) {
182+
c.getName().matches("Inheritance%") and
183+
i = c.getABaseInterface()
184+
}
185+
186+
query predicate test24(UnboundGenericInterface ugi, TypeParameter tp, string s) {
187+
ugi.fromSource() and
188+
ugi.getATypeParameter() = tp and
189+
(
190+
tp.isOut() and s = "out"
191+
or
192+
tp.isIn() and s = "in"
193+
)
194+
}
195+
196+
query predicate test25(ConstructedMethod cm) {
197+
cm.hasName("CM3") and
198+
cm.getParameter(0).getType() instanceof DoubleType and
199+
cm.getParameter(1).getType() instanceof IntType and
200+
cm.getReturnType() instanceof DoubleType and
201+
exists(Method sourceDeclaration |
202+
sourceDeclaration = cm.getSourceDeclaration() and
203+
sourceDeclaration.getParameter(0).getType().(TypeParameter).hasName("T2") and
204+
sourceDeclaration.getParameter(1).getType().(TypeParameter).hasName("T1") and
205+
sourceDeclaration.getReturnType().(TypeParameter).hasName("T2")
206+
) and
207+
exists(Method unbound |
208+
unbound = cm.getUnboundGeneric() and
209+
unbound.getParameter(0).getType().(TypeParameter).hasName("T2") and
210+
unbound.getParameter(1).getType() instanceof IntType and
211+
unbound.getReturnType().(TypeParameter).hasName("T2")
212+
)
213+
}
214+
215+
query predicate test26(ConstructedGeneric cg, string s) {
216+
// Source declaration and unbound generic must be unique
217+
(
218+
strictcount(cg.getSourceDeclaration+()) > 1 or
219+
strictcount(cg.getUnboundGeneric()) > 1
220+
) and
221+
s = "Non-unique source decl or unbound generic"
222+
}
223+
224+
query predicate test27(ConstructedType ct, UnboundGenericType ugt, UnboundGenericType sourceDecl) {
225+
ct instanceof NestedType and
226+
ugt = ct.getUnboundGeneric() and
227+
sourceDecl = ct.getSourceDeclaration() and
228+
ugt != sourceDecl
229+
}

csharp/ql/test/library-tests/generics/Generics1.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

csharp/ql/test/library-tests/generics/Generics1.ql

Lines changed: 0 additions & 12 deletions
This file was deleted.

csharp/ql/test/library-tests/generics/Generics10.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

csharp/ql/test/library-tests/generics/Generics10.ql

Lines changed: 0 additions & 17 deletions
This file was deleted.

csharp/ql/test/library-tests/generics/Generics11.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

csharp/ql/test/library-tests/generics/Generics11.ql

Lines changed: 0 additions & 13 deletions
This file was deleted.

csharp/ql/test/library-tests/generics/Generics12.expected

Lines changed: 0 additions & 1 deletion
This file was deleted.

csharp/ql/test/library-tests/generics/Generics12.ql

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)