Skip to content

Commit 083214f

Browse files
committed
C#: Use inline test expectations for FieldFlow.ql
1 parent ed6a182 commit 083214f

File tree

12 files changed

+1227
-344
lines changed

12 files changed

+1227
-344
lines changed

csharp/ql/test/library-tests/dataflow/fields/A.cs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ public class A
22
{
33
public void M1()
44
{
5-
var c = new C();
5+
var c = Source<C>(1);
66
var b = B.Make(c);
7-
Sink(b.c); // flow
7+
Sink(b.c); // $ hasValueFlow=1
88
}
99

1010
public void M2()
1111
{
1212
var b = new B();
13-
b.Set(new C1());
14-
Sink(b.Get()); // flow
15-
Sink((new B(new C())).Get()); // flow
13+
b.Set(Source<C1>(2.1));
14+
Sink(b.Get()); // $ hasValueFlow=2.1
15+
Sink((new B(Source<C>(2.2))).Get()); // $ hasValueFlow=2.2
1616
}
1717

1818
public void M3()
1919
{
2020
var b1 = new B();
2121
B b2;
22-
b2 = SetOnB(b1, new C2());
23-
Sink(b1.c); // no flow
24-
Sink(b2.c); // flow
22+
b2 = SetOnB(b1, Source<C2>(3));
23+
Sink(b1.c);
24+
Sink(b2.c); // $ hasValueFlow=3
2525
}
2626

2727
public void M4()
2828
{
2929
var b1 = new B();
3030
B b2;
31-
b2 = SetOnBWrap(b1, new C2());
32-
Sink(b1.c); // no flow
33-
Sink(b2.c); // flow
31+
b2 = SetOnBWrap(b1, Source<C2>(4));
32+
Sink(b1.c);
33+
Sink(b2.c); // $ hasValueFlow=4
3434
}
3535

3636
public B SetOnBWrap(B b1, C c)
@@ -52,7 +52,7 @@ public B SetOnB(B b1, C c)
5252

5353
public void M5()
5454
{
55-
var a = new A();
55+
var a = Source<A>(5);
5656
C1 c1 = new C1();
5757
c1.a = a;
5858
M6(c1);
@@ -61,7 +61,7 @@ public void M6(C c)
6161
{
6262
if (c is C1)
6363
{
64-
Sink(((C1)c).a); // flow
64+
Sink(((C1)c).a); // $ hasValueFlow=5
6565
}
6666
C cc;
6767
if (c is C2)
@@ -80,47 +80,47 @@ public void M6(C c)
8080

8181
public void M7(B b)
8282
{
83-
b.Set(new C());
83+
b.Set(Source<C>(7));
8484
}
8585
public void M8()
8686
{
8787
var b = new B();
8888
M7(b);
89-
Sink(b.c); // flow
89+
Sink(b.c); // $ hasValueFlow=7
9090
}
9191

9292
public class D
9393
{
9494
public B b;
9595
public D(B b, bool x)
9696
{
97-
b.c = new C();
98-
this.b = x ? b : new B();
97+
b.c = Source<C>(9.1);
98+
this.b = x ? b : Source<B>(9.2);
9999
}
100100
}
101101

102102
public void M9()
103103
{
104-
var b = new B();
104+
var b = Source<B>(9.3);
105105
var d = new D(b, R());
106-
Sink(d.b); // flow x2
107-
Sink(d.b.c); // flow
108-
Sink(b.c); // flow
106+
Sink(d.b); // $ hasValueFlow=9.2 $ hasValueFlow=9.3
107+
Sink(d.b.c); // $ hasValueFlow=9.1
108+
Sink(b.c); // $ hasValueFlow=9.1
109109
}
110110

111111
public void M10()
112112
{
113-
var b = new B();
113+
var b = Source<B>(10);
114114
var l1 = new MyList(b, new MyList(null, null));
115115
var l2 = new MyList(null, l1);
116116
var l3 = new MyList(null, l2);
117117
Sink(l3.head); // no flow, b is nested beneath at least one .next
118118
Sink(l3.next.head); // flow, the precise nesting depth isn't tracked
119-
Sink(l3.next.next.head); // flow
119+
Sink(l3.next.next.head); // $ hasValueFlow=10
120120
Sink(l3.next.next.next.head); // no flow
121121
for (var l = l3; l != null; l = l.next)
122122
{
123-
Sink(l.head); // flow
123+
Sink(l.head); // $ hasValueFlow=10
124124
}
125125
}
126126

@@ -160,4 +160,6 @@ public MyList(B head, MyList next)
160160
this.next = next;
161161
}
162162
}
163+
164+
static T Source<T>(object source) => throw null;
163165
}

csharp/ql/test/library-tests/dataflow/fields/B.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ public class B
22
{
33
public void M1()
44
{
5-
var e = new Elem();
5+
var e = Source<Elem>(1);
66
var b1 = new Box1(e, null);
77
var b2 = new Box2(b1);
8-
Sink(b2.box1.elem1); // flow
8+
Sink(b2.box1.elem1); // $ hasValueFlow=1
99
Sink(b2.box1.elem2); // no flow
1010
}
1111

1212
public void M2()
1313
{
14-
var e = new Elem();
14+
var e = Source<Elem>(2);
1515
var b1 = new Box1(null, e);
1616
var b2 = new Box2(b1);
1717
Sink(b2.box1.elem1); // no flow
18-
Sink(b2.box1.elem2); // flow
18+
Sink(b2.box1.elem2); // $ hasValueFlow=2
1919
}
2020

2121
public static void Sink(object o) { }
@@ -41,4 +41,6 @@ public Box2(Box1 b1)
4141
this.box1 = b1;
4242
}
4343
}
44+
45+
static T Source<T>(object source) => throw null;
4446
}
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
public class C
22
{
3-
private Elem s1 = new Elem();
4-
private readonly Elem s2 = new Elem();
3+
private Elem s1 = Source<Elem>(1);
4+
private readonly Elem s2 = Source<Elem>(2);
55
private Elem s3;
6-
private static Elem s4 = new Elem();
7-
private Elem s5 { get; set; } = new Elem();
8-
private Elem s6 { get => new Elem(); set { } }
6+
private static Elem s4 = Source<Elem>(4);
7+
private Elem s5 { get; set; } = Source<Elem>(5);
8+
private Elem s6 { get => Source<Elem>(6); set { } }
99

1010
void M1()
1111
{
@@ -15,20 +15,22 @@ void M1()
1515

1616
private C()
1717
{
18-
this.s3 = new Elem();
18+
this.s3 = Source<Elem>(3);
1919
}
2020

2121
public void M2()
2222
{
23-
Sink(s1);
24-
Sink(s2);
25-
Sink(s3);
26-
Sink(s4);
27-
Sink(s5);
28-
Sink(s6);
23+
Sink(s1); // $ hasValueFlow=1
24+
Sink(s2); // $ hasValueFlow=2
25+
Sink(s3); // $ hasValueFlow=3
26+
Sink(s4); // $ hasValueFlow=4
27+
Sink(s5); // $ hasValueFlow=5
28+
Sink(s6); // $ hasValueFlow=6
2929
}
3030

3131
public static void Sink(object o) { }
3232

33+
static T Source<T>(object source) => throw null;
34+
3335
public class Elem { }
3436
}

csharp/ql/test/library-tests/dataflow/fields/D.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,28 @@ static D Create(object o1, object o2, object o3)
2626

2727
private void M()
2828
{
29-
var o = new object();
29+
var o = Source<object>(1);
3030

3131
var d = Create(o, null, null);
32-
Sink(d.AutoProp); // flow
32+
Sink(d.AutoProp); // $ hasValueFlow=1
3333
Sink(d.TrivialProp); // no flow
3434
Sink(d.trivialPropField); // no flow
3535
Sink(d.ComplexProp); // no flow
3636

37-
d = Create(null, o, null);
37+
d = Create(null, Source<object>(2), null);
3838
Sink(d.AutoProp); // no flow
39-
Sink(d.TrivialProp); // flow
40-
Sink(d.trivialPropField); // flow
41-
Sink(d.ComplexProp); // flow
39+
Sink(d.TrivialProp); // $ hasValueFlow=2
40+
Sink(d.trivialPropField); // $ hasValueFlow=2
41+
Sink(d.ComplexProp); // $ hasValueFlow=2
4242

43-
d = Create(null, null, o);
43+
d = Create(null, null, Source<object>(3));
4444
Sink(d.AutoProp); // no flow
45-
Sink(d.TrivialProp); // flow
46-
Sink(d.trivialPropField); // flow
47-
Sink(d.ComplexProp); // flow
45+
Sink(d.TrivialProp); // $ hasValueFlow=3
46+
Sink(d.trivialPropField); // $ hasValueFlow=3
47+
Sink(d.ComplexProp); // $ hasValueFlow=3
4848
}
4949

5050
public static void Sink(object o) { }
51+
52+
static T Source<T>(object source) => throw null;
5153
}

csharp/ql/test/library-tests/dataflow/fields/E.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ static void NotASetter(S s, object o)
1919

2020
private void M()
2121
{
22-
var o = new object();
22+
var o = Source<object>(1);
2323
var s = CreateS(o);
24-
Sink(s.Field); // flow
24+
Sink(s.Field); // $ hasValueFlow=1
2525

2626
s = new S();
2727
NotASetter(s, o);
2828
Sink(s.Field); // no flow
2929
}
3030

3131
public static void Sink(object o) { }
32+
33+
static T Source<T>(object source) => throw null;
3234
}

csharp/ql/test/library-tests/dataflow/fields/F.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ public class F
77

88
private void M()
99
{
10-
var o = new object();
10+
var o = Source<object>(1);
1111
var f = Create(o, null);
12-
Sink(f.Field1); // flow
12+
Sink(f.Field1); // $ hasValueFlow=1
1313
Sink(f.Field2); // no flow
1414

15-
f = Create(null, o);
15+
f = Create(null, Source<object>(2));
1616
Sink(f.Field1); // no flow
17-
Sink(f.Field2); // flow
17+
Sink(f.Field2); // $ hasValueFlow=2
1818

19-
f = new F() { Field1 = o };
20-
Sink(f.Field1); // flow
19+
f = new F() { Field1 = Source<object>(3) };
20+
Sink(f.Field1); // $ hasValueFlow=3
2121
Sink(f.Field2); // no flow
2222

23-
f = new F() { Field2 = o };
23+
f = new F() { Field2 = Source<object>(4) };
2424
Sink(f.Field1); // no flow
25-
Sink(f.Field2); // flow
25+
Sink(f.Field2); // $ hasValueFlow=4
2626
}
2727

2828
public static void Sink(object o) { }
29+
30+
static T Source<T>(object source) => throw null;
2931
}

0 commit comments

Comments
 (0)